将多个查询插入多个表sqlite3 python

xzlaal3s  于 2023-02-13  发布在  SQLite
关注(0)|答案(1)|浏览(185)

我的代码是这样的,但只插入一个quereis

import sqlite3
try:
    sqliteConnection = sqlite3.connect("/etc/x-ui-english/x-ui-english.db")
    cursor = sqliteConnection.cursor()
    sqlite_insert_query1 = """INSERT INTO table1 (id, enable, email)
                             VALUES ('1', '1', 'email')"""
    cursor.execute(sqlite_insert_query1)
    sqliteConnection.commit()
    sqlite_insert_query2 = """UPDATE table2 SET active = '1',
                             WHERE id = 1"""
    cursor.execute(sqlite_insert_query2)
    sqliteConnection.commit()
    print("ok")
    cursor.close()
except sqlite3.Error as error:
    print("no", error)
finally:
    if sqliteConnection:
        sqliteConnection.close()

我尝试在单独的文件中逐个测试每个查询,并测试其是否工作,但不测试一个文件中的所有查询,而只测试第一个查询

r8uurelv

r8uurelv1#

我把这个添加到第二次查询之前,并工作!

cursor = sqliteConnection.cursor()

这是工作

import sqlite3
try:
    sqliteConnection = sqlite3.connect("/etc/x-ui-english/x-ui-english.db")
    cursor = sqliteConnection.cursor()
    sqlite_insert_query1 = """INSERT INTO table1 (id, enable, email)
                             VALUES ('1', '1', 'email')"""
    cursor.execute(sqlite_insert_query1)
    sqliteConnection.commit()

    cursor = sqliteConnection.cursor()
    sqlite_insert_query2 = """UPDATE table2 SET active = '1',
                             WHERE id = 1"""
    cursor.execute(sqlite_insert_query2)
    sqliteConnection.commit()
    print("ok")
    cursor.close()
except sqlite3.Error as error:
    print("no", error)
finally:
    if sqliteConnection:
        sqliteConnection.close()

相关问题