Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sqlite3.connect - unable to open database file

Tags:

python

sqlite

I am running Python3 on my Mac testing simple sql database. I have the below code

import sqlite3

# connecting to the database
connection = sqlite3.connect("myTable.db")
crsr = connection.cursor()

# SQL command to create a table in the database
sql_command = """CREATE TABLE emp (
staff_number INTEGER PRIMARY KEY,
fname VARCHAR(20),
lname VARCHAR(30),
gender CHAR(1),
joining DATE);"""

# execute the statement
crsr.execute(sql_command)

# SQL command to insert the data in the table
sql_command = """INSERT INTO emp VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");"""
crsr.execute(sql_command)

crsr.execute(sql_command)
connection.commit()
connection.close()

When I run this code, I am getting error:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    connection = sqlite3.connect("myTable.db")
sqlite3.OperationalError: unable to open database file

What am I missing? I tried replacing ("myTable.db") with (".myTable.db") and ("./myTable.db") but same issue. Please suggest.

like image 872
nad Avatar asked Jul 29 '26 21:07

nad


1 Answers

Change permissions to SQLite db file.

Ubuntu

sudo chmod 775 /FolderOfSQLiteDBfile/
sudo chmod 664 /FolderOfSQLiteDBfile/sqlite.db

Windows

chmod 775 /FolderOfSQLiteDBfile/

Refer : sqlite-error-unable-to-open-database-file

like image 165
Morse Avatar answered Aug 01 '26 09:08

Morse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!