需要帮助跟踪 SQLiteException:无法识别的令牌



我是使用SQLite的新手,今天大部分时间我一直在研究这个问题,而且我很难追踪我有什么问题。错误在第dbManager.Execute(sql, driftID, driftName, GenerateDriftStep(), driftLat, driftLong, driftTexLocation);行被抛出,但我不确定为什么。我仔细检查以确保表名正确无误。任何人都可以看到无法识别的令牌隐藏在哪里吗?谢谢!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SimpleSQL;
using System;
public class CreateNewDrift : MonoBehaviour
{
// reference to the database manager in the scene
public SimpleSQLManager dbManager;
// reference to the output text in the scene
public Text outputText;
// Drift Variables
public int driftNumSteps;
private string driftID;
private string driftName = "temp";
private float driftLat = 0.0f;
private float driftLong = 0.0f;
private string driftTexLocation = "temp";
string GenerateDriftID()
{
driftID = Guid.NewGuid().ToString().Replace("-", "");
return null;
}
string GenerateDriftStep()
{
string driftStepInput = GetComponent<DriftInstruction>().ConstructStep();
return driftStepInput;
}
public void CreateTable()
{
GenerateDriftID();
string sql =
"CREATE TABLE " + """ + driftID + """ + " " +
"("DriftIndex" INTEGER PRIMARY KEY AUTOINCREMENT, " +
""DriftID" VARCHAR(60), " +
""DriftName" VARCHAR(60), " +
""DriftStep" TEXT, " +
""DriftLat" FLOAT, " +
""DriftLong" FLOAT, " +
""DriftTexLocation" VARCHAR(255))";
dbManager.Execute(sql);
PopulateTable();
}
public void PopulateTable()
{
string sql =
"INSERT INTO " + driftID + " " +
"(DriftID, DriftName, DriftStep, DriftLat, DriftLong, DriftTexLocation) " +
"VALUES(?, ?, ?, ?, ?, ?)";
dbManager.BeginTransaction();
while (driftNumSteps > 0)
{
dbManager.Execute(sql, driftID, driftName, GenerateDriftStep(), driftLat, driftLong, driftTexLocation);
driftNumSteps--;
}
dbManager.Commit();
}
}

完全错误:

SQLiteException: unrecognized token: "8d51c6b280c64bea848f29e5cad91ee3"
SimpleSQL.SQLite3.Prepare2 (IntPtr db, System.String query)
SimpleSQL.SQLiteCommand.Prepare ()
SimpleSQL.SQLiteCommand.ExecuteNonQuery ()
SimpleSQL.SQLiteConnection.Execute (System.String query, System.Object[] args)
SimpleSQL.SimpleSQLManager.Execute (System.String query, System.Object[] args)
CreateNewDrift.PopulateTable () (at Assets/_Scripts/CreateNewDrift.cs:65)
CreateNewDrift.CreateTable () (at Assets/_Scripts/CreateNewDrift.cs:51)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

我很确定"无法识别的标记">表名,并且它没有适当地封闭,因为它需要,因为表名以数字开头。

解决方案可以是将其括在[]、''(重音符号,即 1 键左侧的键(、"' 中,或者在数字部分前面加上非数字有效字符。

有关在 SQL 中封装标识符的更多信息 SQLite 理解 - SQLite 关键字

下面是 4 种类型的外壳的示例,最后是一个以数字开头的未封闭/裸表名称:-

CREATE TABLE "2d51c6b280c64bea848f29e5cad91ee3" (col1 TEXT)
> OK
> Time: 0.21s

CREATE TABLE '3d51c6b280c64bea848f29e5cad91ee3' (col1 TEXT)
> OK
> Time: 0.221s

CREATE TABLE [4d51c6b280c64bea848f29e5cad91ee3] (col1 TEXT)
> OK
> Time: 0.206s

CREATE TABLE `5d51c6b280c64bea848f29e5cad91ee3` (col1 TEXT)
> OK
> Time: 0.251s

CREATE TABLE 1d51c6b280c64bea848f29e5cad91ee3 (col1 TEXT)
> unrecognized token: "1d51c6b280c64bea848f29e5cad91ee3"
> Time: 0s
  • 最后一次尝试也会复制错误;除了实际有意更改的表名。

最新更新