Skip to content Skip to sidebar Skip to footer

How To Print The Results Of A Sqlite Query In Python?

I'm trying to print the results of this SQLite query to check whether it has stored the data within the database. At the moment it just prints None. Is there a way to open the data

Solution 1:

Only SELECT queries have row sets. So, if you want to see the row you just inserted, you need to SELECT that row.

One way to select exactly the row you just inserted is by using the rowid pseudo-column. This column has unique values that are automatically generated by the database, and every INSERT statement updates a lastrowid property on the cursor. So:

cur.execute("INSERT INTO jetfighter VALUES (?)", (self.var.get(),))
cur.execute("SELECT * FROM jetfighter WHERE rowid=?", (cur.lastrowid,))
print(cur.fetchone())

This will print out something like:

('Starfighter F-104G',)

Solution 2:

Fetchone will only produce something if it follows a query. After the INSERT, add

cur.execute("SELECT * FROM jetfighter")
print(cur.fetchone())

Post a Comment for "How To Print The Results Of A Sqlite Query In Python?"