Heim > Fragen und Antworten > Hauptteil
info = "'INSERT INTO brush_card_record(brush_card_date, brush_card_time, card_num_6061, card_num_6654) VALUES(?,?,?,?)',('2017-05-28','12:23:32', 123, 0) „
cur.execute(info)
Dadurch wird ein Fehler gemeldet: sqlite3.OperationalError
cur.execute('INSERT INTO brush_card_record(brush_card_date, brush_card_time, card_num_6061, card_num_6654) VALUES(?,?,?,?)',('2017-05-28','12:23:32', 123, 0 ))
Das wird funktionieren.
代言2017-06-12 09:29:56
cur.execute有两个参数,一个是SQL,一个是给SQL的参数传值,你第一句用双引号括起来相当一个字符串,也就是一个参数,没有传入第二个参数
世界只因有你2017-06-12 09:29:56
info = "INSERT INTO brush_card_record(brush_card_date, brush_card_time, card_num_6061, card_num_6654) VALUES(%s,%s,%s,%s)"%('2017-05-28','12:23:32', 123, 0)
或者str.format
曾经蜡笔没有小新2017-06-12 09:29:56
sqlite中是这么定义:
class Connection(object):
""" SQLite database connection object. """
def cursor(self, *args, **kwargs): # real signature unknown
""" Return a cursor for the connection. """
pass
class Cursor(object):
""" SQLite database cursor class. """
def execute(self, *args, **kwargs): # real signature unknown
""" Executes a SQL statement. """
pass
问题中的第一种方式无法自动解包
曾经蜡笔没有小新2017-06-12 09:29:56
问题已找到,谢谢回答的朋友!
数据库表插入新数据为动态时,更好的处理方式是先生成str,然后作为参数传进cur.execute()中。
代码示例:
insert_info = '''\
INSERT INTO %s(brush_card_date, brush_card_time, card_num_6061, card_num_6654) \
VALUES("%s", "%s", %s, %s)''' % (f_table_name, date, now_time, gold_6061, gold_6654)
cur.execute(insert_info)