如何修复错误“1064,您的SQL语法中有错误”? [英] How to fix error "1064,you have an error in your SQL syntax"?

查看:415
本文介绍了如何修复错误“1064,您的SQL语法中有错误”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张相似的桌子。唯一的区别是名称。一个名字是192.168.1.7

和orther是tt_control

我从表中写代码读取数据,这是我的代码:

  import  MySQLdb 
db_local = MySQLdb.connect( localhost root root luan_van
num = 41001662
db_local :
cur = db_local(MySQLdb.cursors.DictCursor)
cur.execute( SELECT * FROM 192.168.1.7 WHERE Card_ID ='%d'%num)
rows = cur.fetchall()
ID = 0
in 行:
ID =行[' Card_ID']
print ID



如果我在cur.execute中替换192.168.1.7(SELECT * FROM 192.168。 1.7 WHERE Card_ID ='%d'%num,带tt_control。它运作良好。怎么了?我该如何解决?

感谢您的帮助。

解决方案

假设192.168.1.7是您的表名(它看起来更多比如IP地址),试试:

 cur.execute(  SELECT * FROM [192.168.1.7] WHERE Card_ID ='%d'%num)


你必须逃避这个名字。对于MySQL,转义字符是弱重音:`,即`192.168.1.7`

除此之外,您的数据库似乎设计错误。为什么要创建一个具有IP地址名称的表,并使另一个表具有相同的列?想一想规范化!


I have two table similar. The only difference is the name. One name is 192.168.1.7
and orther is tt_control
I write code read data from table, here is my code:

import MySQLdb
db_local = MySQLdb.connect("localhost","root","root","luan_van")
num = 41001662
with db_local:
     cur = db_local(MySQLdb.cursors.DictCursor)
     cur.execute("SELECT * FROM 192.168.1.7 WHERE Card_ID = '%d'" %num)
     rows = cur.fetchall()
     ID = 0
     for row in rows:
         ID = row['Card_ID']
         print ID


If I replace "192.168.1.7" in cur.execute("SELECT * FROM 192.168.1.7 WHERE Card_ID = '%d'" %num) with "tt_control". It work well. What wrong with it? How can I fix it?
Thanks for help.

解决方案

Assuming 192.168.1.7 is your table name (and it looks a lot more like an IP address), try:

cur.execute("SELECT * FROM [192.168.1.7] WHERE Card_ID = '%d'" %num)


You must escape the name. With MySQL, the escape character is a weak accent: `, i.e. `192.168.1.7`.
Apart from that, your database seems to be wrongly designed. Why do you create a table with the name of an IP address, and have another table with the same columns? Think of normalization!


这篇关于如何修复错误“1064,您的SQL语法中有错误”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆