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


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
  Create SQLite3 database with peewee Jim53_1980 2 661 Dec-20-2023, 02:38 PM
Last Post: buran
  Regex replace in SQLite3 database WJSwan 1 785 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Returning Column and Row Data From Spreadsheet knight2000 0 433 Oct-22-2023, 07:07 AM
Last Post: knight2000
  How to detect abnormal data in big database python vanphuht91 5 1,124 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,651 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Database that can compress a column, or all data, automatically? Calab 3 1,161 May-22-2023, 03:25 AM
Last Post: Calab
  Same Data Showing Several Times With Beautifulsoup Query eddywinch82 2 1,236 May-29-2022, 11:46 PM
Last Post: eddywinch82
  Query in sqlite database frewil 2 1,534 Feb-06-2022, 05:35 PM
Last Post: frewil
  I need help parsing through data and creating a database using beautiful soup username369 1 1,705 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,148 Jul-02-2021, 02:19 PM
Last Post: xtc14

Forum Jump:

User Panel Messages

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