使用Python将数组插入mysql

six*_*ro9 2 python mysql arrays insert

我正在使用40k条目构建此数组.

array = [(value1, value2, value3),(value1, value2, value3),(value1, value2, value3) .... ]
Run Code Online (Sandbox Code Playgroud)

是否可以在python中将其插入到mysql中:

cursor.execute('''INSERT IGNORE into %s VALUES *array here*''' % (table_name, array))
Run Code Online (Sandbox Code Playgroud)

我无法正确地将数组变量传递给mysql.任何帮助赞赏.

ahm*_*med 7

是的,你可以用executemany做到这一点:

cursor.executemany('INSERT IGNORE into %s VALUES(%s, %s, %s)'%table_name, sql_data)
Run Code Online (Sandbox Code Playgroud)

注意:您不应该使用%将值传递给数据库,而是需要在execute/ 的第二个参数中传递它们executemany.我用于%表名,因为第一个参数是准备好的查询字符串.