Python Forum
Basic SQL query using Py: Inserting or querying sqlite3 database not returning data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic SQL query using Py: Inserting or querying sqlite3 database not returning data
#1
Hello, can someone assist here please:
When I run the program below, I expect table to be created and record inserted into the database.
However when I query database, it displays no records.

Please let me know if you can point out what is missing here from either query or insert operation.

Thank you,
Marlon

=====
import sqlite3
def insert():


	connection_obj = sqlite3.connect('apix.db')
	cursor_obj = connection_obj.cursor()
	cursor_obj.execute("DROP TABLE IF EXISTS APIX")
	table = """ CREATE TABLE APIX (
				IP VARCHAR(255) NOT NULL,
				DATE CHAR(8) NOT NULL
			); """
	
	cursor_obj.execute(table)
	print("Table is Ready. Now I will insert...")
	connection_obj.execute ("""INSERT INTO APIX (IP,Date) VALUES ("1.1.1.1","03/03/01")""")
	connection_obj.close()


def query():

	connection_obj = sqlite3.connect('apix.db')
	cursor_obj = connection_obj.cursor()
	statement = '''SELECT * FROM APIX'''
	cursor_obj.execute(statement)
	output = cursor_obj.fetchall()
	for row in output:
  		print(row)
  
	connection_obj.commit()
	connection_obj.close()
	return output

insert()
print(query())
(venv) user@3c0630078aad venv % python delmain.py
Table is Ready. Now I will insert...
[]
(venv) user@3c0630078aad venv %
Reply
#2
Please use proper bbcode when posing to keep the format.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
You need to commit in the insert() (i.e. after changing the DB - CREATE, INSERT, etc.) , not in query() (i.e. SELECT - what do you commit after selecting?)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
My bad, once I posted, I verified the commit in the wrong code block. Thanks all for the replies.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 464 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  I think I need to delete input data because returning to start fails thelad 2 1,130 Sep-24-2024, 10:12 AM
Last Post: thelad
  Python script to extract data from API to database melpys 0 895 Aug-12-2024, 05:53 PM
Last Post: melpys
  Create SQLite3 database with peewee Jim53_1980 2 2,113 Dec-20-2023, 02:38 PM
Last Post: buran
  Regex replace in SQLite3 database WJSwan 1 1,522 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Returning Column and Row Data From Spreadsheet knight2000 0 1,119 Oct-22-2023, 07:07 AM
Last Post: knight2000
  How to detect abnormal data in big database python vanphuht91 5 2,423 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 4,516 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Database that can compress a column, or all data, automatically? Calab 3 2,182 May-22-2023, 03:25 AM
Last Post: Calab
  Same Data Showing Several Times With Beautifulsoup Query eddywinch82 2 2,060 May-29-2022, 11:46 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020