python sqlite字符串插入

ili*_*den 6 python sqlite

我正在尝试使用python将作为参数接收的字符串插入到sqlite数据库中:

    def addUser(self, name):

            cursor=self.conn.cursor()
            t = (name)
            cursor.execute("INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0);", t)
            self.conn.commit()
Run Code Online (Sandbox Code Playgroud)

我不想使用字符串连接,因为http://docs.python.org/library/sqlite3.html建议不要使用它.

但是,当我运行代码时,我得到了异常

cursor.execute("INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0);", t)
pysqlite2.dbapi2.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied
Run Code Online (Sandbox Code Playgroud)

为什么Python按字符分割字符串,有没有办法阻止它这样做?

编辑:

更改为t = (name,) 给出以下异常

print "INSERT INTO users ( unique_key, name, is_online, translate) VALUES (NULL, ?, 1, 0)" + t
exceptions.TypeError: cannot concatenate 'str' and 'tuple' objects
Run Code Online (Sandbox Code Playgroud)

Ric*_*dle 8

你需要这个:

t = (name,)
Run Code Online (Sandbox Code Playgroud)

制作单元素元组.

记住,它是逗号,它构成一个元组,而不是括号!