环境介绍:
Windows7,Unity3D,sqlite Expert Personal 3
开发语言:
JavaScript
需要的dll文件:
Mono.Data.sqlite.dll和sqlite3.dll,dll文件位置,截图:
一定要在这个目录下,请保持一致。
如果需要将编译好的程序发布成功的话,需要改一些地方,具体见下面的截图:
要改动的地方已用红色标记,注意这个要改成.NET2.0,这样才能够发布的。系统默认的不是.NET2.0,这一点要注意!!!
下面来看下代码吧,先看下如何创建数据库的代码,这一篇代码是不用挂到任何对象上面去的,你只用把它当成一个工具即可。如下所示:
- /*Javascriptclassforaccessingsqliteobjects.@H_404_38@
- Touseit,youneedtomakesureyouCOPYMono.Data.sqliteClient.dllfromwhereveritlivesinyourUnitydirectory@H_404_38@
- toyourproject'sAssetsfolder@H_404_38@
- Originallycreatedbydklompmakerin2009@H_404_38@
- http://forum.unity3d.com/threads...sier-Database-Stuff@H_404_38@
- Modified2011byAlanChatham*/@H_404_38@
- //#pragmastrict@H_404_38@
- /*@H_404_38@
代码描述
*本代码是为了在Windows环境下运行unity3d和sqlite数据库而写的;实现的基本功能是unity3d能够与数据库之间进行基本的通信,比如说:在数据库中的数据被改变了以后,unity3d中得到的数据也会在刷新了之后跟着改变;这只是一个基本的核心的技术,为的是能够应用在大型的unity3d项目中,能够存储场景中的项目的属性,在需要改变对象的属性或增加、减少等对象时能够很方便的用得上。要实现本代码。首先需要一些dll文件,一个是Mono.Data.sqliteClient.dll,另外一个是sqlite3.dll,这些文件都能够在unity3d的安装目录中找得到。除此之外,还需要把这两个文件放在你的项目的这个路径下面:\Assets\Plugins\,没有Plugins文件夹就必须创建这个文件夹,然后将这两个dll文件放在该文件夹写。当然,如果你想能够在PC上面发布成可执行文件,还需要改动一些地方。在unity3d中的Play Setting ->Other Setting 中将Api Compatibility的等级改为.NET 2.0;那么这些操作做完了以后,如果你的代码写得没有问题,那么你就可以成功了。
细解释代码:
- *@H_404_38@
- */@H_404_38@
- importSystem.Data;//weimportourdataclass我们先导入我们的数据集@H_404_38@
- importMono.Data.sqlite;//weimportsqlite我们导入sqlite数据集,也就是Plugins文件夹下的那个dll文件@H_404_38@
- classdbAccess{@H_404_38@
- //variablesforbasicqueryaccess@H_404_38@
- privatevarconnection:String;//数据库的连接字符串,用于建立与特定数据源的连接@H_404_38@
- privatevardbcon:IDbConnection;//IDbConnection的连接对象,其实就是一个类对象@H_404_38@
- privatevardbcmd:IDbCommand;//IDbCommand类对象,用来实现操作数据库的命令:注解:我在网上资料看到的如何实现对数据库执行命令:@H_404_38@
- //首先创建一个IDbConnection连接对象,然后将一条数据库命令赋值给一个字符串,利用这个字符串和连接对象@H_404_38@
- //就可以创建(new)一个IDbCommand对象了,然后使用提供的方法就可以执行这个命令了。@H_404_38@
- privatevarreader:IDataReader;//reader的作用就是读取结果集的一个或多个只进结果流@H_404_38@
- functionOpenDB(p:String){@H_404_38@
- connection="URI=file:"+p;//wesettheconnectiontoourdatabase@H_404_38@
- dbcon=newsqliteConnection(connection);@H_404_38@
- dbcon.Open();//打开数据库连接操作@H_404_38@
- }@H_404_38@
- functionBasicQuery(q:String,r:boolean){//runabaicsqlitequery@H_404_38@
- dbcmd=dbcon.CreateCommand();//createemptycommand@H_404_38@
- dbcmd.CommandText=q;//fillthecommand@H_404_38@
- reader=dbcmd.ExecuteReader();//executecommandwhichreturnsareader返回IDataReader的对象,创建IDataReader的对象@H_404_38@
- if(r){//ifwewanttoreturnthereader@H_404_38@
- returnreader;//returnthereader返回读取的对象,就是读到了什么东西@H_404_38@
- }@H_404_38@
- }@H_404_38@
- //Thisreturnsa2dimensionalArrayListwithallthe@H_404_38@
- //datafromthetablerequested@H_404_38@
- functionReadFullTable(tableName:String){@H_404_38@
- varquery:String;@H_404_38@
- query="SELECT*FROM"+tableName;@H_404_38@
- dbcmd=dbcon.CreateCommand();@H_404_38@
- dbcmd.CommandText=query;@H_404_38@
- reader=dbcmd.ExecuteReader();@H_404_38@
- varreadArray=newArrayList();@H_404_38@
- while(reader.Read()){@H_404_38@
- varlineArray=newArrayList();@H_404_38@
- for(vari=0;i<reader.FieldCount;i++)@H_404_38@
- lineArray.Add(reader.GetValue(i));//Thisreadstheentriesinarow@H_404_38@
- readArray.Add(lineArray);//Thismakesanarrayofalltherows@H_404_38@
- }@H_404_38@
- returnreadArray;//returnmatches@H_404_38@
- }@H_404_38@
- //Thisfunctiondeletesallthedatainthegiventable.Forever.WATCHOUT!Usesparingly,ifatall@H_404_38@
- functionDeleteTableContents(tableName:String){@H_404_38@
- varquery:String;@H_404_38@
- query="DELETEFROM"+tableName;@H_404_38@
- dbcmd=dbcon.CreateCommand();@H_404_38@
- dbcmd.CommandText=query;@H_404_38@
- reader=dbcmd.ExecuteReader();@H_404_38@
- }@H_404_38@
- functionCreateTable(name:String,col:Array,colType:Array){//Createatable,name,columnarray,columntypearray@H_404_38@
- varquery:String;@H_404_38@
- query="CREATETABLE"+name+"("+col[0]+""+colType[0];@H_404_38@
- for(vari=1;i<col.length;i++){@H_404_38@
- query+=","+col+""+colType;@H_404_38@
- }@H_404_38@
- query+=")";@H_404_38@
- dbcmd=dbcon.CreateCommand();//createemptycommand@H_404_38@
- dbcmd.CommandText=query;//fillthecommand@H_404_38@
- reader=dbcmd.ExecuteReader();//executecommandwhichreturnsareader@H_404_38@
- }@H_404_38@
- functionInsertIntoSingle(tableName:String,colName:String,value:String){//singleinsert@H_404_38@
- varquery:String;@H_404_38@
- query="INSERTINTO"+tableName+"("+colName+")"+"VALUES("+value+")";@H_404_38@
- dbcmd=dbcon.CreateCommand();//createemptycommand@H_404_38@
- dbcmd.CommandText=query;//fillthecommand@H_404_38@
- reader=dbcmd.ExecuteReader();//executecommandwhichreturnsareader@H_404_38@
- }@H_404_38@
- functionInsertIntoSpecific(tableName:String,values:Array){//Specificinsertwithcolandvalues@H_404_38@
- varquery:String;@H_404_38@
- query="INSERTINTO"+tableName+"("+col[0];@H_404_38@
- for(vari=1;i<col.length;i++){@H_404_38@
- query+=","+col;@H_404_38@
- }@H_404_38@
- query+=")VALUES("+values[0];@H_404_38@
- for(i=1;i<values.length;i++){@H_404_38@
- query+=","+values;@H_404_38@
- }@H_404_38@
- query+=")";@H_404_38@
- dbcmd=dbcon.CreateCommand();@H_404_38@
- dbcmd.CommandText=query;@H_404_38@
- reader=dbcmd.ExecuteReader();@H_404_38@
- }@H_404_38@
- functionInsertInto(tableName:String,values:Array){//basicInsertwithjustvalues@H_404_38@
- varquery:String;@H_404_38@
- query="INSERTINTO"+tableName+"VALUES("+values[0];@H_404_38@
- for(vari=1;i<values.length;i++){@H_404_38@
- query+=","+values;@H_404_38@
- }@H_404_38@
- query+=")";@H_404_38@
- dbcmd=dbcon.CreateCommand();@H_404_38@
- dbcmd.CommandText=query;@H_404_38@
- reader=dbcmd.ExecuteReader();@H_404_38@
- }@H_404_38@
- //Thisfunctionreadsasinglecolumn@H_404_38@
- //wColistheWHEREcolumn,wParistheoperatoryouwanttousetocomparewith,@H_404_38@
- //andwValueisthevalueyouwanttocompareagainst.@H_404_38@
- //Ex.-SingleSelectWhere("puppies","breed","earType","=","floppy")@H_404_38@
- //returnsanarrayofmatchesfromthecommand:SELECTbreedFROMpuppiesWHEREearType=floppy;@H_404_38@
- functionSingleSelectWhere(tableName:String,itemToSelect:String,wCol:String,wPar:String,wValue:String){//SelectsasingleItem@H_404_38@
- varquery:String;@H_404_38@
- query="SELECT"+itemToSelect+"FROM"+tableName+"WHERE"+wCol+wPar+wValue;@H_404_38@
- dbcmd=dbcon.CreateCommand();@H_404_38@
- dbcmd.CommandText=query;@H_404_38@
- reader=dbcmd.ExecuteReader();@H_404_38@
- varreadArray=newArray();@H_404_38@
- while(reader.Read()){@H_404_38@
- readArray.Push(reader.GetString(0));//Fillarraywithallmatches@H_404_38@
- }@H_404_38@
- returnreadArray;//returnmatches@H_404_38@
- }@H_404_38@
- functionCloseDB(){@H_404_38@
- reader.Close();//cleaneverythingup@H_404_38@
- reader=null;@H_404_38@
- dbcmd.Dispose();@H_404_38@
- dbcmd=null;@H_404_38@
- dbcon.Close();@H_404_38@
- dbcon=null;@H_404_38@
- }@H_404_38@
- }@H_404_38@
- //#pragmastrict@H_404_38@
- /*ScriptfortestingoutsqliteinJavascript@H_404_38@
- 2011-AlanChatham@H_404_38@
- Releasedintothepublicdomain@H_404_38@
- ThisscriptisaGUIscript-attachittoyourmaincamera.@H_404_38@
- @H_404_38@
- Itcreates/opensasqlitedatabase,andwiththeGUIyoucanreadandwritetoit.@H_404_38@
- @H_404_38@
- */@H_404_38@
- @H_404_38@
- //Thisisthefilepathofthedatabasefilewewanttouse@H_404_38@
- @H_404_38@
- //Rightnow,it'llloadTestDB.sqdbintheproject'srootfolder.@H_404_38@
- @H_404_38@
- //Ifonedoesn'texist,itwillbeautomaticallycreated.@H_404_38@
- @H_404_38@
- publicvarDatabaseName:String="TestDB.sqdb";@H_404_38@
- @H_404_38@
- //Thisisthenameofthetablewewanttouse@H_404_38@
- @H_404_38@
- publicvarTableName:String="TestTable";@H_404_38@
- @H_404_38@
- vardb:dbAccess;@H_404_38@
- @H_404_38@
- functionStart(){@H_404_38@
- @H_404_38@
- //GiveourselvesadbAccessobjecttoworkwith,andopenit@H_404_38@
- @H_404_38@
- db=newdbAccess();@H_404_38@
- @H_404_38@
- db.OpenDB(DatabaseName);@H_404_38@
- @H_404_38@
- //Let'smakesurewe'vegotatabletoworkwithaswell!@H_404_38@
- @H_404_38@
- vartableName=TableName;@H_404_38@
- @H_404_38@
- varcolumnNames=newArray("firstName","lastName");@H_404_38@
- @H_404_38@
- varcolumnValues=newArray("text","text");@H_404_38@
- @H_404_38@
- try{db.CreateTable(tableName,columnNames,columnValues);@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- catch(e){//Donothing-ourtablewasalreadycreated判断表是否被创建了@H_404_38@
- @H_404_38@
- //-wedon'tcareabouttheerror,wejustdon'twanttoseeit@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- //ThesevariablesjustholdinfotodisplayinourGUI@H_404_38@
- @H_404_38@
- varfirstName:String="FirstName";@H_404_38@
- @H_404_38@
- varlastName:String="LastName";@H_404_38@
- @H_404_38@
- varDatabaseEntryStringWidth=100;@H_404_38@
- @H_404_38@
- varscrollPosition:Vector2;@H_404_38@
- @H_404_38@
- vardatabaseData:ArrayList=newArrayList();@H_404_38@
- @H_404_38@
- //ThisGUIprovidesuswithawaytoenterdataintoourdatabase@H_404_38@
- @H_404_38@
- //aswellasawaytoviewit@H_404_38@
- @H_404_38@
- functionOnGUI(){@H_404_38@
- @H_404_38@
- GUI.Box(Rect(25,25,Screen.width-50,Screen.height-50),"Data");@H_404_38@
- @H_404_38@
- GUILayout.BeginArea(Rect(50,50,Screen.width-100,Screen.height-100));@H_404_38@
- @H_404_38@
- //Thisfirstblockallowsustoenternewentriesintoourtable@H_404_38@
- @H_404_38@
- GUILayout.BeginHorizontal();@H_404_38@
- @H_404_38@
- firstName=GUILayout.TextField(firstName,GUILayout.Width(DatabaseEntryStringWidth));@H_404_38@
- @H_404_38@
- lastName=GUILayout.TextField(lastName,GUILayout.Width(DatabaseEntryStringWidth));@H_404_38@
- @H_404_38@
- //lastName=GUILayout.TextField();@H_404_38@
- @H_404_38@
- GUILayout.EndHorizontal();@H_404_38@
- @H_404_38@
- if(GUILayout.Button("Addtodatabase")){@H_404_38@
- @H_404_38@
- //Insertthedata@H_404_38@
- @H_404_38@
- InsertRow(firstName,lastName);@H_404_38@
- @H_404_38@
- //Andupdatethereadoutofthedatabase@H_404_38@
- @H_404_38@
- databaseData=ReadFullTable();@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- //Thissecondblockgivesusabuttonthatwilldisplay/refreshthecontentsofourdatabase@H_404_38@
- @H_404_38@
- GUILayout.BeginHorizontal();@H_404_38@
- @H_404_38@
- if(GUILayout.Button("ReadDatabase"))@H_404_38@
- @H_404_38@
- databaseData=ReadFullTable();@H_404_38@
- @H_404_38@
- if(GUILayout.Button("Clear"))@H_404_38@
- @H_404_38@
- databaseData.Clear();@H_404_38@
- @H_404_38@
- GUILayout.EndHorizontal();@H_404_38@
- @H_404_38@
- GUILayout.Label("DatabaseContents");@H_404_38@
- @H_404_38@
- scrollPosition=GUILayout.BeginScrollView(scrollPosition,GUILayout.Height(100));@H_404_38@
- @H_404_38@
- for(varline:ArrayListindatabaseData){@H_404_38@
- @H_404_38@
- GUILayout.BeginHorizontal();@H_404_38@
- @H_404_38@
- for(varsinline){@H_404_38@
- @H_404_38@
- GUILayout.Label(s.ToString(),GUILayout.Width(DatabaseEntryStringWidth));@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- GUILayout.EndHorizontal();@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- GUILayout.EndScrollView();@H_404_38@
- @H_404_38@
- if(GUILayout.Button("DeleteAllData")){@H_404_38@
- @H_404_38@
- DeleteTableContents();@H_404_38@
- @H_404_38@
- databaseData=ReadFullTable();@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- GUILayout.EndArea();@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- //Wrapperfunctionforinsertingourspecificentriesintoourspecificdatabaseandtableforthisfile@H_404_38@
- @H_404_38@
- functionInsertRow(firstName,lastName){@H_404_38@
- @H_404_38@
- varvalues=newArray(("'"+firstName+"'"),("'"+lastName+"'"));@H_404_38@
- @H_404_38@
- db.InsertInto(TableName,values);@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- //Wrapperfunction,soweonlymesswithourtable.@H_404_38@
- @H_404_38@
- functionReadFullTable(){@H_404_38@
- @H_404_38@
- returndb.ReadFullTable(TableName);@H_404_38@
- @H_404_38@
- }@H_404_38@
- @H_404_38@
- //Anotherwrapperfunction...@H_404_38@
- @H_404_38@
- functionDeleteTableContents(){@H_404_38@
- @H_404_38@
- db.DeleteTableContents(TableName);@H_404_38@
- @H_404_38@
- }@H_404_38@
运行结果:
这是在Unity3D中运行的结果,数据的操作结果如下:
原文链接:http://bbs.9ria.com/forum.PHP?mod=viewthread&tid=168629&fromuid=308561