2016-10-11 1 views
-1

여기 내 코드입니다 :이와 무슨 일이 있었는지 모르겠다 "pymysql.err.ProgrammingError : (1064 ..."

from urllib.request import urlopen 
from bs4 import BeautifulSoup as bs 
import re 
import pymysql 

resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8") 

soup = bs(resp ,"html.parser") 

listUrls = soup.findAll("a", href=re.compile("^/wiki/")) 

for url in listUrls: 

     if not re.search('\.(jpg|JPG)$', url['href']): 

      conn = pymysql.connect(
          host='127.0.0.1', 
          user='root', 
          password='', 
          db='wikiurl', 
          charset='utf8mb4' 
          ) 
      try: 
       with conn.cursor() as cursor: 

        sql = "insert into 'wikiurl'('urlname','urlhref') VALUES (%s , %s)" 
        cursor.execute(sql,(url.get_text(), "https://en.wikipedia.org" + url["href"])) 
        conn.commit() 

      finally: 
        conn.close() 

오류 : 모든

pymysql.err.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 ''wikiurl'('urlname','urlhref') VALUES ('Wikipedia' , ' https://en.wikipedia.org/w ' at line 1")

답변

0

첫째, 나는 세부 사항에 공백을 최대한주의를주는 것이 좋습니다

을이 시도해보십시오.

sql = "INSERT INTO wikiurl (urlname, urlhref) VALUES (%s, %s)" 

테이블 이름 주변에는 작은 따옴표가 필요하지 않습니다. 참조 : MySQL Insert documentation.

편집 : 그리고 열 이름 주변에는 따옴표가 필요하지 않습니다.

0

SQL 구문에 약간의 오류가 있지만 디버깅하기가 쉽지 않다고 생각됩니다.

mogrify(self, query, args=None)

'' '데이터베이스로 전송됩니다 정확한 문자열을 반환

난 당신이 위의 MySQL의 server.pymysql 설명서로 전송되는 실제 SQL 문자열이 무엇을 인쇄하려면이 방법을 사용하는 것이 좋습니다 execute() 메서드를 호출하여 호출합니다. 이 방법은 Psycopg 다음에 DB의 API 2.0의 확장을 따라 '' ' 는 예 :.

당신이

print cursor.mogrify(sql,(url.get_text(), "https://en.wikipedia.org" + url["href"]))

행운을 사용할 수 있습니다!

관련 문제