Home > OS >  With Peewee, how to check if an SQLite file has been created vs filled without creating a table. If
With Peewee, how to check if an SQLite file has been created vs filled without creating a table. If

Time:01-08

first I'd like to check if the file exists, and Ive used this os.path:

      def check_db_exist():
        try:
            file_exists = exists('games.db')
            if file_exists:
                file_size = os.path.getsize('games.db')
                if file_size > 3000:
                    return True, file_size
                else:
                    return False, 'too small'
            else:
                return False, 'does not exist'
        except:
            return False, 'error'

I have a separate file for my models, and creating the database. My concern is, if I import the class for the database it instantiates the sql file. Moreover, pywebview when displaying my html, wipes all variables. If I were to run this process as I load my page, then I can't access the variable for true/false sqlite exists.

        db = SqliteDatabase('games.db')

        class Game(Model):
            game = CharField()
            exe = CharField()
            path = CharField()
            longpath = CharField()
            i_d = IntegerField()
            
            class Meta:
                database = db

This creates the table, so checking if the file exists is useless. Then if I uncomment the first line in this file the database gest created, otherwise all of my db. variables are unusable. I must be missing a really obvious function to solve my problems.

    # db = SqliteDatabase('games.db')

    def add_game(game, exe, path, longpath, i_d):
        try:
            Game.create(game=game, exe=exe, path=path, longpath=longpath, i_d=i_d) 
        except:
            pass

    def loop_insert(lib):
        db.connect()

        for i in lib[0]:
            add_game(i.name, i.exe, i.path, i.longpath, i.id)
        db.close()



    def initial_retrieve():
        db.connect()
        vals = ''
        for games in Game.select(): 
            val = js.Import.javascript(str(games.game), str(games.exe), str(games.path), games.i_d)
            vals = vals   val

        storage = vals
        db.close() 
        return storage

should I just import the file at a different point in the file? whenever I feel comfortable? I havent seen that often so I didnt want to be improper in formatting.

edit: edit: Maybe more like this?

      def db():
      db = SqliteDatabase('games.db')
      return db 
   class Game(Model):
        game = CharField()
        exe = CharField()
        path = CharField()

file 2:

        from sqlmodel import db, Game

    def add_game(game, exe, path, longpath, i_d):
        try:
            Game.create(game=game, exe=exe, path=path, longpath=longpath, i_d=i_d) 
        except:
            pass

    def loop_insert(lib):
        db.connect()

        for i in lib[0]:
            add_game(i.name, i.exe, i.path, i.longpath, i.id)
        db.close()

CodePudding user response:

from reddit: me: To the original challenge, there's a reason I want to know whether the file exists. Maybe its flawed at the premises, I'll explain and you can fill in there.

This script will run on multiple machines I dont ahve access to. At the entry point of a first-time use case, I will be porting data from a remote location, if its the first time the script runs on that machine, its going down a different work flow than a repeated opening.

Akin to grabbing all pc programs vs appending and reading from teh last session. How would you suggest quickly understanding if that process has started and finished from a previous session.

Checking if the sqlite file is made made the most intuitive sense, and then adjusting to byte size. lmk

them: This is a good question!

  How would you suggest quickly understanding if that process 
  has started and finished from a previous session.

If the first thing your program does on a new system is download some kind of fixture data, then the way I would approach it is to load the DB file as normal, have Peewee ensure the tables exist, and then do a no-clause SELECT on one of them (either through the model, or directly on the database through the connection if you want.) If it's empty (you get no results) then you know you're on a fresh system and you need to make the remote call. If you get results (you don't need to know what they are) then you know you're not on a fresh system.

CodePudding user response:

I am not sure if this answers your question, since it seems to involve multiple processes and/or processors, but In order to check for the existence of a database file, I have used the following:

DATABASE = 'dbfile.db'
if os.path.isfile(DATABASE) is False:
  # Create the database file here
  pass
else:
  # connect to database here
  db.connect()
  • Related