我正在使用python 2.7和pyqt4.10以及sqlite3 Db,尝试从QlineEdit
获取用户输入以插入已创建的sqlite3
表
表格结构
CREATE TABLE `categories` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`category_name` TEXT NOT NULL UNIQUE
);
我尝试在添加输入后使用新数据刷新Qlistwidget
中的列表
这是我的完整代码:
def proc():
input_user = self.lineEdit.text()
conn = sqlite3.connect('storage/container.db')
conn.row_factory = lambda c, row: row[0]
c = conn.cursor()
c.execute("INSERT INTO categories (category_name) VALUES (?)", (input_user, ))
conn.commit()
conn = sqlite3.connect('storage/container.db')
conn.row_factory = lambda c, row: row[0]
c = conn.cursor()
c.execute("SELECT category_name FROM categories")
category_all = c.fetchall()
for items in category_all:
self.listWidget.addItem(items)
conn.close()
如您所见,我使用input_user = self.lineEdit.text()
从QlineEdit
错误是:
Traceback (most recent call last):
File "C:\python\townoftechwarehouse\add_category.py", line 63, in proc
c.execute("INSERT INTO categories (category_name) VALUES (?)", (input_user, ))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
答案 0 :(得分:0)
发现问题是我使用的是qslite3无法读取的阿拉伯字符所以我必须使用unicode
,这里是编辑:
已更改:
input_user = self.lineEdit.text()
致:
input_user1 = self.lineEdit.text()
input_user = unicode(input_user1)