首页 > 解决方案 > 简单表创建中的 Python sqlite3 OperationalError

问题描述

这是代码

def __init__(self):
    self._db = sqlite3.connect("Reservation.db")
    self._db.row_factory = sqlite3.Row
    self._db.execute("create table if not exists Ticket(ID integer primary key autoincrement, Name text, Gender text, Order text)")#create a table called Ticket with 4 columns
    self._db.commit()

问题

self._db.execute("如果不存在则创建表 Ticket(ID 整数主键自动增量,名称文本,性别文本,订单文本)") sqlite3.OperationalError: near "Order": 语法错误

标签: python-3.xsqlite

解决方案


order是 SQL 中的保留字。我建议您找到一个不同的名称,它不是order_text该列的保留字(例如,)。如果您绝对必须使用此名称,您可以通过用双引号 ( ) 将其转义"

self._db.execute("create table if not exists Ticket(ID integer primary key autoincrement, Name text, Gender text, \"Order\" text)
# Here -----------------------------------------------------------------------------------------------------------^------^

推荐阅读