gpt4 book ai didi

Python-插入数据库表

转载 作者:行者123 更新时间:2023-11-29 15:17:48 26 4
gpt4 key购买 nike

我有一个变量,我想将其值插入到我的数据库表中。我有这个,但不起作用:

import mysql.connector
import statistics
idk = []
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT `First Name` FROM employees")

myresult = mycursor.fetchall()

for x in myresult:
idk.append(x)

y = statistics.mode(idk)
f = '( ,)'.join(y)

print(f)

mycursor.execute("INSERT INTO results (`First Name`) VALUES (%s)", f)

mydb.commit()

我收到此错误:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s)' at line 1

最佳答案

谢谢,我已经找到答案了。我只需要在 [] 中输入 f 即可。

import mysql.connector
import statistics
idk = []
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="python"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT `First Name` FROM employees")

myresult = mycursor.fetchall()

for x in myresult:
idk.append(x)

y = statistics.mode(idk)
f = '( ,)'.join(y)

print(f)

mycursor.execute("INSERT INTO results (`First Name`) VALUES (%s)", [f])

mydb.commit()

关于Python-插入数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59565174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com