0
我有此腳本在Unity 4 ..無法寫入到離線數據庫
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using Mono.Data.SqliteClient;
using System.IO;
public class dbAccess : MonoBehaviour {
private string connection;
private IDbConnection dbcon;
private IDbCommand dbcmd;
private IDataReader reader;
public string nama;
void OnGUI()
{
GUI.Box(new Rect(150,50,200,200),"Nama");
nama = GUI.TextField(new Rect(50,30,100,20),nama,25);
string[] values = {"NULL", nama};
if(GUI.Button(new Rect(65,50,70,20),"Ok"))
{
InsertInto("user", values);
}
}
// Use this for initialization
void Start() {
if(!PlayerPrefs.GetInt("first_run").Equals(1))
{
String _databaseFileName = "mydatabase.sqlite";
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 + "!/asset/!" + dbName);
yield return loadDb;
File.WriteAllBytes(writePath, loadDb.bytes);
}
public void OpenDB(string p)
{
connection = "URI=file:mydatabase.sqlite" + 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;
}
void Update() {
}
}
當我插入的名稱,然後點擊「OK 」按鈕,
不能插入我的數據庫..
有人可以幫我修復我的腳本嗎?
我在C#新手,我很困惑..
感謝您的幫助..
System.NullReferenceException:對象引用未設置爲對象的實例 at dbAccess.InsertInto(System.String tableName,System.String [] values)[0x00043]位於C:\ Users \ budi \ Documents \ testDB \ Assets/dbAccess.cs:154 UnityEngine.Debug:Log(Object) dbAccess:InsertInto(String,String [])(在Assets/dbAccess.cs:159) dbAccess:OnGUI()(在Assets/dbAccess.cs :25) – myura5
您是否在查詢中的對象周圍添加了一些空格,如我所示?你看到我的陣容和你的陣容有差異嗎? – OneFineDay
仍然錯誤,因爲它就像InsertIntoSingle .. – myura5