query+=","+values;
}
query+=")";
dbcmd=dbcon.CreateCommand();
dbcmd.CommandText=query;
reader=dbcmd.ExecuteReader();
}
//Thisfunctionreadsasinglecolumn
//wColistheWHEREcolumn,wParistheoperatoryouwanttousetocomparewith,
//andwValueisthevalueyouwanttocompareagainst.
//Ex.-SingleSelectWhere("puppies","breed","earType","=","floppy")
//returnsanarrayofmatchesfromthecommand:
SELECTbreedFROMpuppiesWHEREearType=floppy;
functionSingleSelectWhere(tableName:
String,itemToSelect:
String,wCol:
String,wPar:
String,wValue:
String){//SelectsasingleItem
varquery:
String;
query="SELECT"+itemToSelect+"FROM"+tableName+"WHERE"+wCol+wPar+wValue;
dbcmd=dbcon.CreateCommand();
dbcmd.CommandText=query;
reader=dbcmd.ExecuteReader();
varreadArray=newArray();
while(reader.Read()){
readArray.Push(reader.GetString(0));//Fillarraywithallmatches
}
returnreadArray;//returnmatches
}
functionCloseDB(){
reader.Close();//cleaneverythingup
reader=null;
dbcmd.Dispose();
dbcmd=null;
dbcon.Close();
dbcon=null;
}
}
7、如何在Unity3D中使用这个数据库的代码:
//#pragmastrict
/*ScriptfortestingoutSQLiteinJavascript
2011-AlanChatham
Releasedintothepublicdomain
ThisscriptisaGUIscript-attachittoyourmaincamera.
Itcreates/opensaSQLitedatabase,andwiththeGUIyoucanreadandwritetoit.
*/
//Thisisthefilepathofthedatabasefilewewanttouse
//Rightnow,it'llloadTestDB.sqdbintheproject'srootfolder.
//Ifonedoesn'texist,itwillbeautomaticallycreated.
publicvarDatabaseName:
String="TestDB.sqdb";
//Thisisthenameofthetablewewanttouse
publicvarTableName:
String="TestTable";
vardb:
dbAccess;
functionStart(){
//GiveourselvesadbAccessobjecttoworkwith,andopenit
db=newdbAccess();
db.OpenDB(DatabaseName);
//Let'smakesurewe'vegotatabletoworkwithaswell!
vartableName=TableName;
varcolumnNames=newArray("firstName","lastName");
varcolumnValues=newArray("text","text");
try{db.CreateTable(tableName,columnNames,columnValues);
}
catch(e){//Donothing-ourtablewasalreadycreated判断表是否被创建了
//-wedon'tcareabouttheerror,wejustdon'twanttoseeit
}
}
//ThesevariablesjustholdinfotodisplayinourGUI
varfirstName:
String="FirstName";
varlastName:
String="LastName";
varDatabaseEntryStringWidth=100;
varscrollPosition:
Vector2;
vardatabaseData:
ArrayList=newArrayList();
//ThisGUIprovidesuswithawaytoenterdataintoourdatabase
//aswellasawaytoviewit
functionOnGUI(){
GUI.Box(Rect(25,25,Screen.width-50,Screen.height-50),"Data");
GUILayout.BeginArea(Rect(50,50,Screen.width-100,Screen.height-100));
//Thisfirstblockallowsustoenternewentriesintoourtable
GUILayout.BeginHorizontal();
firstName=GUILayout.TextField(firstName,GUILayout.Width(DatabaseEntryStringWidth));
lastName=GUILayout.TextField(lastName,GUILayout.Width(DatabaseEntryStringWidth));
//lastName=GUILayout.TextField();
GUILayout.EndHorizontal();
if(GUILayout.Button("Addtodatabase")){
//Insertthedata
InsertRow(firstName,lastName);
//Andupdatethereadoutofthedatabase
databaseData=ReadFullTable();
}
//Thissecondblockgivesusabuttonthatwilldisplay/refreshthecontentsofourdatabase
GUILayout.BeginHorizontal();
if(GUILayout.Button("ReadDatabase"))
databaseData=ReadFullTable();
if(GUILayout.Button("Clear"))
databaseData.Clear();
GUILayout.EndHorizontal();
GUILayout.Label("DatabaseContents");
scrollPosition=GUILayout.BeginScrollView(scrollPosition,GUILayout.Height(100));
for(varline:
ArrayListindatabaseData){
GUILayout.BeginHorizontal();
for(varsinline){
GUILayout.Label(s.ToString(),GUILayout.Width(DatabaseEntryStringWidth));
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
if(GUILayout.Button("DeleteAllData")){
DeleteTableContents();
databaseData=ReadFullTable();
}
GUILayout.EndArea();
}
//Wrapperfunctionforinsertingourspecificentriesintoourspecificdatabaseandtableforthisfile
functionInsertRow(firstName,lastName){
varvalues=newArray(("'"+firstName+"'"),("'"+lastName+"'"));
db.InsertInto(TableName,values);
}
//Wrapperfunction,soweonlymesswithourtable.
functionReadFullTable(){
returndb.ReadFullTable(TableName);
}
//Anotherwrapperfunction...
functionDeleteTableContents(){
db.DeleteTableContents(TableName);
}
运行结果:
Unity3D教程:
Unity3D与Sqlite数据库直连
这是在Unity3D中运行的结果,数据的操作结果如下:
Unity3D教程:
Unity3D与Sqlite数据库直连
我们看见了数据的操作能够成功,经过测试,其他的Button也都能出现相对应的效果,那我们再看看这个到底有没有生成我们想要的数据库文件:
Unity3D教程:
Unity3D与Sqlite数据库直连
Unity3D教程:
Unity3D与Sqlite数据库直连
文件当中数据:
经测试,我们在对数据库中的数据进行操作的时候,我们的Unity3D中的数据也会发生相应的改变了!