0

Im using Django to develop my web service. I have a database name is 'django_dulichdb' and have one table 'polls_dulich'. I had set CHARACTER SET utf8 and COLLATE utf8_general_ci, but still have diffrenent between data on database and data when it's got by python code. That's it

After typing SHOW variables LIKE '%character_set%';, this is my charset

> | Variable_name            | Value                      |
> +--------------------------+----------------------------+ | character_set_client     | latin1                     | |
> character_set_connection | latin1                     | |
> character_set_database   | utf8                       | |
> character_set_filesystem | binary                     | |
> character_set_results    | latin1                     | |
> character_set_server     | latin1                     | |
> character_set_system     | utf8                       |

| character_sets_dir       | /usr/share/mysql/charsets/

1. In mysql, i type

select * from polls_dulich

And result is true. Its display right Vietnamese

tp://travelhanoi.com.vn/Data/ResizeImage/~/userfiles/files/diem du lich ha noi/van_mieu_quoc_tu_giamx131x99x2.jpg | Ha Noi | Văn Miếu, ban đầu là nơi học của các hoàng tử, sau mở rộng thu nhận cả các học trò giỏi trong thiên hạ. Văn Miếu Quốc Tử Giam ban đầu là nơi học của các hoàng tử, sau mở rộng thu nhận cả...

2>In terminal, i type

python select.py

to call select.py. That is select.py

import MySQLdb as mdb
con = mdb.connect('localhost', 'root', '1', 'django_dulichdb');
with con:
cur = con.cursor()
    cur.execute('SET NAMES utf8;')

    cur.execute("SELECT * FROM polls_dulich")

    for i in range(cur.rowcount - 10):

        row = cur.fetchone()
        print (i+1),row[4]

And result is like that

2 Chùa có qui hoạch tương đối gá»n gồm cổng tam quan, phương đình, hai dãy dải vÅ© và nhà thá» mẫu. Các kiến trúc này tuy má»›i được trùng tu vào năm 1954 nhưng vẫn giữ được tính cổ truyá»n trong...

I dont know why is that. Help me please. Thanks a lot :(

lex_luther_9x
  • 111
  • 1
  • 2
  • 10

1 Answers1

0

Whowww, Its very simple :D. This is answer for anyone when get trouble like this Just modify select.py:

# -*- coding: utf-8 -*-
# code above for using utf8
import MySQLdb as mdb
import sys
reload(sys)
con = mdb.connect(host='localhost',
                     user='root',
                     passwd='1',
                     db='django_dulichdb',
                     charset='latin1',
                     use_unicode=False);
with con:
    cur = con.cursor()
    cur.execute("SELECT * FROM polls_dulich")
    for i in range(cur.rowcount - 10):     
        row = cur.fetchone()
        print (i+1),row[4]

:D. May help somebody.

lex_luther_9x
  • 111
  • 1
  • 2
  • 10