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()
    {
        
    }

}

  

posted @ 2022-06-23 09:47  多见多闻  阅读(63)  评论(0)    收藏  举报