2017-05-05 1 views
1

python 사전의 데이터를 mySql DB에 삽입하려고합니다. 하지만 난 내 SQL 쿼리와 함께 잘못 이해하지 않습니다.pymysql.err.ProgrammingError : SQL 구문에 오류가 있습니다.

나는이 오류가 무엇입니까 :

pymysql.err.ProgrammingError: (1064, u"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 ''DiedIn' ('name', 'city') VALUES ('\'Ethel_Merman\'', '\'New_York_City\\n\'')' at line 1")

이 내 코드입니다 : 도움을

import pymysql.cursors 

wasBornIn = {} 
with open("wasBornIn.txt") as f: 
for line in f: 
    (key, val) = line.split(':') 
    wasBornIn[key] = val 

diedIn = {} 
with open("diedIn.txt") as f: 
for line in f: 
    (key, val) = line.split(':') 
    diedIn[key] = val 

isLocatedIn = {} 
with open("isLocatedIn.txt") as f: 
for line in f: 
    (key, val) = line.split(':') 
    isLocatedIn[key] = val 

connection = pymysql.connect(host='********', user='******', password='******', db='*******', 
         charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) 
try: 
with connection.cursor() as cursor: 
# Create a new record 
    sql = "DROP TABLE DiedIn" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
# Create a new record 
    sql = "DROP TABLE isLocatedIn" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
# Create a new record 
    sql = "DROP TABLE BornIn" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
    sql = "CREATE TABLE `DiedIn`(`name` varchar(100) COLLATE utf8_bin NOT NULL, `city` varchar(50) COLLATE utf8_bin NOT NULL, " \ 
     "PRIMARY KEY(`name`)) ENGINE = InnoDB DEFAULT CHARSET = utf8" \ 
      " COLLATE = utf8_bin;" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
    sql = "CREATE TABLE `isLocatedIn`(`name` varchar(150) COLLATE utf8_bin NOT NULL, `location` varchar(50) COLLATE utf8_bin NOT NULL, " \ 
     "PRIMARY KEY(`name`)) ENGINE = InnoDB DEFAULT CHARSET = utf8" \ 
      " COLLATE = utf8_bin;" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
    sql = "CREATE TABLE `BornIn`(`name` varchar(100) COLLATE utf8_bin NOT NULL, `city` varchar(50) COLLATE utf8_bin NOT NULL, " \ 
     "PRIMARY KEY(`name`)) ENGINE = InnoDB DEFAULT CHARSET = utf8" \ 
      " COLLATE = utf8_bin;" 
    cursor.execute(sql) 

with connection.cursor() as cursor: 
    for key, value in diedIn.iteritems(): 
     strKey = repr(key) 
     strValue = repr(value) 
     sql = "INSERT INTO 'DiedIn' ('name', 'city') VALUES (%s, %s);" 
     cursor.execute(sql, (strKey, strValue)) 
# connection is not autocommit by default. So you must commit to save 
# your changes. 
connection.commit() 
finally: 
connection.close() 

감사합니다.

+0

('\'Ethel_Merman \ '', '\'New_York_City \\ n \ '') '그 문제를 만들고있는 자 봐. 그들을 벗어나 라. – Exprator

답변

-1

시도 :

sql = "INSERT INTO 'DiedIn' (name, city) VALUES ('%s', '%s');"