Python Postgres编码错误

Python Postgres编码错误,python,mysql,postgresql,Python,Mysql,Postgresql,我在尝试将一些表从mysql服务器复制到postgres服务器时遇到问题。下面是我当前用于连接、获取数据和写入数据的代码 my_sqlconnection = MySQLdb.connect(host='a.b.c.d', user='user', passwd='admin' ) try: pl_sqlconnect

我在尝试将一些表从mysql服务器复制到postgres服务器时遇到问题。下面是我当前用于连接、获取数据和写入数据的代码

my_sqlconnection = MySQLdb.connect(host='a.b.c.d',
                              user='user',
                              passwd='admin'
                              )
try:
pl_sqlconnection = psycopg2.connect("host='x.y.z.c' dbname='user' 
                   user='uadmin' password='uadmin'" )
except psycopg2.Error as e:
print('PSQL: Unable to connect!\n{0}')
print (e)
print (e.pgcode)
print (e.pgerror)
print (traceback.format_exc())
sys.exit(1)
cursor1 = my_sqlconnection.cursor(MySQLdb.cursors.DictCursor)
cursor2 = pl_sqlconnection.cursor()
for db in db_list.db_set:
    restaurant_name = db[:-3]
    my_sqlconnection.select_db(db)

    sql = "SELECT * FROM Orders"
    cursor1.execute(sql)
    for row in cursor1:
    try:
       cursor2.execute("INSERT INTO Orders (all the values) Values
       (%(all the values)s)", row)
    except psycopg2.Error as e:
        print ("cannot execute that query!!")
        print (e)
        print (e.pgcode)
        print (e.pgerror)
        print (traceback.format_exc())
        sys.exit("Some problem occured with that query! leaving early")
    sql2 = "SELECT * FROM USERS"
    cursor1.execute(sql2)
    for row in cursor1:
    try:
       cursor2.execute("INSERT INTO Users (all the values) Values
       (%(all the values)s)", row)
    except psycopg2.Error as e:
        print ("cannot execute that query!!")
        print (e)
        print (e.pgcode)
        print (e.pgerror)
        print (traceback.format_exc())
        sys.exit("Some problem occured with that query! leaving early")
cursor1.close()
cursor2.close()

pl_sqlconnection.commit()

my_sqlconnection.close()
pl_sqlconnection.close()
现在我得到的错误是

python backup.py
cannot execute that query!!
invalid byte sequence for encoding "UTF8": 0xeef1e5
HINT:  This error can also happen if the byte sequence does not match   the encoding expected by the server, which is controlled by "client_encoding".
22021 错误:编码“UTF8”的字节序列无效:0xeef1e5 提示:如果字节序列不匹配,也可能发生此错误 服务器所需的编码,由“客户端编码”控制


当我试图执行第二个查询时,会特别显示此错误。当我只运行第一个查询时,一切都按预期运行。这两个表都存在于同一数据库中。为什么在执行第二个查询时显示编码错误。

似乎目标数据库(Postgres)具有字符集UTF-8,但源数据库(或表)具有不同的编码。要小心,因为在MySQL中,整个数据库和单个表可能有不同的字符集。也许看看这里,PS这是如何在MySQL中转换字符集的,可能也很方便。