我正在尝试使用 pymysql 在 mysql 表中插入一些数据,但失败了。数据已经保存在变量中,因此我需要将它们传递给 INSERT 语句。
这就是我目前正在尝试的......
con = pymysql.connect(host='*.*.*.*', port=***, user='****',
passwd='***', db='****')
with con:
cur = con.cursor()
sql = ("INSERT INTO groupMembers (groupID, members) VALUES (%s, %s)")
data = (groupID, (x for x in membersList))
cur.executemany(sql, data)
con.commit()
con.close()
Run Code Online (Sandbox Code Playgroud)
我试图传递的数据如下所示......
组 ID = G9gh472
成员列表 = [戴夫、鲍勃、迈克、比尔、科林]
列表的长度未知,并且可能会改变结果表,我希望看起来像这样......
| groupID | members |
+---------+---------+
| G9gh472 | Dave |
| G9gh472 | Bob |
| G9gh472 | Mike |
| G9gh472 | Bill |
| G9gh472 | …Run Code Online (Sandbox Code Playgroud) 我正在尝试将psycopg2 executemany用于简单的多插入,但是我只能使用dict而不是“纯”值序列来使其工作:
# given:
values = [1, 2, 3] ; cursor = conn.cursor()
# this raises TypeError: 'int' object does not support indexing:
cursor.executemany('INSERT INTO t (col_a) VALUES ( %s )', values)
# I also tried encapsulating my 'values' into a tuple/list but it gives another exception (TypeError: not all arguments converted during string formatting).
# while this is ok:
cursor.executemany('INSERT INTO t (col_a) VALUES ( %(value)s )', [ dict(value=v) for v in values ])
Run Code Online (Sandbox Code Playgroud)
是否可以在不使用“命名”参数(%(value)s)的情况下给出“简单”值列表/元组?
我正在使用连接器/Python 将多行插入到 mysql 的临时表中。这些行都在一个列表列表中。我像这样执行插入:
cursor = connection.cursor();
batch = [[1, 'foo', 'bar'],[2, 'xyz', 'baz']]
cursor.executemany('INSERT INTO temp VALUES(?, ?, ?)', batch)
connection.commit()
Run Code Online (Sandbox Code Playgroud)
我注意到(当然还有更多的行)性能非常差。使用 SHOW PROCESSLIST,我注意到每个插入都是单独执行的。但是文档https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html说这应该优化为 1 个插入。这是怎么回事?
好的,所以我有一个函数可以根据插件的输入选择sqlite数据库中的某些行.当只涉及一个语句时,我得到了插件来选择和获取行,但由于我想为此添加一些灵活性,我尝试在遇到列表或元组时使用executemany.然而,尽管我已经摆弄和改变了所有的东西,但仍然无法使其工作,因为sqlite语句将字符串中的每个字符视为绑定,或者因为元组中的绑定太多.这是我到目前为止的代码:
def readoffset(self,offset):
vartype = type(name)
print(vartype)
if vartype == int:
self.memcursor.execute('''select all id,matbefore,matafter,name,date
from main as main where id = ?''',[offset])
undolist = self.memcursor.fetchall()
print(undolist)
return(undolist)
elif vartype == tuple or list:
print(vartype)
self.memcursor.executemany('''select all id,matbefore,matafter,name,date
from main as main where name = (?)''', [offset])
undolist = self.memcursor.fetchall()
return(undolist)
Run Code Online (Sandbox Code Playgroud) 我想使用sqlite3的executemany()插入多个值Python3。
码:
import sqlite3
conn = sqlite3.connect('rssnewsdata.db')
c = conn.cursor()
entries = [
('url1', 1234, 'title1', 'summary1', 'feedurl1'),
('url2', 1235, 'title2', 'summary2', 'feedurl2'),
('url3', 1236, 'title3', 'summary3', 'feedurl3'),
('url4', 1237, 'title4', 'summary4', 'feedurl4')
]
c.executemany('INSERT INTO entries VALUES (?, ?, ?, ?, ?)', entries)
Run Code Online (Sandbox Code Playgroud)
该db文件存在,该表存在,我可以使用Python3到SELECT来自它,所以连接到它是没有问题的。该栏目是TEXT,INTEGER,TEXT,TEXT,TEXT键入。
Python没有报告错误。缺什么?
executemany ×5
python ×3
python-3.x ×2
sqlite ×2
connector ×1
database ×1
insert ×1
mysql ×1
psycopg2 ×1
pymysql ×1
sql-insert ×1