execute sqlite3 dot commands in python

Aahz aahz at pythoncraft.com
Fri Feb 12 00:16:59 EST 2010


In article <mailman.2017.1265411978.28905.python-list at python.org>,
Steve Holden  <steve at holdenweb.com> wrote:
>
>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.

However, it may not be obvious that some things that don't look like SQL
are.  For example, "pragma integrity_check".
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"



More information about the Python-list mailing list