usingUnityEngine;
usingSystem.Collections;
usingMono.Data.Sqlite;//Mono.Data.SQLiteClient.dll;
public class SqliteManager{
//定義數(shù)據(jù)庫(kù)連接
SqliteConnection? dbconnection;
//定義SQL命令
SqliteCommand?? dbcommand;
//定義數(shù)據(jù)讀取對(duì)象
SqliteDataReader? reader;
//構(gòu)造函數(shù)(自定義初始化方法)
public? SqliteManager(string? dbPath){
#ifUNITY_EDITOR
string??? connectionString="DataSource="+dbPath;
#endif
try{
//創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象
dbconnection=new SqliteConnection(connectionString);
//打開(kāi)數(shù)據(jù)庫(kù)
dbconnection.Open();
}catch(SqliteException ex){
Debug.Log(ex.Message);
}
}
//執(zhí)行SQL命令
public? SqliteDataReader?? ExecuteQuery(stringqueryString){
dbcommand=dbconnection.CreateCommand();
dbcommand.CommandText=queryString;
reader=dbcommand.ExecuteReader();
return reader;
}
//關(guān)閉數(shù)據(jù)庫(kù)連接
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);
}
//插入數(shù)據(jù)
public SqliteDataReader InsertValues(string tableName,string[]values){
//獲取數(shù)據(jù)表中的列數(shù)
int fieldCount=ReadFullTable(tableName).FieldCount;
//當(dāng)前要插入的值的長(zhǎng)度是否等于數(shù)據(jù)表中的列數(shù)
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);
}
//更新指定數(shù)據(jù)""
//UPDATE"UserTable"SET"passwoed"='5666589'WHERErowid=2;
publicSqliteDataReaderUpdateValues(stringtableName,string[]colNames,string[]values,stringkey,stringoperation,stringvalue){
//當(dāng)列名個(gè)數(shù)和值個(gè)數(shù)不一致時(shí)飘哨,拋出異常
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);
}
//刪除指定數(shù)據(jù)
//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);
}
//查找數(shù)據(jù)
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);
}
//創(chuàng)建表
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);
}
}
使用封裝好的數(shù)據(jù)庫(kù)類
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[]{"'魂鎖典獄長(zhǎng)'","23","87","6"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'德邦總管'","10","100","10"});
sqliteManager.InsertValues("MonsterInfo",newstring[]{"'煉金術(shù)士'","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();
}
}