[Python]sqlite3 binary file Storage problem (BLOB) (You must "not" use 8-bit bytestrings unless ...)

Source: Internet
Author: User

The thing is this:

Bloggers try to use the Python sqlite3 database to store encrypted user name password information, the table is like this

CREATE TABLE IF not EXISTS user (UserID INTEGER PRIMARY KEY autoincrement,userstudentid BLOB not NULL UNIQUE on CONFLICT IG Nore,userpassword BLOB not NULL);

Where Userstudentid and UserPassword are stored as BLOB types, as binary storage.

But when the blogger inserts the encrypted byte string into the database, it reports the following error:

Sqlite3. Programmingerror:you must 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (l Ike text_factory = str). It is highly recommended, instead just switch your application to Unicode strings.

Obviously it treats the blogger's byte string as an encoded string. At this point it is not possible to refer to its practice, the text_factory is set to STR, so that the blogger's ciphertext will be encoded to store (such as Utf-8), and if some bytes can not be encoded by utf-8, it will throw an exception or be ignored.

Online Search a lot of articles, did not solve the problem of Bo master.

And then I found official Document.


Originally Python and sqlite3 blob corresponding to the data type of buffer, bloggers surprised out a cold sweat, busy looking at their own insert part of the code:

DEF INSERTUSERNAMEANDPASSWORDTODB (Conn, Cu, username, password): username = Encrypt (username) password = Encrypt ( Password) cu.execute ("INSERT into User (Userstudentid, UserPassword) VALUES (?,?)", (username, password)) Conn.commit ()

Tested the data types for the next username and password

Print isinstance (username, str) print isinstance (password, str)
The result is true, no wonder Sqlite3 tries to store them as strings. This involves a knowledge that Sqlite3 uses a Dynamic data type system that attempts to convert data into a standard type within the database, based on the value of the data. Here it tries to convert my byte cipher into a string.

Reference: http://www.cnblogs.com/kfqcome/archive/2011/06/27/2137000.html

The problem is solved by converting username and password to the buffer type.

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 ()

Weibo: @ zhejiang Song Bo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.