0

I have tried getting the registered data from a database the program has created when first run.

database.py

import sqlite3
CREATE_TABLE = """CREATE TABLE IF NOT EXISTS college (
 id INTEGER PRIMARY KEY,
 FirstName TEXT,
 SecondName TEXT,
 Position TEXT);"""
INSERT_DATA = "INSERT INTO college (FirstName, SecondName, Position) VALUES (?, ?, ?);"
DISPLAY_DATA = "SELECT * FROM college WHERE Position = ?;"
def connect():
 return sqlite3.connect('databas.db')
def create_table(connection):
 with connection:
 connection.execute(CREATE_TABLE)
def insert_data(connection, FirstName, SecondName, Position):
 with connection:
 connection.execute(INSERT_DATA, (FirstName, SecondName, Position))
def request_data(connection, position):
 with connection:
 return connection.execute(DISPLAY_DATA, (position,)).fetchall()

api.py

import database
API_MENU = """
--- Student Database ---
1) Register new student
2) Delete
3) Show students
4) Register new staff
5) Delete
6) Show staff
7) >> Quit the program
Your choice: """
def main():
 connection = database.connect()
 database.create_table(connection)
 
 while (choice := input(API_MENU)) != "7":
 if choice == "1":
 FirstName = "Julius"
 SecondName = "Jessie"
 Position = "Student"
 database.insert_data(connection, FirstName, SecondName, Position)
 elif choice == "2":
 database.request_data(connection, "Student")
 elif choice == "3":
 pass
 elif choice == "4":
 pass
 elif choice == "5":
 pass
 elif choice == "6":
 pass
 else:
 print("Invalid input, please try again!")
main()

I am simply not being returned any data after I have registered the data using option 1. When running the code and choosing option 1, you should input a name, surname and position (Student / Staff) and then write that to the database (i have just defined strings for the purpose of putting the code here) and then choosing the option 2 should return the data that has "Student" as a position however it does not return any data.

asked Oct 28, 2020 at 17:20
1

1 Answer 1

0

The problem was that I was simply negligent with actually using the function print() to show the data.

database.request_data(connection, "Student")

The line above should be:

print(database.request_data(connection, "Student"))
answered Oct 29, 2020 at 9:22
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.