使用Python 3更新MySQL数据库
I'm trying to insert data from python into a MySQL database I made with phpmyadmin. I'm using Python 3. This doesn't seem to update my database - what's happening here?
我试图将数据从python插入到我用phpmyadmin做的MySQL数据库中。我使用Python 3。这似乎没有更新我的数据库——这里发生了什么?
import MySQLdb
db = MySQLdb.connect("localhost", "uname", "password", "HORSERACES")
cursor = db.cursor()
sql = """INSERT INTO HORSERACES(Name, Event participant id, Market id, Event name, Event time)
VALUES('Brimful of Asha', 1099999876, 2098574628, 'Worthing Cup Chase', 29385756568, 2017-05-30 00:00:00)"""
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
print ('Success')
The error I get is:
我得到的错误是:
_mysql_exceptions.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 'participant id, Market id, Event name, Event time)\n\tVALUES('Brimful of Asha', 10' at line 1")
_mysql_exceptions。编程错误:(1064,“您的SQL语法有错误;检查与MySQL服务器版本对应的手册,找到正确的语法,以使用接近“参与者id、市场id、事件名称、事件时间”的\n\tVALUES(“满溢的Asha”,第1行10)
1 个解决方案
#1
2
What's happening here is poor exception handling
这里发生的是糟糕的异常处理
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
# Something goes wrong and you are rolling back silently
At the very least you should do
至少你应该这么做
import traceback
traceback.print_exc()
db.rollback()
then you know why the insert isn't happening.
然后你就知道为什么插入没有发生。
Update: After you have added the traceback it's easy to see that mysql is chocking on column names. spaces are not allowed in column names. if you use them you need to escape them with backquotes, but still they don't play well on some libraries and clients.
更新:添加traceback后,很容易看到mysql正在填充列名。列名中不允许使用空格。如果您使用它们,您需要使用后引号来转义它们,但它们在某些库和客户端上仍然不能很好地发挥作用。
更多相关文章
- MySQL添加外键错误1215
- Linux第二篇----使用rpm方法安装MySQL(含各种出现的错误和排错)
- MySQL查询中的变量会导致错误
- mysql编译安装后各种常见错误集锦
- org.json Android系统错误。JSONException:在字符0处输入结束
- Navicat连接mysql8出现1251错误
- MYSQL错误代码和消息
- mySQL错误: The used table type doesn't support FULLTEXT inde
- MySQL 主从同步Out of Memory 错误分析