How to create a new database using python and sqlite3

If it isn't created automatically, make sure that you have the directory permissions correct


The code you give does create 'D:\\aaa.db' if it doesn't exist.


As it was already mentioned, your code should work if you have permissions to write for this path. However, it is important that directory must exist. If you make call for non-existing folder:

conn = sqlite3.connect(r"D:\Some new non-existing folder\aaa.db")

It will not work, you will have

sqlite3.OperationalError: unable to open database file. 

The same is for relative paths:

1) conn = sqlite3.connect(r"aaa.db") 
2) conn = sqlite3.connect(r"Some new folder\aaa.db")

First will always work, because you are working in already existing directory and second will not work if you do not create te folder beforehand.

Tags:

Python

Sqlite