[Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
事情是这种:
博主尝试用Python的sqlite3数据库存放加密后的usernamepassword信息,表是这种
CREATE TABLE IF NOT EXISTS user
(
userID INTEGER PRIMARY KEY AUTOINCREMENT,
userStudentID BLOB NOT NULL UNIQUE ON CONFLICT IGNORE,
userPassword BLOB NOT NULL
);
当中userStudentID and UserPassword 储存成了BLOB类型,作为二进制存储。
但当博主把加密后的字节串插入数据库时。却报出例如以下错误:
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,这种话博主的密文就会被编码存放(如utf-8),而假设有些字节无法按utf-8编码的话,就会引发异常或被忽略。
网上搜了好多文章,均没有解决博主的问题。
后来还是找到了Official Document
https://docs.python.org/2/library/sqlite3.html#module-sqlite3
原来Python中与sqlite3的BLOB相应的数据类型为buffer,博主惊出一身冷汗,忙看了下自己的插入部分的代码:
def insertUsernameAndPasswordToDB(conn, cu, username, password):
username = encrypt(username)
password = encrypt(password)
cu.execute("INSERT INTO user(userStudentID, userPassword) VALUES (?,?)", (username, password) )
conn.commit()
測试了下username和password的数据类型
print isinstance(username, str)
print isinstance(password, str)
结果均为True。怪不得sqlite3尝试把他们按字符串的形式存储。这里又涉及一个知识,sqlite3用的是动态的数据类型系统。它会依照数据的值来尝试将数据转换成数据库内部的标准类型。这里它就尝试将我的字节串密文转换成字符串。
參考资料:http://www.cnblogs.com/kfqcome/archive/2011/06/27/2137000.html
将username和password转换成buffer类型,问题解决。
def insertUsernameAndPasswordToDB(conn, cu, username, password):
username = encrypt(username)
password = encrypt(password)
cu.execute("INSERT INTO user(userStudentID, userPassword) VALUES (? ,?)", (buffer(username), buffer(password)) )
conn.commit()
微博: wvr=5">@浙大宋博
[Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)的更多相关文章
- python django中使用sqlite3数据库 存储二进制数据ByteArray
在python中使用sqlite3数据库存储二进制流数据ByteArray,在django使用sqlite3数据库时,有时候也要注意最好使用二进制流ByteArray插入字符串. 使用ByteArra ...
- python+sqlite3
一个小例子, # -*- coding:utf-8 -*- ''' Created on 2015年10月8日 (1.1)Python 2.7 Tutorial Pt 12 SQLite - http ...
- python sqlite3使用
python sqlite3文档地址:http://docs.python.org/2/library/sqlite3.html The sqlite3 module was written by G ...
- python sqlite3 数据库操作
python sqlite3 数据库操作 SQLite3是python的内置模块,是一款非常小巧的嵌入式开源数据库软件. 1. 导入Python SQLite数据库模块 import sqlite3 ...
- python sqlite3 入门 (视频讲座)
python sqlite3 入门 (视频讲座) an SQLite mini-series! - Simple Databases with Python 播放列表: YouTube https:/ ...
- C语言和Python语言在存储变量方面的不同
C语言和Python语言在存储变量方面的不同 众所周知,Python是脚本语言,边解释边执行,而C语言是编译型语言 存储变量: C语言定义变量,变量本身代表的就是大小,任何一个字母或者数字 符号均可以 ...
- 初学Python写二进制文件
初学Python写二进制文件 把一个图片的16进制数据保存到一个txt文本,从这个txt文本读出并保存为二进制文件jpg图片文件.说明:图片读出的0xff粘贴ff到文本中,读出时是字符串的”ff”. ...
- python sqlite3简单操作
python sqlite3简单操作(原创)import sqlite3class CsqliteTable: def __init__(self): pass def linkSqlite3(sel ...
- 用Python将二进制文件转化为数组并以文件形式存储
最近在学习Python,发现Python语言非常适合文件的批处理操作.本文将介绍一下用Python如何实现将一个二进制bin文件转化为一个常量数组的.c文件存储起来.这为我们在一些没有文件系统不能调用 ...
随机推荐
- Java的wait(), notify()和notifyAll()使用心得(转)
本篇文章是对java的 wait(),notify(),notifyAll()进行了详细的分析介绍,需要的朋友参考下wait(),notify()和notifyAll()都是java.lang.Obj ...
- 【翻译】Why JavaScript Is and Will Continue to Be the First Choice of Programmers
花费2半小时,那么最终会被翻译.假设有问题,请提出,毕竟,自己的6不超过级别. 附加链接 Why JavaScript Is and Will Continue to Be the First Cho ...
- UINavigationController的横屏问题
近期用代码创建了一个UINavigationController,并且当前的屏幕设置为横屏的,此时遇到的问题是UINavigationController的view的大小为宽768 高1024,也就是 ...
- HotSpot关联规则算法(2)-- 挖掘连续型和离散型数据
本篇代码可在 http://download.csdn.net/detail/fansy1990/8502323下载. 前篇<HotSpot关联规则算法(1)-- 挖掘离散型数据>分析了离 ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- REDGATE又一好用的脚本工具ScriptsManager1.3
原文:REDGATE又一好用的脚本工具ScriptsManager1.3 REDGATE又一好用的脚本工具ScriptsManager1.3 先说明一下:这个工具是免费的 下载地址:http://ww ...
- 【原创】poj ----- 3009 curling 2 解题报告
题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Tot ...
- 【解决】/usr/bin/ld: cannot find -lc
现象:运行gcc静态编译程序时报错: /usr/bin/ld: cannot find -lc collect2: ld returned 1 exit statusmake: *** [gcc_dr ...
- shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟
/********************************************************************* * Author : Samson * Date ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(一)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...