By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Python Database Scripts

P: n/a
Hello,

Can anyone provide any kind of python database (mysql) code or point me
to a link that has this? Just simple things as maybe using a driver,
opening up a db, an insert and select. Any help would be greatly
appreciated!

Thanks,
--Chuck

Sep 12 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 12 Sep 2005 08:28:39 -0700, Chuck
Can anyone provide any kind of python database (mysql) code or point me
to a link that has this? Just simple things as maybe using a driver,
opening up a db, an insert and select. Any help would be greatly
appreciated!


It might be more than you're looking for, but there is a Python
framework called Dabo that makes working with databases, including
MySQL, very, very easy. Their URL is http://dabodev.com.

--

# p.d.
Sep 12 '05 #2

P: n/a
import MySQLdb

# Create a connection object and create a cursor
conn = MySQLdb.Connect(host="localhost", port=3306, user="mysql",
passwd="pwd123", db="mytest")
c = conn.cursor()

# execute some SQL
c.execute("SELECT * FROM mystuff")

# Fetch all results from the cursor into a sequence
results = c.fetchall()
for r in results:
do_something(r)

# close the connection
conn.close()

Cheers,
Miklos
--
Software development: Python,Zope,Plone,PDF,XML,MivaScript
http://www.jegenye.com

Sep 12 '05 #3

P: n/a
On 12 Sep 2005 08:28:39 -0700, "Chuck" <wi*************@gmail.com>
declaimed the following in comp.lang.python:
Hello,

Can anyone provide any kind of python database (mysql) code or point me
to a link that has this? Just simple things as maybe using a driver,
opening up a db, an insert and select. Any help would be greatly
appreciated!
Did you read the DB-API documentation?
import db_module
aConn = db_module.connect(db specific login parameters)
aCurs = aConn.cursor()
stat = aCurs("SQL Statement", (varying parameters))
for row in aCurs.fetch():
#do stuff with the data
aCurs.close()
aConn.close()

with sometimes an aCurs.commit() or aCurs.rollback()
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Sep 12 '05 #4

P: n/a
ncf
Hmm...sorry to go a little off topic here, but I, also, have been
striving to learn Python/MySQL for a while using MySQL's official
thing. Can you please explain to me why one must use a cursor and can't
just do an execute on the connction? :confused about the subject:

Sep 12 '05 #5

P: n/a
Well, for a single connection object you could use several cursor
objects and juggle with all of them in your program. This can come in
handy if it's not about a simple script like I put in here. You can
reuse the results from the cursors, etc. without issuing more,
potentially resource-hungry, SQL statements.
DB connections can also be shared across threads (I think.. or at least
for some kind of DB adapters.)
Cheers,
Miklos
--
The ring of the friendly serpent in business suit: Python, Zope, Plone
http://www.jegenye.com

Sep 12 '05 #6

P: n/a
Hi, thanks for (all) of your help.

BTW, where is the DB-API docs for python?

Thanks,
--Chuck

Sep 13 '05 #7

P: n/a
On 13 Sep 2005 11:32:05 -0700, Chuck <wi*************@gmail.com> wrote:
BTW, where is the DB-API docs for python?


Google is your friend - <http://www.google.co.uk/search?q=DB-API>, 1st hit.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Sep 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.