Question

1.sqlite3

import sqlite3
con=sqlite3.connect("g:\\mytest1.db")
cur=con.cursor()
cur.execute('create  table test (上市  TEXT)')
con.commit()
cur.close()
con.close()

I successfully create a test table mytest1.db ,and a chinese character name "上市" as field.

2.in mysql command console.

C:\Users\root>mysql -uroot -p   
Welcome to the MySQL monitor.  Commands end with ; or \g.  
mysql> create database mytest2;  
Query OK, 1 row affected (0.00 sec)  
mysql> use mytest2;  
Database changed  
mysql> set names "gb2312";  
Query OK, 0 rows affected (0.00 sec)  
mysql> create table  stock(上市 TEXT) ;  
Query OK, 0 rows affected (0.07 sec)  

The conclusion can be get : chinese characters can be used in mysql console.

3.pymysql

code31

import pymysql
con = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='******')
cur=con.cursor()
cur.execute("create database  if not exists mytest31")
cur.execute("use mytest31")
cur.execute('set names "gb2312" ')
cur.execute('create table  stock(上市 TEXT) ')
con.commit()

code32

import pymysql
con = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='******')
cur=con.cursor()
cur.execute("create database  if not exists mytest32")
cur.execute("use mytest32")
cur.execute('set names "gb2312" ')
cur.execute('create table  stock(上市  TEXT) ')
con.commit()

The same problem occurs

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 21-22: o rdinal not in range(256)

4.mysql-python-connect

code 41

import mysql.connector  
config={'host':'127.0.0.1', 
        'user':'root',  
        'password':'123456',  
        'port':3306 , 
        'charset':'utf8' 
        }  

con=mysql.connector.connect(**config) 
cur=con.cursor()
cur.execute("create database  if not exists mytest41")
cur.execute("use mytest41")
cur.execute('set names "gb2312" ')
str='create table  stock(上市  TEXT)'
cur.execute(str)

code 42

import mysql.connector  
config={'host':'127.0.0.1', 
        'user':'root',  
        'password':'******',  
        'port':3306 , 
        'charset':'utf8' 
        }  

con=mysql.connector.connect(**config) 
cur=con.cursor()
cur.execute("create database  if not exists mytest42")
cur.execute("use mytest42")
cur.execute('set names "gb2312" ')
str='create table  stock(上市.encode("utf-8") TEXT)'
cur.execute(str)

same errrors such as in pymysql.

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 22-23: o
rdinal not in range(256)

It is surely a bug in python mysql module that chinese characters can not be used as field name.
1.Chinese characters can be used as field name in python sqlite3 module.
2.Chinese characters can be used as field name in mysql console only if you 'set name "gb2312" '

Was it helpful?

Solution

pymysql.connect() accepts a charset argument. I have tested charset="utf8" and charset="gb2312" and both works (Python 3, PyMySQL 0.6.2). You don't need to use a "SET NAMES" query in this case.

import pymysql
con = pymysql.connect(host='127.0.0.1', port=3306,
                      user='root', passwd='******',
                      charset="utf8")
cur = con.cursor()
cur.execute("create database if not exists mytest31")
cur.execute("use mytest31")
cur.execute("create table stock(上市 TEXT)")
con.commit()

OTHER TIPS

You're encodeing when you should decode. To convert a Chinese character to a unicode character, use:

"上市".decode("GB18030")

Which is an encoding generally used for Chinese chars. latin-1 will not work as most Chinese characters are not within its scope. The GB18030 encoding should work, but if not, there are a host of other encodings you can try, like gbk or big5_hkscs (generally for encodings done within HK/China).

Unicode errors are easy to spot, they show up as u'\ufffd' (which when encoded will be a diamond with a question mark in the middle).

I hope this was helpful!

Edit: I'm somewhat confused by your comment.

>>> print type("上市")
<type 'str'>
>>> print type("上市".decode("GB18030"))
<type 'unicode'>

str.decode() returns unicode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top