python 中向sqlite 插入数据时,使用占位符出现问题
非常纠结的一个问题。先建一个表,向里面插入数据,其中包含中文字符,非常正常。
但是当用占位符替换中文字符时,就不行了。出现错误,ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 6 supplied.
第一条插入数据的代码如下:
import sqlite3
# -*- coding: utf-8 -*-
cx = sqlite3.connect('test.db')
cu = cx.cursor()
cu.execute("""create table catalog( id integer primary key, pid integer, name text UNIQUE )""")
cu.execute("insert into catalog values(10, 0,'是的')")
cx.commit()
把插入条数据的代码用变量t和占位符表示:
t='是的'
cu.execute("insert into catalog values(11, 0,?)",t)
追问:会出现错误:
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.
如果t后面不加逗号,出现错误
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.如果里面的文字'是的'已经存储到了变量中,例如变量a中存储的是'是的'
那如何操作才能使 t 达到与“t=u'是的' ”相同的效果?