import MySQLdb
class Mysql_api:
def __init__(self,database_name,username="root",password="******"):
try:
self.con=MySQLdb.connect(host="localhost",user=username,passwd=password,db=database_name)
self.cursor=self.con.cursor()
except Exception,e:
print e
def execute(self,*sql):
try:
if len(sql)>1:
for sqlchild in sql:
self.cursor.execute(sqlchild)
else:
self.cursor.execute(sql[0])
except Exception,e:
print e
def fetch(self):
try:
result_man=[]
result=self.cursor.fetchall()
if result:
if isinstance(result[0][0],long):
result_man=list(result)
else:
zip_result=zip(*result)[0]
result_man=list(zip_result)
except Exception,e:
print e,
return result_man
def close(self):
self.cursor.close()
self.con.close()
self.con.close()
04-03