SQLite错误无法识别令牌Unity

时间:2017-01-10 16:10:56

标签: sqlite unity3d

我正在使用Unity,这里是我的代码的一小部分。我想要做的就是将data.tokendata.expire放入SQLite。由于某种原因,它不断给我一个错误:

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();

1 个答案:

答案 0 :(得分:2)

在SQL查询中,您应该将字符串值括在单引号中(在这种情况下,在data.token周围放置引号):

var sqlQuery = "INSERT INTO MyDataBase(Token, Expire) VALUES('" + data.token +"', " + data.expire + ")";

请注意,字符串连接不是构建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();

使用此方法,可以根据数据类型正确格式化值。