【发布时间】:2012-03-25 17:52:15
【问题描述】:
我遇到了这个我无法解决的错误 我在 Python 中编写了一些代码,使用 tkinter 接口将数据从文本文件传输到 sqlite。
首先,相关代码如下:
def submit_data(self):
self.new_filename = self.file_entry.get()
self.new_tablename = self.table_entry.get()
self.new_fieldname = self.field_entry.get().split(',')
#print(self.new_fieldname)
self.create_new.destroy()
from sqlite3 import dbapi2 as sqlite
con = sqlite.connect(self.new_filename)
cur = con.cursor()
cur.execute('CREATE TABLE ' + self.new_tablename + '(id INTEGER PRIMARY KEY)')
for field in self.new_fieldname:
cur.execute('ALTER TABLE ' + self.new_tablename + ' ADD ' + field)
with open(self.filename, 'r', encoding='latin-1') as self.the_file:
status = True
#_keynumber=1
while status:
_row = self._next_line()
if _row:
_entry_list = _row.split(',')
# add space after text line comma for formatting
_entry_list = ', '.join(_entry_list)
#print(_entry_list)
#entries = {'row': _keynumber, 'entry': _entry_list}
#row_entry = "INSERT INTO " + self.new_tablename + " VALUES(" + _entry_list + ")"
cur.execute("INSERT INTO " + self.new_tablename + " VALUES(" + _entry_list + ")")
#_colrange = range(_colamount)
#_keynumber+=1
else:
status = False
con.commit()
在 cur.execute("INSERT INTO " ... 行(大约 6 行)我收到此错误: ** cur.execute("INSERT INTO " + self.new_tablename + " VALUES(" + _entry_list + ")") sqlite3.OperationalError:靠近“。”:语法错误**
我已经用许多不同的方式改变了这一点。有一次,我将整个“INSERT INTO ... VALUES ....”字符串作为变量并使用了
cur.execute(*variable*)
当我这样做时,错误是相同的,除了“OperationalError: near "." 是 "OperationalError: near "of" ...并且任何地方都没有 'of'。
我真的很困惑和沮丧。有人帮我分解一下吗??
谢谢 F
其读取的文本文件行设置如下: 好莱坞的大明星,桑德拉·狄金森
所以我想如果我使用 .join() 在逗号后放置一个空格,那么对于 INSERT INTO 语句,字符串将相当于两个 VALUES。
【问题讨论】:
标签: python sqlite python-3.x tkinter