1

I am unable to create a database file using python. I am facing this issue only in windows. On my Mac machine i was able to get it working. Here is a test code i tried -

import os, sys, shutil, glob, pandas, sqlite3

db_path = 'E:\\Archive.db'
conn = sqlite3.connect(db_path)

Here are other versions of variable db_path i have tried

db_path = 'E:\Archive.db'

db_path = 'E:/Archive.db'

db_path = 'E://Archive.db'

But i always get following error -

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

I do have write permissions on E: I was able to create csv files using python script. Not sure why its stuck with database. It should create the database in E:

1
  • Often you don't have permission to create files in the root of a drive. Commented Sep 10, 2019 at 1:43

1 Answer 1

1

Following example taken from here works for me. Try this out. If this doesn't work, try to create a file in your documents folder by changing the path

import sqlite3
from sqlite3 import Error


def create_connection(db_file):
    """ create a database connection to a SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)
    finally:
        if conn:
            conn.close()


if __name__ == '__main__':
    create_connection(r"E:\Archive.db")
Sign up to request clarification or add additional context in comments.

1 Comment

That code will not solve the issue. It just shows the error nicely. But your suggestion of changing the folder work. I manually created a folder in E: (let's call it tempFolder). Then changed my code to create database inside tempFolder and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.