溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在SQLite-Python中實現(xiàn)返回、查詢中文字段的方法

發(fā)布時間:2020-08-27 17:51:43 來源:腳本之家 閱讀:228 作者:Joy_917 欄目:開發(fā)技術(shù)

博主在這個問題上卡了挺久的,貼出來解決方法幫助需要的朋友,直接上代碼(測試環(huán)境:win10+Python2.7):

# coding=utf-8
 
import sqlite3
 
with sqlite3.connect(":memory:") as conn:
  try:
    init_sql = " create table test (id integer primary key ,name text(200) not null);" \
          " insert into test (name) values ('小居居');" \
          " insert into test (name) values ('大居居');"
    conn.executescript(init_sql)
  except Exception as e:
    conn.rollback()
    raise e
  else:
    conn.commit()
    conn.text_factory = str # 此處是關(guān)鍵,sqlite默認(rèn)文本存取是Unicode
    try:
      for row in conn.execute(" select * from test where name = ?",("大居居",)):
        print row[1],type(row[1])
    except Exception as e:
      raise e

結(jié)果:

大居居 <type 'str'>

以上這篇在SQLite-Python中實現(xiàn)返回、查詢中文字段的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI