CJW
CJW

Reputation: 11

Importing an SQLite file into Python

I've been trying (unsuccessfully) to import an existing SQLite database into Python. I want to write a script in Python to run a large set of database queries all at once.

When I try to connect to the SQLite file, Python writes a new file with the same name in folder I'm trying to connect to. Code as follows:

import sqlite3;

conn = sqlite3.connect("C:\Test\Databasename.sqlite")

c = conn.cursor()

When I then try to run queries on specific tables, Python displays 'cannot find table (or similar)' because it has created a new database file; as opposed to importing the existing one.

How do I get Python to open my existing file? I've had a lengthy scour on here, I haven't been able to find an answer sadly :()

Many thanks for any assistance.

Upvotes: 0

Views: 10197

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

You're using backslashes in your path. Python will use them to escape the following character. E.g. \t will become a TAB (hex 09) character.

Either escape your backslashes or use a raw string.

sqlite3.connect("C:\\Test\\Databasename.sqlite")
- or -
sqlite3.connect(r"C:\Test\Databasename.sqlite")

Upvotes: 3

Related Questions