python - Django --- Could not decode to UTF-8 column
PHP中文网
PHP中文网 2017-04-17 15:34:13
[Python讨论组]

我在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

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

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(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稍有不同,让我尝试了很久终于做出来啦,开心

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号