execute sqlite3 dot commands in python

Steve Holden steve at holdenweb.com
Fri Feb 5 18:19:09 EST 2010


gintare statkute wrote:
> Does anybody know if it possible to execute sqlite3 dot commands in python?
> 
> dovanotas:/pages/links# python
> Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import sqlite3
>>>> sqlite3  .help
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'help'
> 
> the same with other commands:
> .databases
> .tables
> http://www.sqlite.org/sqlite.html
> 
No. That's not how you pass commands to sqlite3 - you connect to a
database, create a cursor on the connection, and then execute SQL
statements (not sqlite commands) on the cursor. Like this:

>>> import sqlite3
>>> c = sqlite3.connect("deleteme")
>>> cu = c.cursor()
>>> cu.execute(".help")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near ".": syntax error
>>> cu.execute("""\
... CREATE TABLE test(
...   a int primary key,
...   b varchar)""")
<sqlite3.Cursor object at 0x01FD4740>
>>>

As you can see, the cursor's execute() method expects SQL statements.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list