Mysql In Python Encoding
This post is the same with my question in MySQL in Python: UnicodeEncodeError: 'ascii' this is just to clear things up. I am trying to save a string to a MySQL database but I get a
Solution 1:
add these parameters MySQLdb.connect(..., use_unicode=1,charset="utf8")
.
create a cursor
cur = db.cursor()
and then execute like so:
risk = m['Text']
sql = """INSERT INTO posts(nmbr, msg, tel, sts) \
VALUES (%s, %s, %s, %s)"""
values = (number, risk, 'smart', 'u')
cur.execute(sql,values) #use comma to separate sql and values, this will ensure values are escaped/sanitized
cur.commit()
now you dont need these two lines:
msg = risk.encode('utf8')
text = db.escape_string(msg)
Solution 2:
It is not clear whether your m['Text']
value is of type StringType
or UnicodeType
. My bet is that it is a byte-string (StringType
). If that's true, then adding a line m['Text'] = m['Text'].decode('UTF-8')
before your insert may work.
Post a Comment for "Mysql In Python Encoding"