usingUnityEngine;
usingSystem.Collections;
usingMono.Data.Sqlite;//Mono.Data.SQLiteClient.dll;
public class SqliteManager{
//定义数据库连接
SqliteConnection dbconnection;
//定义SQL命令
SqliteCommand dbcommand;
//定义数据读取对象
SqliteDataReader reader;
//构造函数(自定义初始化方法)
public SqliteManager(string dbPath){
#ifUNITY_EDITOR
string connectionString="DataSource="+dbPath;
#endif
try{
//创建数据库连接对象
dbconnection=new SqliteConnection(connectionString);
//打开数据库
dbconnection.Open();
}catch(SqliteException ex){
Debug.Log(ex.Message);
}
}
//执行SQL命令
public SqliteDataReader ExecuteQuery(stringqueryString){
dbcommand=dbconnection.CreateCommand();
dbcommand.CommandText=queryString;
reader=dbcommand.ExecuteReader();
return reader;
}
//关闭数据库连接
public void CloseConnection( ){
//销毁Command
if(dbcommand!=null){
dbcommand.Dispose();
//dbcommand.Cancel();
}
dbcommand=null;
//销毁Reader
if(reader!=null){
reader.Close();
}
reader=null;
//销毁Connection
if(dbconnection!=null){
dbconnection.Close();
}
dbconnection=null;
}
//读取整张表
public SqliteDataReader ReadFullTable(string tableName){
stringquery="SELECT*FROM"+tableName;
returnExecuteQuery(query);
}
//插入数据
public SqliteDataReader InsertValues(string tableName,string[]values){
//获取数据表中的列数
int fieldCount=ReadFullTable(tableName).FieldCount;
//当前要插入的值的长度是否等于数据表中的列数
if(values.Length!=fieldCount){
//returnnull;
//抛出异常
thrownewSqliteException("values.Length!=fieldCount");
}
stringqueryString="INSERTINTO"+tableName+"VALUES("+values[0];
for(inti=1;i
queryString+=","+values[i];
}
queryString+=")";
Debug.Log(queryString);
returnExecuteQuery(queryString);
}
public SqliteDataReader InsertValues(string tableName,string[]values,string[]colNames){
if(values.Length!=colNames.Length){
//returnnull;
//抛出异常
thrownewSqliteException("values.Length!=colNames.Length");
}
stringqueryString="INSERTINTO"+tableName+"("+colNames[0];
for(inti=1;i
queryString+=","+colNames[i];
}
queryString+=")"+"VALUES"+"("+values[0];
for(inti=1;i
queryString+=","+values[i];
}
queryString+=")";
returnExecuteQuery(queryString);
}
//更新指定数据""
//UPDATE"UserTable"SET"passwoed"='5666589'WHERErowid=2;
publicSqliteDataReaderUpdateValues(stringtableName,string[]colNames,string[]values,stringkey,stringoperation,stringvalue){
//当列名个数和值个数不一致时,抛出异常
if(colNames.Length!=values.Length){
thrownewSqliteException("colNames.Length!=values.Length");
}
stringqueryString="UPDATE"+tableName+"SET"+colNames[0]+"="+values[0];
for(inti=1;i
queryString+=","+colNames[i]+"="+values[i];
}
queryString+="WHERE"+key+operation+value;
returnExecuteQuery(queryString);
}
//删除指定数据
//DELETEFROM"UserTable"WHEREname='xiaoli'ANDage>25;
publicSqliteDataReaderDeleteValueAND(stringtableName,string[]colNames,string[]operations,string[]values){
if(colNames.Length!=values.Length||operations.Length!=colNames.Length||operations.Length!=values.Length){
thrownewSqliteException("colNames.Length!=values.Length");
}
stringqueryString="DELETEFROM"+tableName+"WHERE"+colNames[0]+operations[0]+values[0];
for(inti=1;i
queryString+="AND"+colNames[i]+operations[i]+values[i];
}
returnExecuteQuery(queryString);
}
publicSqliteDataReaderDeleteValueOR(stringtableName,string[]colNames,string[]operations,string[]values){
if(colNames.Length!=values.Length||operations.Length!=colNames.Length||operations.Length!=values.Length){
thrownewSqliteException("colNames.Length!=values.Length");
}
stringqueryString="DELETEFROM"+tableName+"WHERE"+colNames[0]+operations[0]+values[0];
for(inti=1;i
queryString+="OR"+colNames[i]+operations[i]+values[i];
}
returnExecuteQuery(queryString);
}
//查找数据
publicSqliteDataReaderSelectTable(stringtableName,string[]colNames,string[]keys,string[]operations,string[]values){
if(keys.Length!=values.Length||operations.Length!=keys.Length||operations.Length!=values.Length){
thrownewSqliteException("keys.Length!=values.Length");
}
stringqueryString="SELECT"+colNames[0];
for(inti=1;i
queryString+=","+colNames[i];
}
queryString+="FROM"+tableName+"WHERE"+keys[0]+operations[0]+values[0];
for(inti=1;i
queryString+="AND"+keys[i]+operations[i]+values[i];
}
returnExecuteQuery(queryString);
}
//创建表
publicSqliteDataReaderCreateTable(stringtableName,string[]colNames,string[]colTypes){
if(colNames.Length!=colTypes.Length){
thrownewSqliteException("colName.Length!=colTypes.Length");
}
stringqueryString="CREATETABLE"+tableName+"("+colNames[0]+""+colTypes[0];
for(inti=1;i
queryString+=","+colNames[i]+""+colTypes[i];
}
queryString+=")";
returnExecuteQuery(queryString);
}
}
使用封装好的数据库类
usingUnityEngine;
usingSystem.Collections;
usingMono.Data.Sqlite;
public classTextScript:MonoBehaviour{
SqliteManager sqliteManager;
voidStart( ){
string dbPath=Application.streamingAssetsPath+"/LOLHeroInfo_1.sqlite";
sqliteManager=new SqliteManager(dbPath);
sqliteManager.CreateTable("MonsterInfo",newstring[]{"name","AP","AD","Level"},newstring[]{"TEXT","INTEGER","INTEGER","INTEGER"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'魂锁典狱长'","23","87","6"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'德邦总管'","10","100","10"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'炼金术士'","10","100","10"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'沙漠死神'","10","100","10"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'蛮族之王'","10","100","15"});
sqliteManager.UpdateValues("MonsterInfo",newstring[]{"name","AD"},newstring[]{"'努努'","500"},"name","=","'锤石'");
sqliteManager.DeleteValueAND("MonsterInfo",newstring[]{"Level"},newstring[]{">"},newstring[]{"11"});
SqliteDataReaderreader=sqliteManager.ExecuteQuery("SELECT*FROMMonsterInfo");
while(reader.Read()){
//读取name
print(reader.GetString(reader.GetOrdinal("name")));
}
sqliteManager.CloseConnection();
}
}