Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, when using sqlite3 where are databases stored physically?

Tags:

python

sqlite

Sorry for the basic questions. I'm starting to work with Python. I'm using Windows 10, Python 3.5, Notepad++ as editor. Python is installed in z:\python35 and scripts are in z:\python\sqlite, then my script is really simple:

import sqlite3
conn = sqlite3.connect('sample.db')
c = conn.cursor()
c.execute("CREATE TABLE example (name VARCHAR)")
conn.close()

When I run the script inside Notepad++ (run as administrator) I execute z:\python35\python.exe -i "$(FULL_CURRENT_PATH)". Looks like the script runs correctly, since it does nothing the first time run, but the next time it says:

sqlite3.OperationalError: table example already exits

And that's okay because I wanted to create the database and the table. Thing is, where does this sample.db is stored? I don't find it in the python directory or my projects directory. Not even by searching in Windows.

How can I delete the whole database?

like image 824
luisfer Avatar asked Oct 19 '15 23:10

luisfer


3 Answers

probably in the "current directory" that notepadd++ uses. try to add on top of script:

import os
print(u"current directory: %s" % os.getcwdu())

maybe you could specify an absolute path for the db do deal with that uncertainty

like image 133
Stephane Martin Avatar answered Oct 08 '22 18:10

Stephane Martin


It will be in the current working directory. You can see what this is by

import os
print(os.getcwd())

Once you know the directory you can find and delete the sample.db file.

like image 20
Chad S. Avatar answered Oct 08 '22 16:10

Chad S.


If you do not need the databases to persist on your file system, you can store them in memory using the special :memory: name:

import sqlite3
conn = sqlite3.connect(':memory:')
like image 43
Filip Dupanović Avatar answered Oct 08 '22 16:10

Filip Dupanović