Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use sqlite with python shell

Tags:

python

sqlite

I just got python and typing:

sqlite test.db

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


2 Answers

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

like image 189
soerface Avatar answered Dec 11 '25 12:12

soerface


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
like image 22
Ulises Avatar answered Dec 11 '25 12:12

Ulises



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!