How to print the results of a SQLite query in python?

2024/11/19 13:18:32

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 database in a program like Microsoft Word or LibreOffice. Just to see whether it has saved the content into the database.

import tkinter as tk
import sqlite3 as lite
import sysclass GUI(tk.Frame):def __init__(self, master=None, **kwargs):tk.Frame.__init__(self, master, **kwargs)self.var = tk.StringVar()entry = tk.Entry(self, textvariable=self.var)entry.pack()btn = tk.Button(self, text='read', command=self.read_entry)btn.pack()btn = tk.Button(self, text='write', command=self.write_entry)btn.pack()def read_entry(self):#print(self.var.get())def write_entry(self):self.var.set(self.var.get())con = lite.connect('RandomThings.db')cur = con.cursor()cur.execute("CREATE TABLE jetfighter(Name TEXT)")cur.execute("INSERT INTO jetfighter VALUES (?)", (self.var.get(),))#con.commit()print (cur.fetchone())cur.close()def main():root = tk.Tk()root.geometry('200x200')win = GUI(root)win.pack()root.mainloop()if __name__ == '__main__':main()

Thank you for helping me with my problem.

Answer

Only SELECT queries have row sets.1 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',)
https://en.xdnf.cn/q/118534.html

Related Q&A

python sort strings with leading numbers alphabetically

I have a list of filenames, each of them beginning with a leading number:10_file 11_file 1_file 20_file 21_file 2_file ...I need to put it in this order:1_file 10_file 11_file 2_file 21_file 22_file ..…

Javascript is not recognizing a Flask variable

Im passing a set of variables into a Flask template, and I would like to first manipulate them with Javascript. The problem is that when I use the {{ var }} syntax, Javascript isnt recognizing it. The …

Float sum broken? [duplicate]

This question already has answers here:Is floating-point math broken?(36 answers)Closed 9 years ago.print(0.1 + 0.2 == 0.3)returnsFalseWhy?

SyntaxError: EOL while scanning string literal -Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Formatting a return value from a serial device

I am reading a value from a device over serial, and the return value has the format: [Theoretical position in mm, Encoder position in mm], for example, b\r#-0.001504,-0.001516\n I would like to format …

if Else statement inside for loop is not working [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.am using following code to check certain conditionsfor myarg in my…

Sending data back and forth from android server to python client

I have posted this few days back but now i ran into another problem after solving that one. DESCRIPTION: working on an android app written in kotlin that behaves as a server side and Python program tha…

Loop to run 4 times to try run a SQL procedure, after 4 attempts then stop

I have attempted to write some code in python to do a loop 4 times. It will fail as spTest doesnt exist. So I want to try loop again (repeated 4 times total) if it still cant find it, I want to break o…

Loops in Python 3.4.3

I apologize ahead of time for my ignorance but I have trying to code something in python that requires a question to be asked to the user and the user responds. Dependent on that response, the program …

Adding userdata on create VM operation with Python SDK for Azure

I am using Python sdk for azure creation virtual machine operation. I want some script to be executed whenever the VM starts. So, I have tried adding the custom-data while creating VM with Python. My d…