python MySQLdb在尝试插入到表中时得到无效语法
我一直在犯这个错误python MySQLdb在尝试插入到表中时得到无效语法,python,mysql,insert,Python,Mysql,Insert,我一直在犯这个错误 ## COMMENT OUT below just for reference "" cursor.execute (""" CREATE TABLE yellowpages ( business_id BIGINT(20) NOT NULL AUTO_INCREMENT, categories_name VARCHAR(255), business_name VARCHAR(500) NOT NUL
## COMMENT OUT below just for reference
""
cursor.execute ("""
CREATE TABLE yellowpages
(
business_id BIGINT(20) NOT NULL AUTO_INCREMENT,
categories_name VARCHAR(255),
business_name VARCHAR(500) NOT NULL,
business_address1 VARCHAR(500),
business_city VARCHAR(255),
business_state VARCHAR(255),
business_zipcode VARCHAR(255),
phone_number1 VARCHAR(255),
website1 VARCHAR(1000),
website2 VARCHAR(1000),
created_date datetime,
modified_date datetime,
PRIMARY KEY(business_id)
)
""")
""
## TOP COMMENT OUT (just for reference)
## code
website1g = "http://www.triman.com"
business_nameg = "Triman Sales Inc"
business_address1g = "510 E Airline Way"
business_cityg = "Gardena"
business_stateg = "CA"
business_zipcodeg = "90248"
phone_number1g = "(310) 323-5410"
phone_number2g = ""
website2g = ""
cursor.execute ("""
INSERT INTO yellowpages(categories_name, business_name, business_address1, business_city, business_state, business_zipcode, phone_number1, website1, website2)
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')
""", (''gas-stations'', business_nameg, business_address1g, business_cityg, business_stateg, business_zipcodeg, phone_number1g, website1g, website2g))
cursor.close()
conn.close()
知道为什么吗
提前谢谢你的帮助
更新#2,我删除了“类别名称”上的双单引号,但现在甚至
File "testdb.py", line 51
""", (''gas-stations'', business_nameg, business_address1g, business_cityg, business_stateg, business_zipcodeg, phone_number1g, website1g, website2g))
^
SyntaxError: invalid syntax
还是有这个错误
import MySQLdb
conn = MySQLdb.connect(host="localhost",port=22008,user="cholestoff",passwd="whoami",db="mydatabase")
cursor = conn.cursor()
## Find mysql version
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone()
print "server version:", row[0]
website1g = "http://www.triman.com"
business_nameg = "Triman Sales Inc"
business_address1g = "510 E Airline Way"
business_cityg = "Gardena"
business_stateg = "CA"
business_zipcodeg = "90248"
phone_number1g = "(310) 323-5410"
phone_number2g = ""
cursor.execute ("""
INSERT INTO yellowpages(categories_name, business_name)
VALUES ('%s','%s')
""", ('gas-stations', business_nameg))
cursor.close()
conn.close()
服务器版本:5.1.33-community
回溯(最近一次呼叫最后一次):
文件“testdb.py”,第23行,在
“,(“加油站”,商业名称)
文件“C:\Python26\lib\site packages\MySQLdb\cursors.py”,第173行,在execute中
errorhandler(self、exc、value)
defaulterrorhandler中第36行的文件“C:\Python26\lib\site packages\MySQLdb\connections.py”
提高errorclass,errorvalue
_mysql_exceptions.ProgrammingError:(1064,“您的SQL语法有错误;
检查与您的MySQL服务器版本对应的手册,以了解要使用的正确语法
“加油站”附近,2号线的“Triman销售公司”)
再次感谢您的帮助我想您的问题在于:
server version: 5.1.33-community
Traceback (most recent call last):
File "testdb.py", line 23, in <module>
""", ('gas-stations', business_nameg))
File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 173, in execute
self.errorhandler(self, exc, value)
File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_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 'gas-stations'',''Triman Sales Inc'')' at line 2")
这会导致语法错误。您可能希望使用一组引号:
''gas-stations''
如果要将值“gas-stations”
插入包含引号的数据库,则可以转义引号或用双引号而不是单引号将字符串括起来:
'gas-stations'
“向上箭头”指向错误位置的原因是因为您的线条太长,以至于缠绕在控制台上。请缩短线条,或拓宽控制台窗口以查看错误的真正位置。您不能使用双单引号(即
“加油站”
)-只使用单引号('gas-stations'
)或实际的双引号('gas-stations'
)。对于第二个问题,您需要丢失VALUES子句中的所有单引号字符…看起来应该像值(%s,%s)
而不是值('%s','%s')
一般规则非常简单:对于每个参数,都有一个位置标记(对于mySQLdb,这是%s
)在SQL语句中,在参数元组中提供一个Python表达式。然后向后倾斜,让接口软件为您做正确的事情。这包括正确引用字符串。不要自己尝试这样做。特别是,字符串表达式应该正是您希望稍后检索的内容
示例:加油站的
business\u name
是作为Python字符串常量的“O'Reilly's Pump'n'Go”。这将在构造的SQL中作为…值(…,'O'Reilly's Pump'n'Go',…
结束,而您不必考虑它。我也遇到了这样的错误,我通过将值下的“%s”替换为%s来解决它
"'gas-stations'"
VALUES(%s,%s)