代码之家  ›  专栏  ›  技术社区  ›  sayth

SQLITE未在ExecuteMy上插入数据

  •  0
  • sayth  · 技术社区  · 6 年前

    我正在插入一个包含两列数据的元组。我的insert没有收到任何错误,但是我看不到数据库中的数据。

    我的数据

    output = ((5153419, 5178566), (5153419, 5178568), (5153419, 5178565), (5153419, 5178562), (5153419, 5178567), (5153419, 5178563), (5153419, 5178561), (5153419, 5178564))
    

    只需在一个表数据库中进行基本的插入。

    connection = sqlite3.connect("test_database.db")
    c = connection.cursor()
    c.execute("DROP TABLE IF EXISTS Meeting")
    c.execute("CREATE TABLE Meeting(MeetingID INT, RaceID INT)")
    c.executemany("INSERT INTO Meeting VALUES(?,?)", output)
    c.execute("SELECT MeetingID, RaceID from Meeting")
    

    如果我用fetchall数据返回检查数据。

    for row in c.fetchall():
        print(row)
    
    ## Output
    
    [saythrenshaw@localhost racing]$ /usr/bin/python3 /home/saythrenshaw/racing/parse_nsw.py
    (5153419, 5178566)
    (5153419, 5178568)
    (5153419, 5178565)
    (5153419, 5178562)
    (5153419, 5178567)
    (5153419, 5178563)
    (5153419, 5178561)
    (5153419, 5178564)
    

    困惑为什么如果它从游标连接返回,为什么SQLITE或DBeaver的DB Browser无法在我的表中看到任何数据。 数据是否真的存在,或者连接是否以某种方式“保存着数据”。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Pedro Duque    3 年前

    在代码末尾添加提交。

    connection = sqlite3.connect("test_database.db")
    c = connection.cursor()
    c.execute("DROP TABLE IF EXISTS Meeting")
    c.execute("CREATE TABLE Meeting(MeetingID INT, RaceID INT)")
    c.executemany("INSERT INTO Meeting VALUES(?,?)", output)
    c.execute("SELECT MeetingID, RaceID from Meeting")
    connection.commit()