简体   繁体   中英

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?

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

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

You have a column named From. From is a sql Keyword, I would avoid using it as it may cause syntax errors

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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