SQLite using Anaconda?

I’m reviewing the Python with Databases article and am having a little trouble getting started with SQLite on my local machine.

I am utilizing Python inside of an Anaconda Powershell Prompt. There are no issues when I run the basic line “import sqlite3” but when I try to create a table as suggested in the article I run into an operational error. It reads as follows:

conn = sqlite3.connect(“first.db”)
Traceback (most recent call last):
File “”, line 1, in
sqlite3.OperationalError: unable to open database file

As a result I’m not able to utilize any of the other options noted in the article. Any suggestions on how to get this going? Thanks!

https://www.codecademy.com/paths/data-science/tracks/dscp-data-acquisition/modules/dscp-sql/articles/python-with-databases

It looks like it’s simply unable to open that database file. Should this file already exist or are you creating it?

Perhaps you could try opening it with either the full file path ("C:\\Users\etc...\first.db") or at least a path to a directory where you have write permissions (so it can be created if it doesn’t exist).

If that works then you need to consider what directory you are running in and whether altering the relative path or the working directory is appropriate.

This database does not currently exist. The articled didn’t seem to make a distinction between creating a new DB and accessing an existing one. I assumed that perhaps it was just a matter of sqlite looking for an existing db and if it didn’t find it then it created a new one…

All that said, perhaps the better question is how do I create a new db?

I think the issue might be with the permissions of your current working directory. You’re right that it should create a new database even if one doesn’t exist but it may not be able to write to the current directory. I’d suggest trying to open this file specifically in a place you know you have access, e.g. under your User.

What directory are you running this in? If you’re not sure then confirm with something like- import os followed by print(os.getcwd()) to output your current working directory.

1 Like