您的位置:首页 > 其它

文章标题

2015-10-14 20:02 423 查看
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: