簡體   English   中英

python錯誤:“發件人”附近:語法錯誤

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

我試圖將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()

錯誤是這樣的:

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,到TEXT,金額為REAL)')sqlite3.OperationalError:“ From”附近:語法錯誤

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

您有一個名為From的列。 From是一個sql關鍵字,我會避免使用它,因為它可能會導致語法錯誤

嘗試一些更具描述性的內容,例如

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