数据库异常 Flutter sqflite

huangapple go评论108阅读模式
英文:

Database Exception Flutter sqflite

问题

我想创建多个像这样的表格

static Future<void> createTables(sql.Database database) async {
    await database.execute('''CREATE TABLE cart (
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      food TEXT,
      price INTEGER,
      qty INTEGER,
      cashier TEXT,
      createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ''');
    await database.execute('''CREATE TABLE transaction (
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      no_nota TEXT,
      food TEXT,
      price INTEGER,
      qty INTEGER,
      cashier TEXT,
      createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ''');
}

但是在构建/运行应用程序后,应用程序会抛出错误

异常已发生。SqfliteDatabaseException
(DatabaseException(在"transaction"附近: 语法错误 (代码 1
SQLITE_ERROR): , 在编译时: CREATE TABLE transaction (
id INTEGER 主键 自增不为空,
no_nota TEXT,
food TEXT,
price INTEGER,
qty INTEGER,
cashier TEXT,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)) sql 'CREATE TABLE transaction (
id INTEGER 主键 自增不为空,
no_nota TEXT,
food TEXT,
price INTEGER,
qty INTEGER,
cashier TEXT,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ' args [])

我该怎么办?谢谢

英文:

i want to create multiple table like this

static Future&lt;void&gt; createTables(sql.Database database) async {
    await database.execute(&quot;&quot;&quot;CREATE TABLE cart (
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      food TEXT,
      price INTEGER,
      qty INTEGER,
      cashier TEXT,
      createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) &quot;&quot;&quot;);
    await database.execute(&quot;&quot;&quot;CREATE TABLE transaction (
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      no_nota TEXT,
      food TEXT,
      price INTEGER,
      qty INTEGER,
      cashier TEXT,
      createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) &quot;&quot;&quot;);
}

But after i build/run application, the application throws error

> Exception has occurred. SqfliteDatabaseException
> (DatabaseException(near "transaction": syntax error (code 1
> SQLITE_ERROR): , while compiling: CREATE TABLE transaction (
> id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
> no_nota TEXT,
> food TEXT,
> price INTEGER,
> qty INTEGER,
> cashier TEXT,
> createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
> )) sql 'CREATE TABLE transaction (
> id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
> no_nota TEXT,
> food TEXT,
> price INTEGER,
> qty INTEGER,
> cashier TEXT,
> createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
> ) ' args [])

how should i do ? Thanks

答案1

得分: 1

避免在SQL中使用保留关键字或对其进行转义

问题出在你尝试创建的表名上。单词“transaction”是SQLite中的保留关键字,这意味着你不能直接将其用作表名,除非正确地对其进行转义。

要么你更改表名,要么像这样对其进行转义:`transaction`。考虑下面的代码

CREATE TABLE `transaction` (
id INTEGER PRIMARY KEY
AUTOINCREMENT NOT NULL,
no_nota TEXT,
food TEXT,
price INTEGER,
qty INTEGER,
cashier TEXT,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)

英文:

Avoid reserved keywords in SQL or escape them

The issue is with the table name you're trying to create. The word "transaction" is a reserved keyword in SQLite, which means you can't use it directly as a table name without escaping it properly.

It is either you change the table name or escape it like so `transaction`.. Consider the below code

    CREATE TABLE `transaction` (
  id INTEGER PRIMARY KEY 
  AUTOINCREMENT NOT NULL,
  no_nota TEXT,
  food TEXT,
  price INTEGER,
  qty INTEGER,
  cashier TEXT,
  createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)

huangapple
  • 本文由 发表于 2023年6月25日 17:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定