两个类,DbAccess和TestDB
TestDB类测试,创建数据库,创建数据表,插入数据,更新数据,删除数据,查询数据
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using System.IO;
public class TestDB : MonoBehaviour {
DbAccess db; //数据库
string appDBPath; //数据库路径
string name;
int age;
float exp;
//创建数据库
void CreateDataBase()
{
#if UNITY_EDITOR
appDBPath = Application.dataPath + "/Test.db";
#elif UNITY_STANDALONE_WIN
appDBPath = Application.dataPath + "/Test.db";
#elif UNITY_ANDROID
appDBPath = Application.persistentDataPath + "/Test.db";
//判断路径内数据库是否存在
if(!File.Exists(appDBPath))=
{
//拷贝数据库
StartCoroutine(CopyDataBase());
}
#elif UNITY_IPHONE
appDBPath = Application.persistentDataPath + "/Test.db";
//判断路径内数据库是否存在
if(!File.Exists(appDBPath))
{
//拷贝数据库
StartCoroutine(CopyDataBase());
}
#endif
//"URI=file:" 必须添加
db = new DbAccess ("URI=file:" + appDBPath);
}
//拷贝数据库
IEnumerator CopyDataBase()
{
//用www先从unity中下载数据库
//Application.streamingAssetsPath
#if UNITY_IPHONE
WWW loadDB = new WWW(Application.dataPath
+ "/Raw" + "/Test.db");
#elif UNITY_ANDROID
WWW loadDB = new WWW("jar:file://" +
Application.dataPath + "!/assets" + "/Test.db");
#elif UNITY_EDITOR||UNITY_STANDALONE_WIN
WWW loadDB = new WWW(
Application.dataPath + "/Test.db");
#endif
yield return loadDB;
//拷贝数据库
File.WriteAllBytes (appDBPath, loadDB.bytes);
}
void