gpt4 book ai didi

python MySQLdb在尝试INSERT INTO表时得到无效语法

转载 作者:行者123 更新时间:2023-11-29 05:45:39 25 4
gpt4 key购买 nike

## 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()

我一直收到这个错误

  File "testdb.py", line 51
""", (''gas-stations'', business_nameg, business_address1g, business_cityg, business_stateg, business_zipcodeg, phone_number1g, website1g, website2g))
^
SyntaxError: invalid syntax

知道为什么吗?

提前感谢您的帮助


更新#2,我已经删除了“categories_name”上的双单引号,但现在甚至

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()

仍然有这个错误

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'"

“向上箭头”指向错误位置的原因是因为您的行太长以至于它在您的控制台上换行。缩短您的线路,或扩大您的控制台窗口以查看错误的真正位置。

关于python MySQLdb在尝试INSERT INTO表时得到无效语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2530770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com