简体   繁体   English

python错误:“发件人”附近:语法错误

[英]python error: near “From”: syntax error

I am trying to insert 6 values into separate columns in a database and when running my code i'm getting the near "From" syntax error can someone help? 我试图将6个值插入数据库的单独列中,并且在运行我的代码时,我遇到了附近的“发件人”语法错误,有人可以帮忙吗?

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

the error is this: 错误是这样的:

Traceback (most recent call last):

Python Shell, prompt 2, line 1 File "/Users/user1/Desktop/assignment 2/banking.py", line 64, in cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)') sqlite3.OperationalError: near "From": syntax error 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,到TEXT,金额为REAL)')sqlite3.OperationalError:“ From”附近:语法错误

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

You have a column named From. 您有一个名为From的列。 From is a sql Keyword, I would avoid using it as it may cause syntax errors From是一个sql关键字,我会避免使用它,因为它可能会导致语法错误

Try something more descriptive like 尝试一些更具描述性的内容,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM