python error: near "From": syntax error

python error: near "From": syntax error

我正在尝试将 6 个值插入到数据库中的单独列中,当 运行 我的代码出现接近 "From" 语法错误时,有人可以帮忙吗?

def setup_transactions(db, filename):
    '''(str, str) -> NoneType
    Create and populate the Transactions table for database db using the
    contents of the file named filename.'''

    data_file = open(filename)
    con =  sqlite3.connect(db)
    cur = con.cursor()

    # create and populate the table here
    cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type     TEXT, From TEXT, To TEXT, Amount REAL)')

    for line in data_file:
        data = line.split()
        cur.execute('INSERT INTO Accounts VALUES (?, ?, ?, ?, ?, ?)', (data[0], data[1], data[2], data[3], data[4], data[5]))
    data_file.close()
    cur.close()
    con.commit()
    con.close()

错误是这样的:

Traceback (most recent call last):

Python Shell,提示 2,第 1 行 文件“/Users/user1/Desktop/assignment 2/banking.py”,第 64 行,在 cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)') sqlite3.OperationalError: 附近 "From": 语法错误

cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)')

您有一个名为发件人的列。 From 是一个 sql 关键字,我会避免使用它,因为它可能会导致语法错误

尝试使用更具描述性的内容,例如

cur.execute('CREATE TABLE Transactions(date_created TEXT, current_Number TEXT, record_type TEXT, from_somewhere TEXT, to_somewhere TEXT, amount REAL)')