英文:
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<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
) """);
}
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论