数据库
首页 > 数据库> > Unity C# SQLite4Unity 用于Android APK 使用介绍

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

}

  

标签:SQLite4Unity,string,C#,SqliteCommand,System,APK,mCon,SqliteConnection,filePathNa
来源: https://www.cnblogs.com/guangzhiruijie/p/16404092.html