Django --- Could not decode to UTF-8 column

我在Django中创建的数据库如下:

from django.db import models

# Create your models here.
class User(models.Model):
    hash_value = models.CharField(max_length=64)
    sealKey = models.CharField(max_length=600)
    encKey = models.CharField(max_length=300)

    def __unicode__(self):
        return self.hash_value

写入数据到数据库中用的是:

User.objects.create(hash_value=hash_value,sealKey=sealKey,encKey=encKey)

从数据库中读取用的是:

db = User.objects.get(hash_value__exact = hash_value)
sealKey = db.sealKey
encKey = db.encKey

然后就报错:

Could not decode to UTF-8 column 'encKey' with text ''

在网上搜了一下,看到大家说改进的方法是添加:

conn = sqlite3.connect('./db.sqlite3')
conn.text_factory = str

可是在我的代码中怎么使用这种设置呢?

阅读 5.4k
1 个回答

花了一下午,终于解决了

con = sqlite3.connect('./db.sqlite3')
con.text_factory = str
cur = con.cursor()
res = cur.execute("SELECT * FROM online_user WHERE hash_value='"+hash_value+"'").fetchone()
usealKey = res[2]
uencKey = res[3]

用最原始的查找数据库的方法就可以了
我用的数据库是Django默认的sqlite3,所以格式跟mysql稍有不同,让我尝试了很久终于做出来啦,开心

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题