sqlite for Unity with C sharp

using UnityEngine;
using System;
using System.Collections;
using Mono.Data.Sqlite;
using System.Data;
using System.IO;

public class DBManager : MonoBehaviour {
	static private SqliteConnection _dbConnection;
	static private SqliteCommand _dbCommand;
	static private SqliteDataReader _reader;

	private static DBManager _sharedDBManager = null;

	public static DBManager sharedManager() {
		if (_sharedDBManager == null) {
			GameObject obj = new GameObject();
			_sharedDBManager = obj.AddComponent<DBManager>();
		}
		return _sharedDBManager;
	}

	public DBManager () {

	}
	
	public void openDB (string fileName) {
		string createString;
		string appDBPath = Application.persistentDataPath + "/" + fileName;
		if (Application.platform == RuntimePlatform.IPhonePlayer) {
			createString = @"Data Source=" + appDBPath;
		} else if (Application.platform ==  RuntimePlatform.Android) {
			createString = "URI=file:" + appDBPath;
		} else {
			Debug.Log ("editor");
			createString = "data source = " + Application.streamingAssetsPath + "/" + fileName;
		}
//		yield return StartCoroutine(copyFileToDevice(fileName));

		//establish database connection
		try {
			_dbConnection = new SqliteConnection(createString);
			_dbConnection.Open();
			Debug.Log("connected to db");
		} catch (Exception e){
			string exceptionString = e.ToString();
			Debug.Log(exceptionString);
		}
	}

	public IEnumerator copyFileToDevice(string fileName) {
		string appDBPath = Application.persistentDataPath + "/" + fileName;
		if (!File.Exists(appDBPath)) {
			string prefix;
			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				prefix = "file://";
			} else {
				prefix = "";
			}
			WWW loadDB = new WWW(prefix + Application.streamingAssetsPath + "/" + fileName);
			Debug.Log(prefix + Application.streamingAssetsPath + "/" + fileName);
			yield return loadDB;
			File.WriteAllBytes(appDBPath, loadDB.bytes);
			Debug.Log("copy file to local dir");
		}
	}
	
	public void closeConnection () {
		if (_dbCommand != null) {
			_dbCommand.Dispose();
		}
		_dbCommand = null;
		
		if (_reader != null) {
			_reader.Close();
		}
		_reader = null;
		
		if (_dbConnection != null) {
			_dbConnection.Dispose();
		}
		_dbConnection = null;
		Debug.Log("Disconnected from db");
	}
	
	public SqliteDataReader executeQuery (string query) {
		Debug.Log("sql: " + query);
		_dbCommand = _dbConnection.CreateCommand();
		_dbCommand.CommandText = query;
		_reader = _dbCommand.ExecuteReader();
		return _reader;
	}

	//check table exists or not in sqlite database
	public bool CheckTableExists(string tableName) {
		string query = "select count(type) from sqlite_master where type='table' and name ='" + tableName + "'";
		SqliteDataReader reader = this.executeQuery(query);
		return reader.HasRows;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值