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

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):您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以找到在第1行附近使用'%s)'的正确语法

最佳答案

谢谢,我已经找到了答案。我只需要在[]中输入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 - Python-插入数据库表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59565174/

10-12 21:41