我在尝试将几个表从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 提示:如果字节序列不匹配,也会发生此错误 服务器期望的编码,由" client_encoding"。
控制当我尝试执行第二个查询时,会显示此错误。当我只运行第一个查询时,一切都按预期运行。这两个表都存在于同一个数据库中。为什么执行第二个查询时会显示编码错误。