1
1
101-s2015-11-13 19:51:43
Python
101-s, 2015-11-13 19:51:43

How would you solve the problem of updating two databases with different encodings in Python?

I am writing a script to transfer data from an Access database with cp1251 encoding to MySQL (utf8) . I write in PyCharm using the pyodbc library. Faced a number of problems:

  • on error PhyCharm outputs unicode output to the console pyodbc.Error: ('HY000', "[HY000] [Microsoft][\xc4\xf0\xe0\xe9\xe2\xe5\xf0 ODBC Microsoft Access] \xcd\xe5\xe2 \xee\xe7\xec\xee\xe6\xed\xee \xe8\xf1\xef\xee\xeb\xfc What is not good for understanding errors It seems to be registered in the settings of the IDE Encoding program, Project Encoding = windows-1251
  • The second gag is that you need to convert the data, is it possible to bypass this procedure somehow?

Let's say this code, from which the program displays errors due to special characters, you have to make Update queries for each row of the table being updated, convert characters to str in order to make a query ...
# -*- coding: CP1251 -*-
import pyodbc

con = pyodbc.connect("DSN=test", autocommit=True)
cur=con.cursor()
row=cur.execute("SELECT * FROM tblSiteKos WHERE ProductsId=3455").fetchone()
row=list(row)
#пример строки из базы Access
drebeden=['3455', u'kre-721', u'kre-721.jpg', u'\u041c\u044f\u0433\u043a\u043e\u0435', u'', u'', '765', 0.1, '1', '510', '25', '1', '1', u'z.html']
newlist=[]
for i in row:
    if type(i)==int:newval=str(i)
    if type(i)==unicode:newval=i.encode("cp1251")
    else: newval=i
    newlist.append(newval)
print newlist
cur.close()
con.close()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question