Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite错误无法识别的令牌统一_Sqlite_Unity3d - Fatal编程技术网

SQLite错误无法识别的令牌统一

SQLite错误无法识别的令牌统一,sqlite,unity3d,Sqlite,Unity3d,我使用的是Unity,下面是我的一段代码。我要做的就是将data.token和data.expire放入SQLite。出于某种原因,它不断向我抛出一个错误,即: SqliteException: SQLite error unrecognized token: "587503bc773a565d52401c87" Mono.Data.Sqlite.SQLite3.Prepare (Mono.Data.Sqlite.SqliteConnection cnn, System.String strSq

我使用的是Unity,下面是我的一段代码。我要做的就是将
data.token
data.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
字段是一个
字符串
Expire
字段是一个
整数

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

在SQL查询中,您应该将字符串值括在单引号中(在本例中,在
数据.标记
周围加引号):

请注意,字符串连接并不是构建SQL查询的最佳方法-避免这些问题的更可靠方法是使用占位符,如用于添加参数的:

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

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

@CorySparks很乐意提供帮助!有时,如果您使用
String.Format()
构建查询,则更容易发现这些问题,但要做对您最有利的事情。
IDbConnection
是否没有使用占位符的方法?@simbabque啊,您完全正确。我忘记了SQLite实际上有自己的方法来添加参数,正如投票率最高的答案中所描述的——这是我涉足SQLite时喜欢的方法。实际上,这主要是由驱动程序来完成的。一些RDBMS本身支持
占位符,但不是所有占位符,因此驱动程序通过正确引用来模拟占位符。在任何情况下,你都应该更新你的答案来说明问题所在,同时也要指出你应该始终使用占位符。SQL注入也可能发生在非web应用程序上,我们不希望妈妈删除我们的savegames,是吗?:)@simbabque感谢您的见解!我已经在答案中添加了建议和修改的代码。
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();