0

有人能告诉我如何从android连接离线数据库吗?

我这里有一些脚本..

using UnityEngine;
using System;
using System.Collections;
using System.Data;
using Mono.Data.SqliteClient;

public class dbAccess : MonoBehaviour {
private string connection;
private IDbConnection dbcon;
private IDbCommand dbcmd;
private IDataReader reader;

// Use this for initialization
void Start () {

}

public void OpenDB(string p)
{
connection = "URI=file:" + p;
dbcon = new SqliteConnection(connection);
dbcon.Open();
}

public void CloseDB()
{
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}

IDataReader BasicQuery(string query)
{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
return reader;
}

int CreateTable(string name, string[] col, string[] colType)
{
string query;
query = "CREATE TABLE" + name + "(" + col[0] + " " + colType[0];
for(var i=1; i<col.Length; i++){
query += "," + col[i] + " " + colType[i]; 
}
query += ")";
try{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}

int InsertIntoSingle(string tableName, string colName, string value)
{
string query;
query = "INSERT INTO" + tableName + "(" + colName + ")" + "VALUES (" + value + ")";
try{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}

int InsertIntoSpecific(string tableName, string[] col,string[] values)
{
string query;
query = "INSERT INTO" + tableName + "(" + col[0];
for(int i=1; i<col.Length; i++){
query += "," + col[i];
}
query += ") VALUES (" + values[0];
for(int i=1; i<values.Length; i++){
query += "," + values[i];
}
query += ")";
try{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}

int InsertInto(string tableName, string[] values)
{
string query;
query = "INSERT INTO" + tableName + "VALUES(" + values[0];
for(int i=1; i<values.Length; i++){
query += ")" + values[i];
}
query += ")";
try{
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
catch(Exception e){
Debug.Log(e);
return 0;
}
return 1;
}

public string[] SingleSelectWhere(string tableName, string itemToSelect, string wCol, string wPar, string wValue)
{
string query;
query = "SELECT" + itemToSelect + "FROM" + tableName + "WHERE" + wCol + wPar + wValue;
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
string[] readArray = new string[reader.RecordsAffected];
int i=0;
while(reader.Read()){
readArray[i] = reader.GetString(0);
i++;
}
return readArray;
}

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

}
}

我的统一资产中有名为user.db的数据库

问题是,当我点击确定按钮时如何保存用户名..

有人能帮我吗?

4

1 回答 1

0

当您在 Unity android 项目中包含数据库时,它最初会打包在 APK 中,您必须将其从 APK 中提取出来才能访问它。

您应该做的第一件事是将其放在流资产文件夹中,然后您可以将此代码作为协程调用以将其提取到您的 androids 持久数据路径

void Start()
{    
    //Only run on first run
    if(!PlayerPrefs.GetInt("first_run").Equals(1))
    {
        string _databaseFileName = "user.db";
        string _databasePath = Application.persistentDataPath + "/" + _databaseFilename;
        StartCoroutine( ExtractFileFromJar(_databaseFilename, _databasePath));
        PlayerPrefs.SetInt("first_run", 1);
    }
}

public static IEnumerator ExtractFileFromJar(string dbName, string writePath)
{       
    WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + dbName);
    yield return loadDB;
    File.WriteAllBytes(writePath, loadDB.bytes);
}   

从那里您可以从持久数据路径访问数据库。我使用 SQLiteKit 对它运行查询。

http://u3d.as/content/orange-tree/sqlite-kit/3ka

于 2013-11-09T17:34:37.473 回答