1

This might be an obvious error but I'm trying to create a database within python from a script I've already created.

conn = sqlite3.connect('testDB')
c = conn.cursor()
c.execute('.read create.sql')

This gives an error "sqlite3.OperationalError: near ".": syntax error"

If I do the same thing at the sqlite3 cmd line it works fine

[me@myPC ~]$ sqlite3 testDB
SQLite version 3.3.6
Enter ".help" for instructions
sqlite> .read create.sql 
sqlite> 

It seems that any commands that start with a . give me problems.

2 Answers 2

3

just pass the content of the file to the .execute method:

conn = sqlite3.connect('testDB')
c = conn.cursor()
SQL = open('create.sql').read()
c.executescript(SQL)
Sign up to request clarification or add additional context in comments.

Comments

2

I would suppose that commands starting with . are for the CLI client itself, not for the backend.

So you have no chance to do so and would have to do file reading and executing the queries by yourself, i.e. in Python.

Comments

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.