Python Database Scripts

C

Chuck

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
 
P

Peter Decker

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.
 
J

jegenye2001

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
 
D

Dennis Lee Bieber

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()
--
 
N

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:
 
J

jegenye2001

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
 
C

Chuck

Hi, thanks for (all) of your help.

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

Thanks,
--Chuck
 

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

Forum statistics

Threads
474,264
Messages
2,571,317
Members
48,003
Latest member
coldDuece

Latest Threads

Top