2016-11-07 6 views
0

PostgreSQL 테이블에 복사하려는 MySQL 테이블에 데이터가 있습니다. MySQL은 데이터 : : 내 코드를 실행하면MySQL에서 LONGTEXT를 Python을 사용하는 PostgreSQL의 CITEXT에 복사하십시오.

enter image description here

내가 얻을 :

ProgrammingError: ERROR: syntax error at or near "t" 
모든 것은 MySQL이 " 및/또는 ' 예를 들어

문자열을 포함하는 경우를 제외하고 작동

(할 수 없음)

postgre = pg.connect(dbname=DB,user=USR,passwd=PASSWD,host=HOST, port=PORT) 
crs = db_remote.cursor(MySQLdb.cursors.DictCursor) 
crs.execute ("""select post_id, post_excerpt from tabl""") 
data = crs.fetchall() 
for row in data : 
    postgre.query("""INSERT INTO importfrommysql(id,message) 
    VALUES ('%s','%s')"""%(row["post_id"],row["post_excerpt"])) 

pg.connectPygreSQL 패키지 인 연결이 내 코드입니다. 어떻게해야합니까? 텍스트를 그대로 얻을 수 있습니까? 또는 유일한 해결책은 삽입 전에 모두 "/'입니다.

답변

1

사용 Psycopgcursor.execute 매개 변수 전달 :

import psycopg2 
conn = psycopg2.connect(database='DB') 
cursor = conn.cursor() 

for row in data : 

    cursor.execute (""" 
     INSERT INTO importfrommysql (id,message) 
     VALUES (%s,%s) 
     """, 
     (row["post_id"],row["post_excerpt"]) 
    ) 

그것은 탈출 따옴표 필요합니다.

+0

PyGreSQL 패키지를 사용할 수없는 이유는 무엇입니까? – ban

+0

@ban 아마도 네,하지만 익숙하지는 않습니다. –

+0

이 방법은 또한 SQL 주입에 대해 안전합니까? – ban