sqlite3에 중국어 문자를 삽입하는 방법

4476 단어
다음과 같이 sqlite3를 사용했습니다.
#-*-encoding:utf-8-*-
import sqlite3

def create_tables(dbname):  
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''create table userinfo(name text, email text)''')
    conn.commit()
    cursor.close()
    conn.close()
    
def drop_tables(dbname):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''drop table userinfo''')
    conn.commit()
    cursor.close()
    conn.close()

def insert():
    users = (' qq', '[email protected]')
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute("insert into userinfo(name, email) values(?, ?)", users)
    conn.commit()
    cursor.close()
    conn.close()
    
def select(text):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    print "select name from userinfo where email='%s'" % text
    for row in cursor.execute("select name from userinfo where email= ? ", (text,)):
        print row[0]
        
if __name__ == '__main__':
    dbname = 'test.db'
    try:
        drop_tables(dbname)
    except:
        pass
    create_tables(dbname)
    insert()
    select("[email protected]")
    drop_tables(dbname)

실행 중 다음 오류가 발생했습니다.
cursor.execute("insert into userinfo(name, email) values(?, ?)", users)
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

두 가지 방법으로 해결할 수 있습니다.
첫 번째, text_ 설정factory = str
#-*-encoding:utf-8-*-

import sqlite3

def create_tables(dbname):  
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''create table userinfo(name text, email text)''')
    conn.commit()
    cursor.close()
    conn.close()
    
def drop_tables(dbname):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''drop table userinfo''')
    conn.commit()
    cursor.close()
    conn.close()

def insert():
    users = (' qq', '[email protected]')
    conn = sqlite3.connect(dbname)
    conn.text_factory = str  ##!!!
    cursor = conn.cursor()
    cursor.execute("insert into userinfo(name, email) values(?, ?)", users)
    conn.commit()
    cursor.close()
    conn.close()
    
def select(text):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    print "select name from userinfo where email='%s'" % text
    for row in cursor.execute("select name from userinfo where email= ? ", (text,)):
        print row[0]
        
if __name__ == '__main__':
    dbname = 'test.db'
    try:
        drop_tables(dbname)
    except:
        pass
    create_tables(dbname)
    insert()
    select("[email protected]")
    drop_tables(dbname)

두 번째, 삽입된 데이터를 유니코드로 변환합니다.
#-*-encoding:utf-8-*-

import sqlite3

def create_tables(dbname):  
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''create table userinfo(name text, email text)''')
    conn.commit()
    cursor.close()
    conn.close()
    
def drop_tables(dbname):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''drop table userinfo''')
    conn.commit()
    cursor.close()
    conn.close()

def insert():
    users = (' qq'.decode('utf8'), '[email protected]')
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute("insert into userinfo(name, email) values(?, ?)", users)
    conn.commit()
    cursor.close()
    conn.close()
    
def select(text):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    print "select name from userinfo where email='%s'" % text
    for row in cursor.execute("select name from userinfo where email= ? ", (text,)):
        print row[0].encode('utf8')
        
if __name__ == '__main__':
    dbname = 'test.db'
    try:
        drop_tables(dbname)
    except:
        pass
    create_tables(dbname)
    insert()
    select("[email protected]")
    drop_tables(dbname)

좋은 웹페이지 즐겨찾기