我无法在mysql数据库中输入记录。
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "root@123",
database = "testdb",
)
print(mydb)
my_cursor = mydb.cursor()
sql_query = ("INSERT INTO users (name,email,age) VALUES (%s, %s, %s)")
record1 = ('Tim','Tim123',45)
records = [("Ankit","ankit345",30),
("shubhra","shubhra567",29),
("anita","anit789",55),]
my_cursor.execute(sql_query,records)
mydb.commit()
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "root@123",
database = "testdb",
)
print(mydb)
my_cursor = mydb.cursor()
sql_query = ("INSERT INTO users (name,email,age) VALUES (%s %s %s)")
record1 = ('Tim','Tim123',45)
my_cursor.execute(sql_query,record1)
mydb.commit()
Administrator @ DESKTOP-60U78C6 MINGW64 / c / mysql $ python db1.py 追溯(最近一次通话): 文件“ db1.py”,第15行,在 my_cursor.execute(sql_query,record1) 执行中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ mysql \ connector \ cursor.py”,行569 self._handle_result(self._connection.cmd_query(stmt)) 在cmd_query中的行553中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ mysql \ connector \ connection.py” 结果= self._handle_result(self._send_cmd(ServerCmd.QUERY,查询)) 文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ mysql \ connector \ connection.py”,行442,在_handle_result中 引发错误。get_exception(数据包) mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以在第1行的'45)附近使用正确的语法
答案 0 :(得分:1)
您在值之间缺少逗号:
sql_query = ("INSERT INTO users (name,email,age) VALUES (%s, %s, %s)")
# Here ----------------------------------------------------^---^