首页 文章

python错误:靠近“From”:语法错误

提问于
浏览
-1

我试图在数据库中的单独列中插入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事务(日期文本,数字文本,类型文本,从文本, To TEXT,Amount REAL)')sqlite3.OperationalError:near“From”:语法错误

1 回答

  • 5

    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)')

相关问题