From: gintare statkute on
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

regards,
gintare
From: Steve Holden on
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/

From: Carl Banks on
On Feb 5, 3:19 pm, Steve Holden <st...(a)holdenweb.com> wrote:
> 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.


Well actually you can, sort of. For instance:

os.system('sqlite3 :memory: .help')


Carl Banks
From: Roger Binns on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

gintare statkute wrote:
> Does anybody know if it possible to execute sqlite3 dot commands in python?

The dot commands are parsed and executed by different code not part of the
standard SQLite library.

However if you want interactive shell functionality from Python then you can
use APSW. It includes a shell you can just go ahead and use based on a
shell class you can extend with your own methods, direct input and output as
needed, completion etc.

http://apsw.googlecode.com/svn/publish/shell.html

(Disclaimer: I am the APSW author)

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAktvyGMACgkQmOOfHg372QT5mgCgrMCtb3bHd3rF0L+lL/nZV6BX
zrMAn1fcxS4CyKYF4KXVBcVcEXWhxoig
=hpkY
-----END PGP SIGNATURE-----

From: Aahz on
In article <mailman.2017.1265411978.28905.python-list(a)python.org>,
Steve Holden <steve(a)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(a)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'. :-)"