Home > Back-end >  python sqlite can't find db table
python sqlite can't find db table

Time:07-23

enter image description here

I am trying to run a python script which shows all data from a table named RANK.

The python script is located at /Downloads/pythonScript/dbCompare.py
I am trying to run it from /Downloads

So I have tried to run the dbCompare.py script from /Downloads using python pythonScript/dbCompare.py 1300 and it is telling me that there is no RANK table.

After doing that, I changed my working directory from /Downloads to /Downloads/pythonScript then did it again, using python dbCompare.py 1300 command.
And it worked.

The reason why I am trying to run this command from /Downloads is I need to run this command from NodeJS server.

I also realized that after doing what I said, when I do ls command from /Downloads, an empty RANK.db file was generated newly. I don't know why.

enter image description here

I have checked my db file if there is a typo or something else but I don't see any.

How do I run this python script from /Downloads?

CodePudding user response:

change the path in sqlite3.connect('rank.db') to sqlite3.connect('relative path\rank.db') that's because of at the first connection python will search in the same dir of the script

CodePudding user response:

Check if the db exists before connect() which will create it if it doesn't.

db = '/path/to/rank.db'
if not os.path.exists(db):
    raise RuntimeError(f'{db} does not exist')
sqlite3.connect(db)
  • Related