Which SQL module to use?

M

mrstephengross

I'd like to do some basic SQL stuff in Python. It seems like there are
a heck of a lot of SQL modules for Python. What's the simplest and
easiest one to use?

Thanks,
--Steve ([email protected])
 
P

Philipp

mrstephengross said:
I'd like to do some basic SQL stuff in Python. It seems like there are
a heck of a lot of SQL modules for Python. What's the simplest and
easiest one to use?

Thanks,
--Steve ([email protected])

Do you have any DBMS in mind?

Philipp
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

mrstephengross said:
I'd like to do some basic SQL stuff in Python. It seems like there are
a heck of a lot of SQL modules for Python. What's the simplest and
easiest one to use?

It looks like pysqlite would be good for getting started with the
SQL/Python combo:

http://www.pysqlite.org/

It's an embedded database engine, so you do not need to install a
separate database server. Just import the module and you have a database
engine in your Python:

Now, let's create a database connection to a local file mydb.db. As this
file does not exist, yet, SQLite will create it automatically.
<pysqlite2.dbapi2.Connection object at 0x00967B18>

con is the database connection object.

Now, Python needs a cursor object for most database operations, so let's
create one:
<pysqlite2.dbapi2.Cursor object at 0x009CFF50>

We need data to play with, so let's create a simple table:

Now let's populate the table:

Commit the changes, so they're visible to other database connections
that would open the file mydb.db.

Now let's try some queries:
>>> cur.execute("select id, name from persons")
>>> cur.fetchall() [(1, u'David'), (2, u'Rachel'), (3, u'Simon')]
>>>

Note that SQLite returns Unicode strings for TEXT.

Next, let's try to use a parametrized query. pysqlite uses the qmark
style, so you just put ? as placeholders:
[(2, u'Rachel')]

That's enough for a start I think.

Maybe I could whet your appetite. The pysqlite documentation has a few
other small examples at
http://initd.org/pub/software/pysqlite/doc/usage-guide.html#brief-tutorial

But nothing extensive, yet.

HTH,

-- Gerhard
 
S

Steve Bergman

mrstephengross said:
I'd like to do some basic SQL stuff in Python. It seems like there are
a heck of a lot of SQL modules for Python. What's the simplest and
easiest one to use?
The suggestion to use sqllite is probably a good one to get started.

I'm a PostgreSQL fan and use pygresql. It has 2 modes: "Classic" and
"DBAPI 2.0".

The DPAPI 2.0 interface makes your SQL portable between RDBMs. But you
can't beat the classic interface for simplicity, elegance, and convenience.

With classic, just pass a dictionary and say insert this or update this
and its done. With selects, its easy to get your results as a dictionary.

-Steve Bergman
 
B

Bruno Desthuilliers

mrstephengross a écrit :
I'd like to do some basic SQL stuff in Python. It seems like there are
a heck of a lot of SQL modules for Python. What's the simplest and
easiest one to use?

Probably the one that go with your RDBMS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,008
Latest member
KieraMcGuf

Latest Threads

Top