连接Sqlite首先要把dll程序集导入到unity3d工程里面。安装好的unity中可以找到

其实发布PC端有这个就可以了。但如果是发布android的话。则需要这些。

在工程中创建一个文件夹,Plugins,Plugins文件夹中创建一个Android文件夹

Android文件夹放一个so文件libsqlite3,Plugins文件夹放一些sqlite需要的dll文件,如:

Plugins文件下载:http://pan.baidu.com/s/1sjxDzkD

因为Anidoid的文件夹是不固定的。所以数据库和表需要动态创建,

我这来写了一个测试的代码。来创建库,写入数据和读取数据

创建表代码:

 1 using UnityEngine; 2 using System.Collections; 3 using Mono.Data.Sqlite; 4 using System.IO; 5 public class createTable : MonoBehaviour 6 { 7     string path; 8     string source; 9 10     SqliteConnection connect;11     SqliteCommand command;12 13     string error;14 15     // Use this for initialization16     void Start()17     {18         Debug.Log(Application.dataPath);19         try20         {21             /*1:Application.persistentDataPath用法:22              * PC端,Android端通用23             //persistentDataPath 表示持久化数据,24              * 25              * Android路径:/data/data/com.company.productnameyy/files/file5.db26              * pc路径:C:/User/user/AppData/LocalLow/SqiteDemo/flie9.db(ty是发布的时候设置的公司名字,SqiteDemo是工程名)27              * 必须andorid注意:Plugins文件下必须有一个Android文件夹,里面有一个libsqlite3.so文件。这个libsqlite3.so文件是干什么的呢?28             */29             path = Application.persistentDataPath + @"/file9515.db";30             source = @"Data source=" + path; //发布android和pc都可以这样写31             //source = "URI=file:" + path;  //发布android和pc都可以这样写32 33 34             /*35              *2:Application.dataPath:获取当前工程的目录(D:/project/SqiteDemo/Assets)36              Application.dataPath 不能发布Android,只能发布PC37             */38             //path = Application.dataPath + @"/file121.db";39             //source = @"Data source=" + path; //PC可以这样写40             //source = "URI=file:" + path;  //PC也可以这样写41 42 43             /*44              3:Application.streamingAssetsPath:获取当前工程的目录(D:/project/SqiteDemo/Asseets/StreamingAssets)45              * Application.streamingAssetsPath不能发布Android,可以发布PC46              * 必须在工程目录下已经有该文件夹(StreamingAssets)47              * 发布的时候工程里面streamingAssetsPath里面的文件会一起打包发布48              */49             //path = Application.streamingAssetsPath + @"/file5611.db";50             //source = @"Data source=" + path;//PC也可以这样写51             //source = "URI=file:" + path;  //PC也可以这样写52 53 54             //数据库不存在,创建55             if (!File.Exists(path))56             {57                 //可以手动创建数据库58                 //SqliteConnection.CreateFile(path);59 60                 //连接的时候。在没有数据库的时候,会自动创建61                 connect = new SqliteConnection(source);62                 connect.Open();63 64                 //创建表65                 string sql = "create table hero(name nvarchar(30))";66                 command = new SqliteCommand(sql, connect);67                 command.ExecuteNonQuery();68             }69         }70         catch (System.Exception ex)71         {72             error = ex.Message;73         }74     }75 76     void OnGUI()77     {78         GUILayout.Label(error);79         GUILayout.Label(path);80     }81 82     // Update is called once per frame83     void Update()84     {85 86     }87 }

插入数据和读取数据代码

 1 using UnityEngine; 2 using System.Collections; 3 using Mono.Data.Sqlite; 4 using UnityEngine.UI; 5 public class AddDate : MonoBehaviour 6 { 7  8     public InputField input; 9     string path;10     string source;11 12     string msg;13 14     // Use this for initialization15     void Start()16     {17         path = Application.persistentDataPath + @"/file9515.db";18         //source = @"Data source=" + path; //PC19 20         source = "URI=file:" + path;  //21     }22 23     // Update is called once per frame24     void Update()25     {26 27     }28 29     public void Add(bool i)30     {31         if (i) //插入数据,其实每次插入也需要判断表或者库是否存在32         {33             string text = input.text;34             if (!string.IsNullOrEmpty(text))35             {36                 try37                 {38                     SqliteConnection conn = new SqliteConnection(source);39                     conn.Open();40                     SqliteCommand comm = new SqliteCommand("insert into hero(name)values('" + text + "')", conn);41                     comm.ExecuteNonQuery();42                 }43                 catch (System.Exception ex)44                 {45                     //msg = ex.Message + "~";46                 }47             }48         }49         else //读取数据,50         {51             SqliteConnection conn = new SqliteConnection(source);52             conn.Open();53             SqliteCommand comm = new SqliteCommand("select * from hero", conn);54 55             SqliteDataReader read = comm.ExecuteReader();56 57             msg = string.Empty;58 59             while (read.Read())60             {61                 msg += read["name"] + "~";62             }63         }64     }65 66     void OnGUI()67     {68         GUILayout.Label(msg);69     }70 }

主要代码就是这些。大家可以自己打包测试下。

更多相关文章

  1. android的一些开源项目
  2. Android(安卓)Adapter详解(1)
  3. Android学习小结
  4. 预防Android内存泄露
  5. android 开发布局之RelativeLayout
  6. Android常用动画alpha和rotate同时使用
  7. Android(安卓)NDK工程创建与编译运行
  8. Android实现透明式状态栏
  9. Hierarchy Viewer

随机推荐

  1. android MapView 定位与Overlay onTap事
  2. [Android问答] 开发环境问题集锦
  3. Android(安卓)studio中Git的学习和使用心
  4. Java转Android开发不得不知的一些经验
  5. 【Android】Android的消息机制
  6. Linux/Android——Input系统之InputMappe
  7. Android(安卓)NDK下编译google protocol
  8. Android之Adapter用法总结
  9. Android圆形控件
  10. Android之NDK开发