代码改变世界

unity3d连接Sqlite并打包发布Android

2015-07-18 14:46  糯米粥  阅读(2298)  评论(0编辑  收藏  举报

连接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 initialization
16     void Start()
17     {
18         Debug.Log(Application.dataPath);
19         try
20         {
21             /*1:Application.persistentDataPath用法:
22              * PC端,Android端通用
23             //persistentDataPath 表示持久化数据,
24              * 
25              * Android路径:/data/data/com.company.productnameyy/files/file5.db
26              * 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,只能发布PC
37             */
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,可以发布PC
46              * 必须在工程目录下已经有该文件夹(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 frame
83     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 initialization
15     void Start()
16     {
17         path = Application.persistentDataPath + @"/file9515.db";
18         //source = @"Data source=" + path; //PC
19 
20         source = "URI=file:" + path;  //
21     }
22 
23     // Update is called once per frame
24     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                 try
37                 {
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 }

 

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