- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
情况:需要向 SQLite 数据库中插入大量数据。
问题:我们可以使用两个语句来插入数据 -
data = [("111", "222", "333"), ("AAA", "BBB", "CCC"), ("XXX", "YYY", "ZZZ")]
#method1
for item in data:
cursor.execute("INSERT INTO table(value1, value2, value3) VALUES (?,?,?)", item)
conn.commit()
#method2
cursor.execute("INSERT INTO table(value1, value2, value3) VALUES(?,?,?)", data)
conn.commit()
问题:如果忽略速度,从编程的角度来看,哪种做法更好?如果可能的话,也解释一下原因。
最佳答案
从纯粹的编程实践角度来看,除了速度之外,没有什么区别。然而...
准备好的陈述很好。然而,mass-insert 会产生大量变量绑定(bind),而 SQLite 有一个 upper limit它可以处理的主机参数数量,默认为 999。
因此,多重插入很适合玩耍,但对于实际数据,您将使用循环。我可以提供的一个好建议是,您需要将循环包装在事务中,因为没有它,据我所知,每次插入都将是一个自动事务,这将极大地影响时间。 (此外,在循环末尾提交,而不是在循环内提交。)
编辑:根据Python文档,
By default, the sqlite3 module opens transactions implicitly before a Data Modification Language (DML) statement (i.e.
INSERT
/UPDATE
/DELETE
/REPLACE
), and commits transactions implicitly before a non-DML, non-query statement (i. e. anything other thanSELECT
or the aforementioned).
因此,#method1 中的代码正在执行 [BEGIN
]、INSERT
、COMMIT
、[BEGIN
] 、INSERT
、COMMIT
...,其中 BEGIN
由 Python 隐式发送以启动事务,并且 COMMIT
显式发送结束它。如果您的代码结构如下:
for item in data:
cursor.execute("INSERT INTO table(value1, value2, value3) VALUES (?,?,?)", item)
conn.commit()
然后你在开始处有一个隐式的BEGIN
,大量的INSERTS
和在最后一个显式的COMMIT
。这应该会使您的代码速度提高 10-20 倍左右。
关于python - 哪个更好-execute(INSERT) 或executemany(INSERT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51237507/
如何获得这些 SQL 语句序列?上类?我以前只处理过单个 select 语句,而 cursor.execute 工作得很好。我现在不知道在这种情况下该怎么办。我收到的错误是格式需要映射 args =
我正在使用 Python 及其 MySQLdb 模块将一些测量数据导入 Mysql 数据库。我们拥有的数据量非常大(目前大约有 250 MB 的 csv 文件, future 还会有更多)。 目前我使
使用以下函数: import pyodbc def execute_side_effect_stmt(sql_stmt: str, params: list): with get_connec
我可以使用以下代码迭代 python 对象,但是我希望能够使用占位符作为架构和表名称,通常我使用 {}.{} 执行此操作.format() 方法,但是如何将这两个方法结合起来? cur.execute
我正在使用Python中的MySQLdb模块来编写SQL语句。我很难以我想要的方式使用变量。这是我的工作: stmt = ''' INSERT INTO Table1 (name, status) S
如果我跑 cur.execute('SELECT * FROM some_table WHERE some_column = %s;', ('some_value',)) 然后立即调用 cur.fet
我正在使用 Python 及其 MySQLdb 模块,是否可以从条件中的元组/字典/列表中执行“selectmany”之类的操作 像这样: cursor.executemany("""SELECT *
我正在使用 Python MySQLdb 将数据插入 mysql 数据库。 InsertList 包含许多行。除了少数违反数据库完整性规则外,其他都是有效的。如果我运行下面的代码,命令会返回错误。 C
我有以下功能,可以批量插入 IPv6 IP 列表 def insertToDb(ipList): print("OK") try: conn = psycopg2.connect("d
我想按如下方式从 MySQL 中选择值 do_not_select = [1,2,3] cursor = database.cursor() cursor.executemany("""SELECT
我正在使用 oracledb (4.0.1) 和 Node (v10.14.2)。我在下面有 json 列表; [ { DIFFID: 8, DIFFDATE: 2019-11-01T14:0
我在下面的代码中找不到我的错误。当它运行时,出现类型错误:cur.executemany(sql % itr.next()) => 'function takes exactly 2 argument
我正在尝试将 psycopg2 executemany 用于简单的多插入,但我只能使用 dict 而不是“普通”值序列使其工作: # given: values = [1, 2, 3] ; curso
我正在使用 MySQLdb 模块插入到我的数据库中。我有以下代码 sqlString = """INSERT INTO PERFORMER (PERFORMER)
我有一个由名为 member 的字典组成的列表,我像这样插入到数据库中 # Executes query for each dictionary in member. cursor.e
在这种情况下,我创建了一个方法来在数据库中插入行。我向该方法提供列、值和表名。 COLUMNS = [['NAME','SURNAME','AGE'],['SURNAME','NAME','AGE']
我想在我的程序中使用 executemany 一次存储 20 条记录,这是文档中的内容... c.executemany( """INSERT INTO breakfast (name, spam
我正在尝试使用 pgdb 模块执行从 Python 到 PostgreSQL 的 insert 语句。 我看到文档说: cursor.executemany(query, list of pa
如果我运行 SQL 命令 INSERT INTO my_table VALUES (?); 有约束 CREATE my_table( user_name VARCHAR(255) PRIMAR
我使用 Django cursor.execuremany 有一段时间了,我想做一些准备工作。 在这个 Stackoverflow 页面上,我读到使用字符串格式化运算符和问号是个坏主意。 How to
我是一名优秀的程序员,十分优秀!