Python和SQLite:插入表格 [英] Python and SQLite: insert into table
本文介绍了Python和SQLite:插入表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含3行的列表,每行分别代表一个表行:
I have a list that has 3 rows each representing a table row:
>>> print list
[laks,444,M]
[kam,445,M]
[kam,445,M]
如何将此列表插入表格?
How to insert this list into a table?
我的表结构是:
tablename(name varchar[100], age int, sex char[1])
还是我应该使用列表以外的其他东西?
Or should I use something other than list?
这是实际的代码部分:
for record in self.server:
print "--->",record
t=record
self.cursor.execute("insert into server(server) values (?)",(t[0],));
self.cursor.execute("insert into server(id) values (?)",(t[1],))
self.cursor.execute("insert into server(status) values (?)",(t[2],));
分别插入三个字段是有效的,但是使用单行,如
Inserting the three fields separately works, but using a single line like
self.cursor.execute("insert into server(server,c_id,status) values (?,?,?)",(t[0],),(t[1],),(t[2],))
或
self.cursor.execute("insert into server(server,c_id,status) values (?,?,?)",(t),)
没有.
推荐答案
conn = sqlite3.connect('/path/to/your/sqlite_file.db')
c = conn.cursor()
for item in my_list:
c.execute('insert into tablename values (?,?,?)', item)
这篇关于Python和SQLite:插入表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文