unity5 操作sqlite3数据库(一) Android版本



声明:本文主要参考 雨松momo 的文章(十分感谢),但是在android设备上运行时,出现了不能操作数据库的问题;原文链接:http://www.xuanyusong.com/archives/831  http://www.xuanyusong.com/archives/1454

本篇文章我们讨论如何在Unity中打开一个第三方数据库配合Android与编辑器进行同步开发。

一. 怎么让手机使用sqlite3数据库

1. 需要的文件如下:

Mono.Data.Sqlite.dll
System.Data.dll
Mono.Data.dll
sqlite3.dll
libsqlite3.so

2.存放路径:

       在Assets目录下新建Plugins,把Mono.Data.dll、Mono.Data.Sqlite.dll、System.Data.dll、sqlite3.dll 放到这层目录下,然后在这层目录下建立Android,再将libsqlite3.so放到Android目录下,最后再在这个Android目录下创建assets目录,然后将.db文件放于此,我试过各种路径存放和试剥离不需要的dll,都不行。

     如下图所示,这个是我目前工程的结构:


dll下载地址: 链接:http://pan.baidu.com/s/1gefV8vp 密码:5p1b
在打包好的apk中可以看到db文件已经被压缩进去了;

二. c#接口的脚本

1. 注意:下面脚本不要绑定在任何游戏对象身上,大家无需把它当作脚本可以当作一个工具类来使用。

DbAccess.cs
[csharp] view plain copy print ?
  1. using UnityEngine;  
  2. using System;  
  3. using System.Collections;  
  4. using Mono.Data.Sqlite;  
  5.   
  6. public class DbAccess  
  7.   
  8. {  
  9.   
  10.     private SqliteConnection dbConnection;  
  11.   
  12.     private SqliteCommand dbCommand;  
  13.   
  14.     private SqliteDataReader reader;  
  15.   
  16.     public DbAccess(string connectionString)  
  17.   
  18.     {  
  19.   
  20.         OpenDB(connectionString);  
  21.   
  22.     }  
  23.     public DbAccess()  
  24.     {  
  25.   
  26.     }  
  27.   
  28.     public void OpenDB(string connectionString)  
  29.   
  30.     {  
  31.         try  
  32.         {  
  33.             dbConnection = new SqliteConnection(connectionString);  
  34.   
  35.             dbConnection.Open();  
  36.   
  37.             Debug.Log("Connected to db");  
  38.         }  
  39.         catch (Exception e)  
  40.         {  
  41.             string temp1 = e.ToString();  
  42.             Debug.Log(temp1);  
  43.         }  
  44.   
  45.     }  
  46.   
  47.     public void CloseSqlConnection()  
  48.   
  49.     {  
  50.   
  51.         if (dbCommand != null)  
  52.         {  
  53.   
  54.             dbCommand.Dispose();  
  55.   
  56.         }  
  57.   
  58.         dbCommand = null;  
  59.   
  60.         if (reader != null)  
  61.         {  
  62.   
  63.             reader.Dispose();  
  64.   
  65.         }  
  66.   
  67.         reader = null;  
  68.   
  69.         if (dbConnection != null)  
  70.         {  
  71.   
  72.             dbConnection.Close();  
  73.   
  74.         }  
  75.   
  76.         dbConnection = null;  
  77.   
  78.         Debug.Log("Disconnected from db.");  
  79.   
  80.     }  
  81.   
  82.     public SqliteDataReader ExecuteQuery(string sqlQuery)  
  83.   
  84.     {  
  85.   
  86.         dbCommand = dbConnection.CreateCommand();  
  87.   
  88.         dbCommand.CommandText = sqlQuery;  
  89.   
  90.         reader = dbCommand.ExecuteReader();  
  91.   
  92.         return reader;  
  93.   
  94.     }  
  95.   
  96.     public SqliteDataReader ReadFullTable(string tableName)  
  97.   
  98.     {  
  99.   
  100.         string query = "SELECT * FROM " + tableName;  
  101.   
  102.         return ExecuteQuery(query);  
  103.   
  104.     }  
  105.   
  106.     public SqliteDataReader InsertInto(string tableName, string[] values)  
  107.   
  108.     {  
  109.   
  110.         string query = "INSERT INTO " + tableName + " VALUES (" + values[0];  
  111.   
  112.         for (int i = 1; i < values.Length; ++i)  
  113.         {  
  114.   
  115.             query += ", " + values[i];  
  116.   
  117.         }  
  118.   
  119.         query += ")";  
  120.   
  121.         return ExecuteQuery(query);  
  122.   
  123.     }  
  124.   
  125.     public SqliteDataReader UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)  
  126.     {  
  127.   
  128.         string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];  
  129.   
  130.         for (int i = 1; i < colsvalues.Length; ++i)  
  131.         {  
  132.   
  133.             query += ", " + cols[i] + " =" + colsvalues[i];  
  134.         }  
  135.   
  136.         query += " WHERE " + selectkey + " = " + selectvalue + " ";  
  137.   
  138.         return ExecuteQuery(query);  
  139.     }  
  140.   
  141.     public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues)  
  142.     {  
  143.         string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];  
  144.   
  145.         for (int i = 1; i < colsvalues.Length; ++i)  
  146.         {  
  147.   
  148.             query += " or " + cols[i] + " = " + colsvalues[i];  
  149.         }  
  150.         Debug.Log(query);  
  151.         return ExecuteQuery(query);  
  152.     }  
  153.   
  154.     public SqliteDataReader InsertIntoSpecific(string tableName, string[] cols, string[] values)  
  155.   
  156.     {  
  157.   
  158.         if (cols.Length != values.Length)  
  159.         {  
  160.   
  161.             throw new SqliteException("columns.Length != values.Length");  
  162.   
  163.         }  
  164.   
  165.         string query = "INSERT INTO " + tableName + "(" + cols[0];  
  166.   
  167.         for (int i = 1; i < cols.Length; ++i)  
  168.         {  
  169.   
  170.             query += ", " + cols[i];  
  171.   
  172.         }  
  173.   
  174.         query += ") VALUES (" + values[0];  
  175.   
  176.         for (int i = 1; i < values.Length; ++i)  
  177.         {  
  178.   
  179.             query += ", " + values[i];  
  180.   
  181.         }  
  182.   
  183.         query += ")";  
  184.   
  185.         return ExecuteQuery(query);  
  186.   
  187.     }  
  188.   
  189.     public SqliteDataReader DeleteContents(string tableName)  
  190.   
  191.     {  
  192.   
  193.         string query = "DELETE FROM " + tableName;  
  194.   
  195.         return ExecuteQuery(query);  
  196.   
  197.     }  
  198.   
  199.     public SqliteDataReader CreateTable(string name, string[] col, string[] colType)  
  200.   
  201.     {  
  202.   
  203.         if (col.Length != colType.Length)  
  204.         {  
  205.   
  206.             throw new SqliteException("columns.Length != colType.Length");  
  207.   
  208.         }  
  209.   
  210.         string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];  
  211.   
  212.         for (int i = 1; i < col.Length; ++i)  
  213.         {  
  214.   
  215.             query += ", " + col[i] + " " + colType[i];  
  216.   
  217.         }  
  218.   
  219.         query += ")";  
  220.   
  221.         return ExecuteQuery(query);  
  222.   
  223.     }  
  224.   
  225.     public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)  
  226.   
  227.     {  
  228.   
  229.         if (col.Length != operation.Length || operation.Length != values.Length)  
  230.         {  
  231.   
  232.             throw new SqliteException("col.Length != operation.Length != values.Length");  
  233.   
  234.         }  
  235.   
  236.         string query = "SELECT " + items[0];  
  237.   
  238.         for (int i = 1; i < items.Length; ++i)  
  239.         {  
  240.   
  241.             query += ", " + items[i];  
  242.   
  243.         }  
  244.   
  245.         query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";  
  246.   
  247.         for (int i = 1; i < col.Length; ++i)  
  248.         {  
  249.   
  250.             query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";  
  251.   
  252.         }  
  253.   
  254.         return ExecuteQuery(query);  
  255.   
  256.     }  
  257.   
  258. }  
using UnityEngine;
using System;
using System.Collections;
using Mono.Data.Sqlite;

public class DbAccess

{

    private SqliteConnection dbConnection;

    private SqliteCommand dbCommand;

    private SqliteDataReader reader;

    public DbAccess(string connectionString)

    {

        OpenDB(connectionString);

    }
    public DbAccess()
    {

    }

    public void OpenDB(string connectionString)

    {
        try
        {
            dbConnection = new SqliteConnection(connectionString);

            dbConnection.Open();

            Debug.Log("Connected to db");
        }
        catch (Exception e)
        {
            string temp1 = e.ToString();
            Debug.Log(temp1);
        }

    }

    public void CloseSqlConnection()

    {

        if (dbCommand != null)
        {

            dbCommand.Dispose();

        }

        dbCommand = null;

        if (reader != null)
        {

            reader.Dispose();

        }

        reader = null;

        if (dbConnection != null)
        {

            dbConnection.Close();

        }

        dbConnection = null;

        Debug.Log("Disconnected from db.");

    }

    public SqliteDataReader ExecuteQuery(string sqlQuery)

    {

        dbCommand = dbConnection.CreateCommand();

        dbCommand.CommandText = sqlQuery;

        reader = dbCommand.ExecuteReader();

        return reader;

    }

    public SqliteDataReader ReadFullTable(string tableName)

    {

        string query = "SELECT * FROM " + tableName;

        return ExecuteQuery(query);

    }

    public SqliteDataReader InsertInto(string tableName, string[] values)

    {

        string query = "INSERT INTO " + tableName + " VALUES (" + values[0];

        for (int i = 1; i < values.Length; ++i)
        {

            query += ", " + values[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }

    public SqliteDataReader UpdateInto(string tableName, string[] cols, string[] colsvalues, string selectkey, string selectvalue)
    {

        string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];

        for (int i = 1; i < colsvalues.Length; ++i)
        {

            query += ", " + cols[i] + " =" + colsvalues[i];
        }

        query += " WHERE " + selectkey + " = " + selectvalue + " ";

        return ExecuteQuery(query);
    }

    public SqliteDataReader Delete(string tableName, string[] cols, string[] colsvalues)
    {
        string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];

        for (int i = 1; i < colsvalues.Length; ++i)
        {

            query += " or " + cols[i] + " = " + colsvalues[i];
        }
        Debug.Log(query);
        return ExecuteQuery(query);
    }

    public SqliteDataReader InsertIntoSpecific(string tableName, string[] cols, string[] values)

    {

        if (cols.Length != values.Length)
        {

            throw new SqliteException("columns.Length != values.Length");

        }

        string query = "INSERT INTO " + tableName + "(" + cols[0];

        for (int i = 1; i < cols.Length; ++i)
        {

            query += ", " + cols[i];

        }

        query += ") VALUES (" + values[0];

        for (int i = 1; i < values.Length; ++i)
        {

            query += ", " + values[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }

    public SqliteDataReader DeleteContents(string tableName)

    {

        string query = "DELETE FROM " + tableName;

        return ExecuteQuery(query);

    }

    public SqliteDataReader CreateTable(string name, string[] col, string[] colType)

    {

        if (col.Length != colType.Length)
        {

            throw new SqliteException("columns.Length != colType.Length");

        }

        string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

        for (int i = 1; i < col.Length; ++i)
        {

            query += ", " + col[i] + " " + colType[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }

    public SqliteDataReader SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)

    {

        if (col.Length != operation.Length || operation.Length != values.Length)
        {

            throw new SqliteException("col.Length != operation.Length != values.Length");

        }

        string query = "SELECT " + items[0];

        for (int i = 1; i < items.Length; ++i)
        {

            query += ", " + items[i];

        }

        query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

        for (int i = 1; i < col.Length; ++i)
        {

            query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";

        }

        return ExecuteQuery(query);

    }

}


2. 我使用的是CreateDB.cs来创建zyc.db

[csharp] view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4.   
  5. public class CreateDB : MonoBehaviour {  
  6.   
  7.   //  public GameObject objDB;  
  8.     private string appDBPath;  
  9.     private DbAccess db;  
  10.   
  11.     // Use this for initialization  
  12.     void Start () {  
  13.   
  14.           
  15. //如果运行在编辑器中  
  16. #if UNITY_EDITOR  
  17.         //通过路径找到第三方数据库  
  18.         string appDBPath = Application.dataPath + "/Plugins/Android/assets/" + "zyc.db";  
  19.         DbAccess db = new DbAccess("URI=file:" + appDBPath);  
  20.   
  21.   
  22.         //如果运行在Android设备中  
  23. #elif UNITY_ANDROID  
  24. <span style="white-space:pre">        </span>//将第三方数据库拷贝至Android可找到的地方  
  25. <span style="white-space:pre">        </span>string appDBPath = Application.persistentDataPath + "/" + "zyc.db";  
  26. <span style="white-space:pre">        </span>  
  27.         //如果已知路径没有地方放数据库,那么我们从Unity中拷贝  
  28. <span style="white-space:pre">        </span>if(!File.Exists(appDBPath))  
  29.  <span style="white-space:pre">      </span>{  
  30. <span style="white-space:pre">            </span>//用www先从Unity中下载到数据库  
  31. <span style="white-space:pre">        </span>    WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "zyc.db");   
  32.   
  33.   
  34.             while(!loadDB.isDone){}  
  35. <span style="white-space:pre">        </span>     //拷贝至规定的地方  
  36. <span style="white-space:pre">            </span> File.WriteAllBytes(appDBPath, loadDB.bytes);<span style="white-space:pre">      </span>         
  37. <span style="white-space:pre">        </span>}  
  38.    
  39. <span style="white-space:pre">        </span>//在这里重新得到db对象。  
  40. <span style="white-space:pre">        </span>DbAccess db = new DbAccess("URI=file:" + appDBPath);  
  41. #endif  
  42.   
  43.         db.CreateTable("card"new string[] { "id_name""an_name""bk_name" }, new string[] { "text""text""text" });  
  44.         db.InsertInto("card"new string[] { "'1'""'大象'""'zyc1'" });  
  45.         db.InsertInto("card"new string[] { "'2'""'狗    '""'zyc2'" });  
  46.         db.InsertInto("card"new string[] { "'3'""'猫    '""'zyc3'" });  
  47.         db.InsertInto("card"new string[] { "'4'""'老鼠'""'zyc'" });  
  48.         db.InsertInto("card"new string[] { "'5'""'猴子'""'zyc'" });  
  49.         db.InsertInto("card"new string[] { "'6'""'狐狸'""'zyc'" });  
  50.         db.InsertInto("card"new string[] { "'7'""'熊    '""'zyc'" });  
  51.         db.InsertInto("card"new string[] { "'8'""'山羊'""'zyc'" });  
  52.         db.InsertInto("card"new string[] { "'9'""'驴    '""'zyc'" });  
  53.         db.InsertInto("card"new string[] { "'10'""'马    '""'zyc'" });  
  54.         db.InsertInto("card"new string[] { "'11'""'骆驼'""'zyc'" });  
  55.         db.InsertInto("card"new string[] { "'12'""'企鹅'""'zyc'" });  
  56.         db.InsertInto("card"new string[] { "'13'""'鸟    '""'zyc'" });  
  57.         db.InsertInto("card"new string[] { "'14'""'长颈鹿'""'zyc'" });  
  58.         db.InsertInto("card"new string[] { "'15'""'斑马'""'zyc'" });  
  59.         db.InsertInto("card"new string[] { "'16'""'老虎'""'zyc'" });  
  60.         db.InsertInto("card"new string[] { "'17'""'狮子'""'zyc'" });  
  61.         db.InsertInto("card"new string[] { "'18'""'河马'""'zyc'" });  
  62.         db.InsertInto("card"new string[] { "'19'""'蛇    '""'zyc'" });  
  63.         db.InsertInto("card"new string[] { "'20'""'龙    '""'zyc'" });  
  64.   
  65.         //   objDB.SetActive(false);  
  66.   
  67.         db.CloseSqlConnection();  
  68.     }  
  69.   
  70.     // Update is called once per frame  
  71.     void Update () {  
  72.       
  73.     }  
  74. }  
using UnityEngine;
using System.Collections;
using System.IO;

public class CreateDB : MonoBehaviour {

  //  public GameObject objDB;
    private string appDBPath;
    private DbAccess db;

    // Use this for initialization
    void Start () {

        
//如果运行在编辑器中
#if UNITY_EDITOR
        //通过路径找到第三方数据库
        string appDBPath = Application.dataPath + "/Plugins/Android/assets/" + "zyc.db";
        DbAccess db = new DbAccess("URI=file:" + appDBPath);


        //如果运行在Android设备中
#elif UNITY_ANDROID
<span style="white-space:pre">		</span>//将第三方数据库拷贝至Android可找到的地方
<span style="white-space:pre">		</span>string appDBPath = Application.persistentDataPath + "/" + "zyc.db";
<span style="white-space:pre">		</span>
        //如果已知路径没有地方放数据库,那么我们从Unity中拷贝
<span style="white-space:pre">		</span>if(!File.Exists(appDBPath))
 <span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>//用www先从Unity中下载到数据库
<span style="white-space:pre">		</span>    WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "zyc.db"); 


            while(!loadDB.isDone){}
<span style="white-space:pre">		</span>     //拷贝至规定的地方
<span style="white-space:pre">			</span> File.WriteAllBytes(appDBPath, loadDB.bytes);<span style="white-space:pre">		</span>       
<span style="white-space:pre">		</span>}
 
<span style="white-space:pre">		</span>//在这里重新得到db对象。
<span style="white-space:pre">		</span>DbAccess db = new DbAccess("URI=file:" + appDBPath);
#endif

        db.CreateTable("card", new string[] { "id_name", "an_name", "bk_name" }, new string[] { "text", "text", "text" });
        db.InsertInto("card", new string[] { "'1'", "'大象'", "'zyc1'" });
        db.InsertInto("card", new string[] { "'2'", "'狗    '", "'zyc2'" });
        db.InsertInto("card", new string[] { "'3'", "'猫    '", "'zyc3'" });
        db.InsertInto("card", new string[] { "'4'", "'老鼠'", "'zyc'" });
        db.InsertInto("card", new string[] { "'5'", "'猴子'", "'zyc'" });
        db.InsertInto("card", new string[] { "'6'", "'狐狸'", "'zyc'" });
        db.InsertInto("card", new string[] { "'7'", "'熊    '", "'zyc'" });
        db.InsertInto("card", new string[] { "'8'", "'山羊'", "'zyc'" });
        db.InsertInto("card", new string[] { "'9'", "'驴    '", "'zyc'" });
        db.InsertInto("card", new string[] { "'10'", "'马    '", "'zyc'" });
        db.InsertInto("card", new string[] { "'11'", "'骆驼'", "'zyc'" });
        db.InsertInto("card", new string[] { "'12'", "'企鹅'", "'zyc'" });
        db.InsertInto("card", new string[] { "'13'", "'鸟    '", "'zyc'" });
        db.InsertInto("card", new string[] { "'14'", "'长颈鹿'", "'zyc'" });
        db.InsertInto("card", new string[] { "'15'", "'斑马'", "'zyc'" });
        db.InsertInto("card", new string[] { "'16'", "'老虎'", "'zyc'" });
        db.InsertInto("card", new string[] { "'17'", "'狮子'", "'zyc'" });
        db.InsertInto("card", new string[] { "'18'", "'河马'", "'zyc'" });
        db.InsertInto("card", new string[] { "'19'", "'蛇    '", "'zyc'" });
        db.InsertInto("card", new string[] { "'20'", "'龙    '", "'zyc'" });

        //   objDB.SetActive(false);

        db.CloseSqlConnection();
    }

    // Update is called once per frame
    void Update () {
	
    }
}

(1). 上面使用了预定义标签,用于编译时区分游戏平台与版本。

//如果运行在编辑器中

#if UNITY_EDITOR

 //如果运行在Android设备中

#elif UNITY_ANDROID

#endif

更多参考:http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

(2). 把二进制文件放在Plugins->Android->assets中,然后根据下面的路径就可以在Android中读取。

string Path  = jar:file://” + Application.dataPath + “!/assets/” + “你的文件“;

另外,使用这种方法读取地方放数据库后,是可以继续向数据库执行插入、删除、修改 、查询、等操作,用起来还是比较方便的。

3. Test.cs直接放到Camera上

[csharp] view plain copy print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using Mono.Data.Sqlite;  
  5. using System.IO;  
  6. using UnityEngine.UI;  
  7.   
  8. public class Test  :MonoBehaviour  
  9. {  
  10.     public Text objText;  
  11.   
  12.     private string an_name;  
  13.     private string bk_name;  
  14.   
  15.     void Start()  
  16.     {  
  17.   
  18.         数据库文件储存地址  
  19.         //string appDBPath = Application.persistentDataPath + "/zyc.db";  
  20.         注意!!!!!!!这行代码的改动  
  21.         //DbAccess db = new DbAccess("URI=file:" + appDBPath);  
  22.   
  23.         //如果运行在编辑器中  
  24. #if UNITY_EDITOR  
  25.         //通过路径找到第三方数据库  
  26.         string appDBPath = Application.dataPath + "/Plugins/Android/assets/" + "zyc.db";  
  27.         DbAccess db = new DbAccess("URI=file:" + appDBPath);  
  28.   
  29.         //如果运行在Android设备中  
  30. #elif UNITY_ANDROID  
  31.         //将第三方数据库拷贝至Android可找到的地方  
  32.         string appDBPath = Application.persistentDataPath + "/" + "zyc.db";  
  33.           
  34.         //如果已知路径没有地方放数据库,那么我们从Unity中拷贝  
  35.         if(!File.Exists(appDBPath))  
  36.         {  
  37.             //用www先从Unity中下载到数据库  
  38.             WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "zyc.db");   
  39.   
  40.             while(!loadDB.isDone){}  
  41.              //拷贝至规定的地方  
  42.              File.WriteAllBytes(appDBPath, loadDB.bytes);                
  43.         }  
  44.    
  45.         //在这里重新得到db对象。  
  46.         DbAccess db = new DbAccess("URI=file:" + appDBPath);  
  47. #endif  
  48.   
  49.         //db.CreateTable("PuzzleDebris", new string[] { "_coordinates_number", "_number", "_value" }, new string[] { "text", "text", "text" });  
  50.         //db.InsertInto("card", new string[] { "'21'", "'大象'", "'zyc21'" });  
  51.         //db.InsertInto("card", new string[] { "'22'", "'狗  '", "'zyc22'" });  
  52.         using (SqliteDataReader sqReader = db.SelectWhere("card"new string[]{"an_name","bk_name"}, new string[]{"id_name"}, new string[] {"="}, new string[] {"1"}))  
  53.         {  
  54.             Debug.Log("Begining Select !!!");   
  55.             while (sqReader.Read())  
  56.             {  
  57.                 //目前中文无法显示  
  58.                 Debug.Log("zyc" + sqReader.GetString(sqReader.GetOrdinal("an_name")));  
  59.                 Debug.Log("zyc" + sqReader.GetString(sqReader.GetOrdinal("bk_name")));  
  60.                 an_name = sqReader.GetString(sqReader.GetOrdinal("an_name"));  
  61.                 bk_name = sqReader.GetString(sqReader.GetOrdinal("bk_name"));  
  62.   
  63.                 objText.text = bk_name;  
  64.             }  
  65.   
  66.             sqReader.Close();  
  67.   
  68.             db.CloseSqlConnection();  
  69.         }  
  70.   
  71.     }  
  72.   
  73.     void Update()  
  74.     {  
  75.         objText.text = bk_name;  
  76.     }  
  77.   
  78. }  
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Mono.Data.Sqlite;
using System.IO;
using UnityEngine.UI;

public class Test  :MonoBehaviour
{
    public Text objText;

    private string an_name;
    private string bk_name;

    void Start()
    {

        数据库文件储存地址
        //string appDBPath = Application.persistentDataPath + "/zyc.db";
        注意!!!!!!!这行代码的改动
        //DbAccess db = new DbAccess("URI=file:" + appDBPath);

        //如果运行在编辑器中
#if UNITY_EDITOR
        //通过路径找到第三方数据库
        string appDBPath = Application.dataPath + "/Plugins/Android/assets/" + "zyc.db";
        DbAccess db = new DbAccess("URI=file:" + appDBPath);

        //如果运行在Android设备中
#elif UNITY_ANDROID
		//将第三方数据库拷贝至Android可找到的地方
		string appDBPath = Application.persistentDataPath + "/" + "zyc.db";
		
        //如果已知路径没有地方放数据库,那么我们从Unity中拷贝
		if(!File.Exists(appDBPath))
 		{
			//用www先从Unity中下载到数据库
		    WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "zyc.db"); 

            while(!loadDB.isDone){}
		     //拷贝至规定的地方
			 File.WriteAllBytes(appDBPath, loadDB.bytes);		       
		}
 
		//在这里重新得到db对象。
		DbAccess db = new DbAccess("URI=file:" + appDBPath);
#endif

        //db.CreateTable("PuzzleDebris", new string[] { "_coordinates_number", "_number", "_value" }, new string[] { "text", "text", "text" });
        //db.InsertInto("card", new string[] { "'21'", "'大象'", "'zyc21'" });
        //db.InsertInto("card", new string[] { "'22'", "'狗  '", "'zyc22'" });
        using (SqliteDataReader sqReader = db.SelectWhere("card", new string[]{"an_name","bk_name"}, new string[]{"id_name"}, new string[] {"="}, new string[] {"1"}))
        {
            Debug.Log("Begining Select !!!"); 
            while (sqReader.Read())
            {
                //目前中文无法显示
                Debug.Log("zyc" + sqReader.GetString(sqReader.GetOrdinal("an_name")));
                Debug.Log("zyc" + sqReader.GetString(sqReader.GetOrdinal("bk_name")));
                an_name = sqReader.GetString(sqReader.GetOrdinal("an_name"));
                bk_name = sqReader.GetString(sqReader.GetOrdinal("bk_name"));

                objText.text = bk_name;
            }

            sqReader.Close();

            db.CloseSqlConnection();
        }

    }

    void Update()
    {
        objText.text = bk_name;
    }

}


版权声明:本文为博主原创文章,未经博主允许不得转载。 //blog.csdn.net/w5897093/article/details/50719461
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值