python的mysql插入表

inkz8wg9  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(347)

我只想将表列表插入mysql数据库表中。代码如下:

import mysql.connector 

# Open database connection

a = [('apq3'), ('aquaporin')]
b = ["skin"]
c = ["down-regulated", "down regulation", "down regulate"]

def input_mysql(a1, b1, c1):
    db = mysql.connector.connect(user='root', password='xxxx',
                              host='localhost',
                              database='crawling')
    #sql = 'INSERT INTO crawling_result(search_content, content) VALUES(%s)' 
    sql_target_a = 'INSERT INTO a (term) VALUES(%s)'
    sql_target_b = 'INSERT INTO b (term) VALUES(%s)'
    sql_target_c = 'INSERT INTO c (term) VALUES(%s)'
    cursor=db.cursor()
    cursor.execute(sql_target_a, a1)
    cursor.execute(sql_target_b, b1)
    cursor.execute(sql_target_c, c1)
    db.commit()
    db.close()

a_content = a
b_content = b
c_content = c
input_mysql (a_content, b_content, c_content)

运行后,显示“mysql.connector.errors.programmingerror:sql语句中没有使用所有参数”。有人能帮我吗?

lvjbypge

lvjbypge1#

你的一些名单- a 以及 c -有多个值,但在语句中只使用一个值 (%s) . 这被认为是一个错误,因为不清楚额外的参数是否是故意的。
如果要在源数组中为每个元素插入一行,请使用 executemany 并传递一系列参数:

a = [('apq3',), ('aquaporin',)]
b = [('skin',)]
c = [('down-regulated',), ('down regulation',) ('down regulate',)]

[...]

# Insert two rows (one for each parameter list in `a`)

cursor.executemany('INSERT INTO a (term) VALUES(%s)', a)

相关问题