Python and SQLite: insert into table(Python 和 SQLite:插入表)
问题描述
我有一个包含 3 行的列表,每行代表一个表格行:
<预><代码>>>>打印清单[湖,444,M][锦,445,M][锦,445,M]如何将这个列表插入到表格中?
我的表结构是:
<前>表名(名称 varchar[100],年龄整数,性别字符 [1])或者我应该使用列表以外的其他东西吗?
这是实际的代码部分:
在 self.server 中记录:打印--->",记录t=记录self.cursor.execute("插入服务器(服务器)值(?)",(t[0],));self.cursor.execute("插入服务器(id)值(?)",(t[1],))self.cursor.execute("插入服务器(状态)值(?)",(t[2],));单独插入三个字段有效,但使用单行
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()对于 my_list 中的项目:c.execute('插入表名值(?,?,?)', item)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?
My table structure is:
tablename(name varchar[100], age int, sex char[1])
Or should I use something other than list?
Here is the actual code part:
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],))
or
self.cursor.execute("insert into server(server,c_id,status) values (?,?,?)",(t),)
does not.
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:插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 和 SQLite:插入表
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
