python连接数据库出错import pymysql
con=pymysql.connect(host='127.0.0.1',user='root',passwd='1997520',
port=3306,db='test1')
cur=con.cursor()
list=[("52192325124","liujihong","124")]
cur.execute('insert into student(Uid,Uname,Upassword) values(%s,%s,%s)',list)
con.commit()
# cur.execute('select * from student')
# res=cur.fetchall()
# for row in res:
# print(row[0])
cur.close()
con.close()
报错如下:
Traceback (most recent call last): File "D:/Python/workspace/Pysql/Mysql.py", line 8, in <module> cur.execute('insert into student(Uid,Uname,Upassword) values(%s,%s,%s)',list) File "D:\Python\workspace\Pysql\venv\lib\site-packages\pymysql\cursors.py", line 168, in execute query = self.mogrify(query, args) File "D:\Python\workspace\Pysql\venv\lib\site-packages\pymysql\cursors.py", line 147, in mogrify query = query % self._escape_args(args, conn)TypeError: not enough arguments for format string
4 回答
已采纳
慕数据8021029
TA贡献1条经验 获得超0个赞
list
=
[(
"52192325124"
,
"liujihong"
,
"124"
)]
cur.execute(
'insert into student(Uid,Uname,Upassword) values(%s,%s,%s)'
,
list
)
改成
list
=
[
"52192325124"
,
"liujihong"
,
"124"
]
cur.execute(
'insert into student(Uid,Uname,Upassword) values(%s,%s,%s)'
,
list
)
安浪创想
TA贡献81条经验 获得超23个赞
字符串格式错误,你字符串三个变量,而实际后面只传了一个变量。你不能用一个含有三个元素的数组去代表这三个变量、
修正:
import pymysql con = pymysql.connect(host='127.0.0.1', user='root', passwd='root', port=3306, db='pytest') cur = con.cursor() list = ["52192325124", "liujihong", "124"] cur.execute('insert into student(Uid,Uname,Upassword) values(%s,%s,%s)', list) con.commit()
添加回答
举报
0/150
提交
取消