posted on: 2008-09-05 07:52:28
This is the simplest...raw_input() based terminal, but I use it.
>I included a check for the ';' at the end to make me feel more like I am doing sql, but of course this terminal doesn't have all of the nifty sql terminal features such as scroll back.
A good reference is found at
http://www.sqlite.org/lang.html
#!/usr/bin/env python
from pysqlite2 import dbapi2 as sqlite
import sys
"""
dbkit.py
This is a small version of a program I use. I have more macros
"""
def no(onecurse):
return 0
def ShowTables(onecurse):
onecurse.execute('SELECT name FROM SQLITE_MASTER where type=?',('table',))
data = onecurse.fetchall()
for item in data:
print repr(item)
macros = {"t":ShowTables}
conn = sqlite.connect(sys.argv[1])
Cursed = conn.cursor()
test = 'start'
while test!='q':
try:
if test[-1]==';':
Cursed.execute(test[:-1])
data = Cursed.fetchall()
for item in data:
print repr(item)
else:
macros.get(test,no)(Cursed)
except:
print sys.exc_info()
test = raw_input('>>')
conn.commit()
I've included a show tables macro since it is a bit more obscure than the standard. Note if the command line argument does not exist, it will create that table. I will probably create another post with usefull ... simple examples.
Such as
DESCRIBE [TABLENAME];
Can be used here as
PRAGMA table_info([TABLENAME]);
Comments
matt
2008-09-08 08:59:58
Here is the command for making it work like a terminal:
<pre>
import readline
</pre>
If you are in windows:
<pre>
import pyreadline
</pre>