2010-03-27 92 views
1
## 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() 

我不断收到此错误蟒蛇MySQLdb的有试图INSERT INTO表

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

再次感谢您的帮助

回答

2

我觉得你的问题是在这里:

''gas-stations'' 

这给出了一个语法错误。你可能想用一组引号的:如果你想值'gas-stations'插入到数据库(包括引号),那么你可以逃脱引号或用双引号,而不是单引号括起来的

'gas-stations' 

"'gas-stations'" 

为什么“向上箭头”是在错误的地方指向的原因是因为你的线是这么长时间,它是你的控制台上包裹。缩短行数,或者扩大控制台窗口以查看错误的真实位置。

0

您不能使用加倍的单引号(即''gas-stations'') - 只使用单个单引号('gas-stations')或实际双引号("gas-stations")。

1

对于第二个问题,您需要在VALUES子句中丢失所有这些单引号字符......应该看起来像VALUES (%s,%s)不像VALUES ('%s','%s')

一般规则非常简单:对于每个参数,在SQL语句中有一个位置标记(在mySQLdb中为%s),并在参数元组中提供一个Python表达式。然后向后靠,让接口软件为您做正确的事情。这包括正确引用字符串。不要试图做你自己。特别是,字符串表达式应该与您期望在稍后检索完全一致。

示例:加油站的business_name是“O'Reilly's Pump'n'Go”作为Python字符串常量。这将最终在构建的SQL中作为...VALUES(...,'O''Reilly''s Pump''n''Go',...而不必考虑它。

0

我也有这样的错误,我解决了它的价值取代'%s'%s。

VALUES(%s,%s)