user1906956
user1906956

Reputation:

Trying to use sqlite with python shell

I just got python and typing:

sqlite test.db

into the shell, but I get a syntax error. What have I missed?

Upvotes: 2

Views: 5295

Answers (4)

Mihai8
Mihai8

Reputation: 3147

Verify if sqlite exist in PATH and what are privileges for file test.db.

Upvotes: 0

soerface
soerface

Reputation: 6823

I guess that you did the following?

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> sqlite test.db
  File "<stdin>", line 1
    sqlite test.db
              ^
SyntaxError: invalid syntax

Try this instead:

import sqlite3
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute('''Your query goes here''')

For more details, take a look at the sqlite documentation for python2 or python3

Upvotes: 8

Ulises
Ulises

Reputation: 13419

Python doesn't provide this command-line utility so make sure sqlite3 is in your path. Then you can either execute:

$ sqlite3 mydb.db

or if you have entered your settings in settings.py:

./manage.py dbshell

Upvotes: 2

doru
doru

Reputation: 9110

I think you want to use the sqlite3 command line tool to create a new database. For this you should use your system terminal not the python console. So the command should look like so (on a linux system):

$ sqlite3 test.db

Upvotes: 0

Related Questions