Create the table on startup
    global db1
    global db1cursor
    
    #Open the database (will be created if not already present)
    db1 = sqlite3.connect(Path(ap_defines.DIRECTORY_FILES_ROOT + ap_defines.DIRECTORY_FILES_DATABASE + ap_defines.FILENAME_DATABASE_MY_DB_NAME))
    
    db1cursor = db1.cursor()
    
    #-------------------------------------
    #----- CREATE TABLES IF REQUIRED -----
    #-------------------------------------
  
    #tbl_settings
    #General app settings
    db1cursor.execute("""CREATE TABLE IF NOT EXISTS tbl_settings (
                        setting_name TEXT NOT NULL PRIMARY KEY,
                        setting_value TEXT
        )""")
    

    #<<<Add any other tables here


    db1.commit()
Accessing the table

#**********************************************
#**********************************************
#********** SETTINGS - WRITE SETTING **********
#**********************************************
#**********************************************
def settings_write_setting(setting_name, setting_value):
    
    
    db1cursor.execute("""INSERT INTO tbl_settings (
                        setting_name,
                        setting_value
                    ) VALUES (
                        ?,
                        ?
                    )
                      ON CONFLICT(setting_name) DO UPDATE SET setting_value=?;
                    """,
                    (setting_name, setting_value, setting_value));
    db1.commit()
    #if db1cursor.rowcount < 0:
    #    print("settings_write_setting FAILED");
    
    
#*********************************************
#*********************************************
#********** SETTINGS - READ SETTING **********
#*********************************************
#*********************************************
#Returns setting_value string, or "" if not found
def settings_read_setting(setting_name):
    
    db1cursor.execute("""SELECT setting_value FROM tbl_settings 
                      WHERE setting_name = ?
                      """,(setting_name,))
    row = db1cursor.fetchone()
    if row != None:
        setting_value = row[0]
    else:
        setting_value = ""
    
    return(setting_value)

    


#***********************************************
#***********************************************
#********** SETTINGS - DELETE SETTING **********
#***********************************************
#***********************************************
def settings_delete_setting(setting_name):
    
    db1cursor.execute("""DELETE FROM tbl_settings 
                      WHERE setting_name = ?
                      """,(setting_name,))
    db1.commit()
Testing its operation
    ap_database.settings_write_setting("MySetting1", "abcd")
    
    setting_value = ap_database.settings_read_setting("MySetting1")
    print("settings_read_setting 1: " + setting_value)
    
    ap_database.settings_delete_setting("MySetting1")

    setting_value = ap_database.settings_read_setting("MySetting1")
    print("settings_read_setting 2: " + setting_value)
    if (setting_value == ""):
        print("Setting was deleted")
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *