1

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.

1 Answer 1

3

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")
Sign up to request clarification or add additional context in comments.

2 Comments

I've given this a go - it's still giving me the 'no such table' error! The subsequent code I've used is sql = "SELECT * FROM table1" followed by cursor.execute(sql). Any further ideas?
Sorry, I don't know the content of your sqlite DB, so I can't verify if the table exists or not.

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.