Python e MySQL

Per poter accedere a MySQL da Python occorre installar l’API python-mysqldb

poi aggiungere la riga import MySQLdb

Esempio completo:


#!/usr/bin/env python
# periodi.py

import sys
import MySQLdb

conn = MySQLdb.connect(host="localhost", user="nome_utente", passwd="password", db="nome_database")

cursore = conn.cursor()

cursore.execute('SELECT * FROM cliente')

tupla = cursore.fetchall()

print
print 'codice cliente\tpartita IVA\tragione sociale'
print '-------------------------------------------------'
for record in tupla:
print '%d\t\t%s\t%s' % (record[0], record[1], record[2])

print 'end.'
print

Riferimenti:

http://www.kitebird.com/articles/pydbapi.html
http://mysql-python.sourceforge.net/MySQLdb.html
http://www.blackbirdblog.it/programmazione/python/python-e-mysql
http://docs.python.org/contents.html

Lascia un commento