Unity C# SQLite4Unity 用于Android APK 使用介绍

2022/6/24 2:19:47

本文主要是介绍Unity C# SQLite4Unity 用于Android APK 使用介绍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Mono.Data.Sqlite;

public class Main : MonoBehaviour
{
	string filePathName = string.Empty;
    // Start is called before the first frame update
    void Start()
    {        			
		SqliteConnection mCon = null;
#if UNITY_EDITOR
		filePathName = "MyTest.db";
		if (!System.IO.File.Exists(filePathName))
		{
			SqliteConnection.CreateFile(filePathName);
			Debug.Log("创建了数据库文件");
		}
		mCon = new SqliteConnection("data source=" + filePathName);
#elif UNITY_ANDROID
        filePathName = Application.persistentDataPath + "/MyTest.db";
		if (!System.IO.File.Exists(filePathName))
		{
			SqliteConnection.CreateFile(filePathName);
			Debug.Log("创建了数据库文件");
		}
        mCon = new SqliteConnection("URI=file:" + filePathName);		
#endif
		mCon.Open();

		string dropDBStr = "drop table if exists Scores";
		string createDBStr = "create table Scores (name TEXT,score int)";
		SqliteCommand cmdDrop = new SqliteCommand(dropDBStr, mCon);
		cmdDrop.ExecuteNonQuery();
		SqliteCommand cmd = new SqliteCommand(createDBStr, mCon);
		cmd.ExecuteNonQuery();
		Debug.Log("创建了数据表");

		mCon.Close();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

}

  



这篇关于Unity C# SQLite4Unity 用于Android APK 使用介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程