如何在sqlite3中插入中文字符

发布时间:2020-06-30 发布网站:脚本之家
价值2580元廖雪峰前端视频终终终于免费啦!
脚本之家收集整理的这篇文章主要介绍了如何在sqlite3中插入中文字符脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使了一下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','qq@example.com')
    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("qq@example.com")
    drop_tables(dbname)

运行时出现下面的错误:

cursor.execute("insert into userinfo(name,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,'qq@example.com')
    conn = sqlite3.connect(dbname)
    conn.text_factory = str  ##!!!
    cursor = conn.cursor()
    cursor.execute("insert into userinfo(name,)):
        print row[0]
        
if __name__ == '__main__':
    dbname = 'test.db'
    try:
        drop_tables(dbname)
    except:
        pass
    create_tables(dbname)
    insert()
    select("qq@example.com")
    drop_tables(dbname)



第二种,把插入的数据转换为unicode:

#-*-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'),)):
        print row[0].encode('utf8')
        
if __name__ == '__main__':
    dbname = 'test.db'
    try:
        drop_tables(dbname)
    except:
        pass
    create_tables(dbname)
    insert()
    select("qq@example.com")
    drop_tables(dbname)

总结

以上是脚本之家为你收集整理的如何在sqlite3中插入中文字符全部内容,希望文章能够帮你解决如何在sqlite3中插入中文字符所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入脚本之家官方QQ群:1065694478