有人可以告诉我如何从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
问题是,当我点按确定按钮时如何保存用户名。
有人可以帮助我吗?
答案 0 :(得分:0)
当您在Unity android项目中包含数据库时,它最初会打包在APK中,您必须将其从APK中提取出来才能访问它。
您应该做的第一件事是将它放在流资产文件夹中,然后您可以将此代码称为协同程序,以将其提取给您,并将其提取到持久性数据路径
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对它运行查询。