简体   繁体   English

mysql通过python插入表

[英]mysql insert table by python

I just want to insert the table list into the mysql database table. 我只想将表列表插入mysql数据库表中。 Here's the code: 这是代码:

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)

After running, it keeps showed that "mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement". 运行后,它一直显示“ mysql.connector.errors.ProgrammingError:SQL语句中未使用所有参数”。 Can some one help me? 有人能帮我吗?

Some of your lists - a and c - have more than one value, but you're only using one value in your statements (%s) . 您的某些列表ac具有多个值,但是您在语句(%s)仅使用一个值。 This is considered an error since it's unclear if the extra parameters were intentional or not. 这是一个错误,因为尚不清楚额外的参数是否是有意的。

If you intend to insert one row per element in the source arrays, use executemany and pass in a sequence of parameters: 如果打算在源数组中的每个元素上插入一行,请使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM