我正在尝试使用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)