Python Forum
sqlite3 database does not save data across restarting the program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite3 database does not save data across restarting the program
#1
I made a class to manage a database using sqlite3, which seems to be working well, except when I restart the program the changes do not take place and I don't know what I'm doing wrong. I make the changes, commit, and then close the database. Here's the Database class:
import sqlite3 as sql


class Database:
    def __init__(self, tables, name):
        self.name = name+'.db'
        self.db = sql.connect(self.name)
        self.cursor = self.db.cursor()
        self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        db_tables = {table[0]: None for table in self.cursor.fetchall()}
        for table in db_tables:
            self.cursor.execute("SELECT * FROM %s" %table)
            db_tables[table] = [description[0] for description in self.cursor.description]
        for table, fields in tables.items():
            if table not in db_tables or len(tuple(set(fields).difference(set(db_tables[table]))) + tuple(set(db_tables[table]).difference(set(fields)))) > 0:
                self.delete_table(table, close=False)
                self.cursor.execute('''CREATE TABLE %s (%s)''' %(table, ", ".join(fields)))
        self.db.commit()
        self.db.close()

    def check_database_connection(self):
        try:
            self.cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        except sql.ProgrammingError:
            self.db = sql.connect(self.name)
            self.cursor = self.db.cursor()

    def insert_column(self, table, values, close=True):
        self.check_database_connection()
        self.cursor = self.db.cursor()
        self.cursor.execute(f"INSERT INTO {table}({','.join(list(values.keys()))}) VALUES({','.join(['?' for _ in range(len(values))])})", list(values.values()))
        self.db.commit()
        if close:
            self.db.close()

    def get_column(self, table, columns, filter='', limit='', limitOffset='', close=True):
        self.check_database_connection()
        self.cursor = self.db.cursor()
        if filter != '':
            filter = f" WHERE {filter[0]} ='{filter[1]}'"
        if limit != '':
            limit = f" LIMIT {str(limit)}"
            if limitOffset != '':
                limitOffset = f" OFFSET {str(limitOffset)}"
        self.cursor.execute(f"SELECT {', '.join(columns)} FROM {table}" + filter + limit + limitOffset)
        columns = self.cursor.fetchall()
        if close:
            self.db.close()
        return columns

    def delete_column(self, table, filter, close=True):
        self.check_database_connection()
        self.cursor = self.db.cursor()
        self.cursor.execute(f"DELETE FROM {table} WHERE {filter[0]} = '{filter[1]}'")
        self.db.commit()
        if close:
            self.db.close()

    def delete_table(self, table, close=True):
        self.check_database_connection()
        self.cursor = self.db.cursor()
        self.cursor.execute("DROP table IF EXISTS %s" %table)
        self.db.commit()
        if close:
            self.db.close()

    def update_column(self, table, columnInfo, close=True):
        self.check_database_connection()
        self.cursor = self.db.cursor()
        self.cursor.execute(f"UPDATE {table} SET {columnInfo[0]} = '{columnInfo[1]}' WHERE {columnInfo[2]} = '{columnInfo[3]}'")
        self.db.commit()
        if close:
            self.db.close()
Thank you in advance for any help.
Reply
#2
Update: I believe the problem may lie in my long if statement failing, leading to all the tables being recreated. I'm going to look through it.

I've made the if statement the following: if table not in db_tables and it works now. For now, since I'm not going to be changing the columns, I'll leave my if statement at that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create SQLite3 database with peewee Jim53_1980 2 652 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+
  how to save to multiple locations during save cubangt 1 535 Oct-23-2023, 10:16 PM
Last Post: deanhystad
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 740 Aug-09-2023, 05:51 PM
Last Post: Calab
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 618 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  How to detect abnormal data in big database python vanphuht91 5 1,109 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,641 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Database that can compress a column, or all data, automatically? Calab 3 1,155 May-22-2023, 03:25 AM
Last Post: Calab
  Pymodbus read and save to database stewietopg 3 1,844 Mar-02-2023, 09:32 AM
Last Post: stewietopg
  data file for .exe program to access ose 2 1,171 Nov-23-2022, 08:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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