473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Which SQL module to use?

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 (mr************@hotmail.com)

Oct 4 '05 #1
4 2434
mrstephengross schrieb:
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 (mr************@hotmail.com)


Do you have any DBMS in mind?

Philipp
Oct 4 '05 #2
mrstephengross wrote:
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:
from pysqlite2 import dbapi2 as sqlite
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.
con = sqlite.connect("mydb.db")
con <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:
cur = con.cursor()
cur <pysqlite2.dbapi2.Cursor object at 0x009CFF50>

We need data to play with, so let's create a simple table:
cur.execute("create table persons(id integer primary key, name text)")
Now let's populate the table:
cur.execute("insert into persons(name) values (?)", ("David",))
cur.execute("insert into persons(name) values (?)", ("Rachel",))
cur.execute("insert into persons(name) values (?)", ("Simon",))

Commit the changes, so they're visible to other database connections
that would open the file mydb.db.
con.commit()
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:
whichname = "Rachel"
cur.execute("select id, name from persons where name=?", (whichname,))
cur.fetchall()

[(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/pysqli...brief-tutorial

But nothing extensive, yet.

HTH,

-- Gerhard

Oct 4 '05 #3
mrstephengross wrote:
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
Oct 4 '05 #4
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.
Oct 4 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: SungSoo, Han | last post by:
There are two coding style when python import module. (1) import sys (2) from import sys (or from import *) I prefer (1) style. Because it's easy to read, understand module in any category .. ...
9
by: W. Borgert | last post by:
Hi, I'm not new to Python, but new to databases and PostgreSQL. I like to write a PostgreSQL client application (not code running inside of the RDBMS), and Debian has four modules: ...
48
by: monkey | last post by:
Read through python site for programming tool, really plenty of choices :-) (For c++, I just can't breath with very very limited choices) Tried Spe, it come with wxGlade built-in very nice(is Spe...
8
by: Zheng Da | last post by:
I don't know where should I ask the question, so send the email to this group. I choose this group, because I want to write the program with c++ :) I want to write a program which support...
0
by: MeMyselfNDotnet | last post by:
Hi All, Im facing a problem with the COM+ component, will appreciate if u can provide any kind of guidance. My COM+, references a class, lets call it class A. It uses the properties of Class A,...
2
by: ygao | last post by:
when a function or method is called,how can get the module global symbol table from which it is called,not the module where it is defined(this is easy). thanks!
6
by: thojens | last post by:
Hi, we're running Apache 2.2 + PHP 5.2.1 on Windows 2003. When booting the server, we get several message boxes saying the following: Warning Unknown(): (null): Unable to initialize module...
0
by: Roy Smith | last post by:
In article <87zlrzw1f5.fsf@physik.rwth-aachen.de>, Torsten Bronger <bronger@physik.rwth-aachen.dewrote: When I first started using unittest, I did the same thing you did -- I put the test...
2
by: Kurda Yon | last post by:
Hi, I would like to declare a global variable, which is seen to a particular function. If I do as the following it works: x = 1 def test(): global x print x return 1
2
by: jesus4gaveme03 | last post by:
I ceated a automatic split form from a table called "Master NSN List." I then added 2 buttons "cmdShowHide" which toggles between showing and hiding the form section giving more room for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.