【问题标题】:Need help tracking down SQLiteException: unrecognized token需要帮助追踪 SQLiteException:无法识别的令牌
【发布时间】:2018-11-06 14:07:12
【问题描述】:

我在使用 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()

【问题讨论】:

  • 能不能在执行sql前把sql写到log中。我的猜测是,这将是表名,它不能(除非包含)以数字开头(但看起来你已经包含了它)。按照SQLite Keywords 括在 []、""、'' 或 `` 中,将解决该问题。
  • 这样说,而不是封闭,会在 Navicat 中导致与 CREATE TABLE 8d51c6b280c64bea848f29e5cad91ee3 (col1 TEXT) > 无法识别的令牌相同的错误:“8d51c6b280c64bea848f29e5cad91ee3”> 时间:0s
  • 感谢@MikeT,现在一切正常。

标签: c# sql sqlite unity3d sql-insert


【解决方案1】:

我很确定 "unrecognized token"表名,并且它没有被适当地封闭,它会需要,因为表名以数字开头。

解决方案可能是将其括在 []``(重音,即 1 键左侧的键)、'' 或交替在数字部分之前加上一个非数字有效字符。

更多关于在SQL As Understood By SQLite - SQLite Keywords封闭标识符

这是一个包含 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
  • 最后一次尝试也复制了错误;禁止实际故意更改的表名。

【讨论】:

    猜你喜欢
    • 2023-01-22
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2022-01-03
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多