python错误:near";从“开始”:语法错误
我试图在数据库中的不同列中插入6个值,当运行代码时,我遇到了接近“From”的语法错误,有人能帮忙吗python错误:near";从“开始”:语法错误,python,syntax-error,python-3.5,Python,Syntax Error,Python 3.5,我试图在数据库中的不同列中插入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_f
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('创建表事务(日期文本、数字文本、类型文本、从文本、到文本、实际金额)')
sqlite3.OperationalError:near“From”:语法错误('CREATE TABLE Transactions(日期文本、数字文本、类型文本、From TEXT、To TEXT、Amount REAL)
您有一个名为From的列。From是一个sql关键字,我会避免使用它,因为它可能会导致语法错误
尝试更具描述性的方法,如
cur.execute('CREATE TABLE Transactions(日期\u created TEXT,当前\u Number TEXT,记录\u type TEXT,from\u某地TEXT,to\u某地TEXT,amount REAL)
from是sql中的关键字,列名称更具描述性,IE:From\u Something
但我没有调用我插入的数据,我根本不用它。请复制错误跟踪和日期,数字甚至可能会隐藏数据_type@AharonSnyder完成后接受答案。其他用户将来可能会有类似的问题!上面说我需要等4分钟才能接受答案。但是如果你真的想使用这些确切的名称,我想你可以引用这些列名,但是这可能会带来更多的麻烦。