60 lines
1 KiB
Python
60 lines
1 KiB
Python
|
#!/usr/bin/python
|
||
|
|
||
|
import MySQLdb
|
||
|
import logging
|
||
|
import sys
|
||
|
|
||
|
class MyDB(object):
|
||
|
|
||
|
host = ""
|
||
|
user = ""
|
||
|
pwd = ""
|
||
|
db = ""
|
||
|
|
||
|
con = 0
|
||
|
|
||
|
def __init__(self,host,user,pwd,db):
|
||
|
self.host = host
|
||
|
self.user = user
|
||
|
self.pwd = pwd
|
||
|
self.db = db
|
||
|
|
||
|
def connect(self):
|
||
|
if self.con == 0:
|
||
|
try:
|
||
|
con = MySQLdb.connect(self.host,self.user,self.pwd,self.db)
|
||
|
self.con = con
|
||
|
except Exception, e:
|
||
|
logging.fatal(e)
|
||
|
sys.exit(1)
|
||
|
|
||
|
def doSQL(self,sql):
|
||
|
cursor = self.con.cursor()
|
||
|
try:
|
||
|
cursor.execute(sql)
|
||
|
self.con.commit()
|
||
|
return True
|
||
|
except Exception, e:
|
||
|
logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
|
||
|
self.con.rollback()
|
||
|
return False
|
||
|
|
||
|
def doSelect(self,sql):
|
||
|
cursor = self.con.cursor()
|
||
|
try:
|
||
|
cursor.execute(sql)
|
||
|
results = cursor.fetchall()
|
||
|
return results
|
||
|
ret=[]
|
||
|
t=0
|
||
|
for row in results:
|
||
|
c=0
|
||
|
for field in row:
|
||
|
ret[t][c]=field
|
||
|
c=c+1
|
||
|
t=t+1
|
||
|
return ret
|
||
|
except Exception, e:
|
||
|
logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
|
||
|
return False
|