Python连接mysql数据库报错

import pymysql.cursors

connection = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='123456',
db='test',
charset='utf8mb4'
)
cur = connection.cursor()
sql = "insert into test(id,username,userpassword) values(%s,'%s','%s')"
# date = (5,'fffff','fffff')
cur.execute(sql, (5, 'fffff', 'fffff'))
connection.commit()
connection.close()

错误

C:\Python34\python.exe E:/pycode/test/test
Traceback (most recent call last):
File "E:/pycode/test/test", line 18, in <module>
cur.execute(sql, (5, 'fffff', 'fffff'))
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\cursors.py", line 165, in execute
result = self._query(query)
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\cursors.py", line 321, in _query
conn.query(q)
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\connections.py", line 857, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\connections.py", line 1058, in _read_query_result
result.read()
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\connections.py", line 1347, in read
first_packet = self.connection._read_packet()
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\connections.py", line 1015, in _read_packet
packet.check_error()
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\connections.py", line 395, in check_error
err.raise_mysql_exception(self._data)
File "C:\Python34\lib\site-packages\pymysql-0.7.11-py3.4.egg\pymysql\err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fffff'',''fffff'')' at line 1")

Process finished with exit code 1

Python连接mysql数据库报错
这里的意思是:数据库连不上啊。

可能是网络问题,可能是防火墙问题,可能是3306端口没开。你先排除这些问题吧。用一些mysql工具连接测试看,比如SQLyog 测试。
温馨提示:答案为网友推荐,仅供参考
相似回答