2017-01-10 47 views
1

我正在使用Unity,這裏是我的代碼的一小段。我想要做的就是將data.tokendata.expire放入SQLite中。出於某種原因,不斷拋出我的錯誤是:SQLite錯誤無法識別的令牌Unity

SqliteException: SQLite error 
unrecognized token: "587503bc773a565d52401c87" 
Mono.Data.Sqlite.SQLite3.Prepare (Mono.Data.Sqlite.SqliteConnection cnn, System.String strSql, Mono.Data.Sqlite.SqliteStatement previous, UInt32 timeoutMS, System.String& strRemain) 
Mono.Data.Sqlite.SqliteCommand.BuildNextCommand() 

我不知道該怎麼令牌無法識別,SQLite中的Token場是STRINGExpire字段是INTEGER

IncomingTokenData data = IncomingTokenData.CreateFromJSON(www.text); 

string conn = "URI=file:" + Application.dataPath + "/MyDataBase.s3db"; //Path to database. 
var sqlQuery = "INSERT INTO MyDataBase(Token, Expire) VALUES(" + data.token +", " + data.expire + ")"; 

if(!string.IsNullOrEmpty(www.error)) { 
    ErrText.text = "Error: " + www.error; 
}else{ 
    if(data.pass == "1"){ 
     IDbConnection dbconn; 
     dbconn = (IDbConnection) new SqliteConnection(conn); 
     dbconn.Open(); //Open connection to the database. 
     IDbCommand dbcmd = dbconn.CreateCommand(); 
     dbcmd.CommandText = sqlQuery; 
     dbcmd.ExecuteNonQuery(); 

回答

2

在SQL查詢,你應該用單引號字符串值(在這種情況下,地方引號data.token):

​​

注意字符串連接是沒有建立起來的最佳途徑SQL查詢 - 一個更強大的方式來避免這些問題的方法是使用佔位符,如built-in functionality IDbCommand has添加參數:

var sqlQuery = "INSERT INTO MyDataBase(Token, Expire) VALUES(@token, @expire)"; 

if(!string.IsNullOrEmpty(www.error)) { 
    ErrText.text = "Error: " + www.error; 
}else{ 
    if(data.pass == "1"){ 
     IDbConnection dbconn; 
     dbconn = (IDbConnection) new SqliteConnection(conn); 
     dbconn.Open(); //Open connection to the database. 
     IDbCommand dbcmd = dbconn.CreateCommand(); 
     dbcmd.Parameters.Add("@token", SqlDbType.VarChar).Value = data.token; 
     dbcmd.Parameters.Add("@expire", SqlDbType.Int).Value = data.expire; 
     dbcmd.CommandText = sqlQuery; 
     dbcmd.ExecuteNonQuery(); 

使用這種方法,值根據其數據類型正確格式化。

+0

非常感謝你:) –

+0

@CorySparks很高興能幫到你!有時,如果使用'String.Format()'構建查詢,則更容易發現這些問題,但是對您來說最好。 – Serlite

+0

'IDbConnection' thingy沒有辦法使用佔位符嗎? – simbabque