【问题标题】:Sqlite in python - print after every insertpython中的Sqlite - 每次插入后打印
【发布时间】:2021-06-29 13:19:34
【问题描述】:
con=sqlite3.connect('database')
cursor=con.cursor()
sql=[("1", "first", "New"),("2", "second", "New"),("3", "third", "New")]
cursor.executemany("""insert into table(srnumber, order, type)
              values(?,?,?)""",sql)
for row in cursor.execute('select * from table WHERE type ="New"'):
    print(row)

这只会打印所有新插入的记录一次。我希望能够在每次插入后进行打印。

第一次插入后:

第一个新的

第二次插入后:

第一个新的

2 秒新

【问题讨论】:

    标签: sql python-3.x database sqlite execute


    【解决方案1】:

    您必须使用execute() 而不是executemany() 一次插入一行,并且在插入每一行后,您必须执行SELECT... 语句:

    rows = [("1", "first", "New"),("2", "second", "New"),("3", "third", "New")]
    sql_insert = "INSERT INTO tablename(srnumber, `order`, type) VALUES (?, ?, ?)"
    sql_select = "SELECT * FROM tablename WHERE type = 'New'"
    for row in rows:
        cursor.execute(sql_insert, row)
        for inserted_row in cursor.execute(sql_select):
            print(inserted_row)
    

    请注意,如果表中已经存在带有type = 'New' 的行,它们都将由SELECT... 语句返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      相关资源
      最近更新 更多