I'm trying to write a python program that takes parser arguments from users and adds it to specific column in the SQLite database

Question:

I’m trying to write a python script that multiple users will execute to add their info. My project is to gather as much data from many users and eventually query base specific demographic of users input.(For example query data based on INSTITUTION of all the users went to.)

I have part of the code that prompts args from user, however I don’t know how handle the data into the specific column in the SQLite db.


import argparse
import sys
import sqlite3

def main():

    # Creating a parser object
    parser = argparse.ArgumentParser(description = "This program will read users Background data into SQLite Table.")
    
    # Adding argument
    parser.add_argument("--EDUCATIONLEVEL", help = "Adds to database EDUCATIONLEVE Column. Where user insert their Education level")
    parser.add_argument("--INSTITUTION", help = "Adds to database INSTITUTION Column. Where user insert their Education Instution")
    parser.add_argument("--FIELD", help = "Adds to database FIELD Column. >> >> Field of study")
    parser.add_argument("--DATE", help = "Adds to database DATE Column. >> >> Date of degree")
    parser.add_argument("--CAREER", help = "Adds to database CAREER Column. >> >> current Career")
    

    # Parse the arguments from standard input
    args= parser.parse_args()

    # Database connection
    conn = sqlite3.connect("Background.db")  
    cur = conn.cursor() 
    
    # create table function 
    def CreateTable():
        cmd =  """CREATE TABLE IF NOT EXISTS BACKGROUND
                (EDUCATIONLEVEL TEXT,
                INSTITUTION TEXT,
                FIELD TEXT,
                DATE INTEGER,
                CAREER TEXT,
                )"""
        cur.execute(cmd)
        conn.commit()
        
    def InsertToTable():
        cmd = """INSERT OR IGNORE INTO BACKGROUND(EDUCATIONLEVEL, INSTITUTION, FIELD, DATE, CAREER) VALUES (?, ?, ?, ?, ?)""", (Education_Level, Institution, Field, Date, Career)
        cur.execute(cmd)
        conn.commit()
        
      
        
    # invoking creation of BACKGROUND Table
    CreateTable()
    
    # if user input is < 1 invoke help
    if len(sys.argv) < 1:
        parser.print_help()
     
# I want to be able to add data to specific column in the db
    for arg in vars (args):
        if arg == None:
            print("No input specified!")
            parser.print_help()
        
        elif arg == "EDUCATIONLEVEL":
           
        elif arg == "INSTITUTION":
        
        elif arg == "FIELD":
        
        elif arg == "DATE":
            
        elif arg == "CAREER":
                   
        else:
            parser.print_help()
    
    # close connection to the database
    conn.commit()
    conn.close()
    
if __name__ == "__main__":
    main() 
    

Asked By: voyager 1

||

Answers:

Just set all your variables from args:

Education_Level = args.EDUCATIONLEVEL
Institution = args.INSTITUTION
# and so on

Then your cursor.execute() will work as written.

Answered By: Barmar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.