Unity中数据库建立与读写详情

两个类,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 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值