Unity数据库的学习笔记
来源:互联网 发布:南方大数据300a 编辑:程序博客网 时间:2024/06/10 00:07
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Mono.Data.Sqlite;
public class DataBaseTool : Singleton<DataBaseTool> {
public string DataBaseName="LOLData.sqlite";
private SqliteConnection conn;
private SqliteCommand command;
private SqliteDataReader reader;
private string datapath="";
public DataBaseTool(){
if(conn==null){
datapath= "Data Source=" + Application.streamingAssetsPath + "/" + DataBaseName;
#if UNITY_ANDRIOD
datapath=AndriodPaltformSet();
#endif
try{
conn = new SqliteConnection (datapath);
}catch(System.Exception e){
Debug.Log ("创建数据库连接失败...");
Debug.LogError (e.Message);
}
}
}
//安卓平台数据库设置
public string AndriodPaltformSet(){
string path = "URI = file:" + Application.persistentDataPath + "/" + DataBaseName;
//Android APK中数据库文件的路径
string androidPath = "jar:file://" + Application.dataPath + "!/assets/" + DataBaseName;
//Android沙盒路径
string androidFilePath = Application.persistentDataPath + "/" + DataBaseName;
//如果Android项目源文件中不存在数据库文件,说明没有加载过,需要加载
if(!File.Exists(androidFilePath))
{
//从APK路径拿到Sqlite数据库文件,下载
WWW www = new WWW(androidPath);
//下载未完成时,保持等待
while(!www.isDone){}
//下载完成,IO流写入到沙盒路径
File.WriteAllBytes(androidFilePath, www.bytes);
}
return path;
}
/// <summary>
/// The .打开数据库连接
/// </summary>
private void OpenConnection(){
if(conn!=null){
try{
conn.Open ();
}catch(System.Exception e){
Debug.Log ("数据库打开失败...");
Debug.LogError (e.Message);
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void CloseConnection(){
if(conn!=null){
try {
conn.Close();
} catch (System.Exception ex) {
Debug.Log ("数据库连接关闭失败...");
Debug.LogError (ex.Message);
}
}
}
/// <summary>
/// 获取一行数据的方法
/// </summary>
/// <returns>The row data.</returns>
#region 对外接口方法
public Dictionary<string ,object> GetRowData(string sql){
OpenConnection ();
command = conn.CreateCommand ();
command.CommandText = sql;
SqliteDataReader reader= command.ExecuteReader ();
Dictionary <string,object> data = new Dictionary<string, object> ();
if(reader.Read()){
for (int i = 0; i < reader.FieldCount; i++) {
data.Add( reader.GetName (i),reader.GetValue(i)) ;
}
}
reader.Close ();
reader = null;
command.Dispose ();
command = null;
CloseConnection ();
return data;
}
#endregion
/// <summary>
/// 获取一张表的方法
/// </summary>
/// <returns>The all row data.</returns>
/// <param name="sql">Sql.</param>
public List<Dictionary<string,object>> GetAllRowData(string sql){
OpenConnection ();
command = conn.CreateCommand ();
command.CommandText = sql;
SqliteDataReader reader = command.ExecuteReader ();
List<Dictionary<string,object>> allData = new List<Dictionary<string, object>> ();
while (reader.Read()) {
Dictionary<string,object> rowData = new Dictionary<string, object> ();
for (int i = 0; i < reader.FieldCount; i++) {
rowData.Add (reader.GetName(i),reader.GetValue(i));
}
allData.Add (rowData);
}
reader.Close ();
reader = null;
command.Dispose ();
command = null;
CloseConnection ();
return allData;
}
}
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Mono.Data.Sqlite;
public class DataBaseTool : Singleton<DataBaseTool> {
public string DataBaseName="LOLData.sqlite";
private SqliteConnection conn;
private SqliteCommand command;
private SqliteDataReader reader;
private string datapath="";
public DataBaseTool(){
if(conn==null){
datapath= "Data Source=" + Application.streamingAssetsPath + "/" + DataBaseName;
#if UNITY_ANDRIOD
datapath=AndriodPaltformSet();
#endif
try{
conn = new SqliteConnection (datapath);
}catch(System.Exception e){
Debug.Log ("创建数据库连接失败...");
Debug.LogError (e.Message);
}
}
}
//安卓平台数据库设置
public string AndriodPaltformSet(){
string path = "URI = file:" + Application.persistentDataPath + "/" + DataBaseName;
//Android APK中数据库文件的路径
string androidPath = "jar:file://" + Application.dataPath + "!/assets/" + DataBaseName;
//Android沙盒路径
string androidFilePath = Application.persistentDataPath + "/" + DataBaseName;
//如果Android项目源文件中不存在数据库文件,说明没有加载过,需要加载
if(!File.Exists(androidFilePath))
{
//从APK路径拿到Sqlite数据库文件,下载
WWW www = new WWW(androidPath);
//下载未完成时,保持等待
while(!www.isDone){}
//下载完成,IO流写入到沙盒路径
File.WriteAllBytes(androidFilePath, www.bytes);
}
return path;
}
/// <summary>
/// The .打开数据库连接
/// </summary>
private void OpenConnection(){
if(conn!=null){
try{
conn.Open ();
}catch(System.Exception e){
Debug.Log ("数据库打开失败...");
Debug.LogError (e.Message);
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
private void CloseConnection(){
if(conn!=null){
try {
conn.Close();
} catch (System.Exception ex) {
Debug.Log ("数据库连接关闭失败...");
Debug.LogError (ex.Message);
}
}
}
/// <summary>
/// 获取一行数据的方法
/// </summary>
/// <returns>The row data.</returns>
#region 对外接口方法
public Dictionary<string ,object> GetRowData(string sql){
OpenConnection ();
command = conn.CreateCommand ();
command.CommandText = sql;
SqliteDataReader reader= command.ExecuteReader ();
Dictionary <string,object> data = new Dictionary<string, object> ();
if(reader.Read()){
for (int i = 0; i < reader.FieldCount; i++) {
data.Add( reader.GetName (i),reader.GetValue(i)) ;
}
}
reader.Close ();
reader = null;
command.Dispose ();
command = null;
CloseConnection ();
return data;
}
#endregion
/// <summary>
/// 获取一张表的方法
/// </summary>
/// <returns>The all row data.</returns>
/// <param name="sql">Sql.</param>
public List<Dictionary<string,object>> GetAllRowData(string sql){
OpenConnection ();
command = conn.CreateCommand ();
command.CommandText = sql;
SqliteDataReader reader = command.ExecuteReader ();
List<Dictionary<string,object>> allData = new List<Dictionary<string, object>> ();
while (reader.Read()) {
Dictionary<string,object> rowData = new Dictionary<string, object> ();
for (int i = 0; i < reader.FieldCount; i++) {
rowData.Add (reader.GetName(i),reader.GetValue(i));
}
allData.Add (rowData);
}
reader.Close ();
reader = null;
command.Dispose ();
command = null;
CloseConnection ();
return allData;
}
}
阅读全文
0 0
- Unity数据库的学习笔记
- unity学习笔记--unity的一些学习资料推荐
- Unity3D 学习笔记(四) Unity连接MySQL数据库
- Unity学习笔记5 unity的js转c#
- Unity学习笔记(2) --- Unity脚本的生命周期
- Unity学习笔记(3) --- Unity的界面排版:初识GUILayout
- Unity学习笔记(4) --- Unity的界面排版:初识GUI
- Unity学习笔记(4) --- Unity的界面排版: RectTransform
- 【unity学习笔记】【新手】unity的两种旋转命令
- unity加载dll库的学习笔记
- Unity学习笔记_UGUI的介绍
- Unity Shader学习笔记:简单的shader
- 数据库的学习笔记
- unity学习笔记
- UNity学习笔记
- unity学习笔记
- Unity学习笔记
- unity学习笔记2
- NUCLEO-L496ZG防火墙
- 视频直播前端方案
- ZOJ 1067
- BZOJ 1951: [Sdoi2010]古代猪文
- TensorFlow 1.0 新增功能和部分改善
- Unity数据库的学习笔记
- 阿里云ECS服务器部署JavaWeb教程
- thread26
- 手机用户意向统计程序设计(C/C++)
- 基于高斯模型的彩色图像反向投影
- 吾生也有涯
- ZOJ 1078
- java面试的一些问题(北京)
- 如何轻松记住HTML表格中的rolspan和cowspan