sqlite for Unity with C sharp
来源:互联网 发布:java实现平衡二叉树 编辑:程序博客网 时间:2024/05/20 05:46
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 connectiontry {_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 databasepublic 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;}}
0 0
- sqlite for Unity with C sharp
- communicating-with-serial-port-in-C-Sharp
- Get WinAD UserName for C Sharp
- C sharp programming for absolute beginners
- c sharp
- C#(Sharp)
- C SHARP(C#)
- c.sharp自定义事件
- effective c sharp 印象
- C Sharp Windows Forms
- C Sharp 读取Excel
- c sharp日志开端
- C Sharp基础
- String in C sharp
- c sharp 异步编程
- C sharp中的“封装”
- c sharp 类型基础
- Unity workflow with SVN for small teams
- Jackson使用(四)----springmvc中配置jackson
- adb devices : error:
- Bayesian and frequentist reasoning in plain English
- twisted入门教程之九:第二个小插曲,Deferred
- c++学习笔记之文件操作
- sqlite for Unity with C sharp
- Android 与pc建立socket连接
- 基于ajax的消息轮播
- twisted入门教程之十:增强defer功能的客户端
- 独立开发者:新手做2D手游该用哪些工具?
- 机器学习系统设计笔迹(一)
- java内存泄露
- twisted入门教程之十一:改进诗歌下载服务器
- error:连接数据库失败