mysqlhelper文件中
表:
import pymysql
class MysqlHelper(object):
def __init__(self):
self.db=pymysql.connect(host='127.0.0.1',user='root',port=3306,password='123456',database='py10',charset='utf8')
self.cursor=self.db.cursor()
def biao(self):
self.cursor.execute("show tables")
table_list = [tuple[0] for tuple in self.cursor.fetchall()]
self.db.commit()
return table_list
def __del__(self):
self.cursor.close()
self.db.close()
if __name__=='__main__':
con =MysqlHelper()
biaos = con.biao()
输出值为列表:['biao1','biao2']
下面信息如上格式
字段:
def ziduan(self,sql):
self.cursor.execute(sql)
col_name_list = [tuple[0] for tuple in self.cursor.description]
self.db.commit()
return col_name_list
con=MysqlHelper()
con.ziduan('select * from biao1')
输出biao1的所有字段
信息条数:
def hangshu(self,sql):
self.cursor.execute(sql)
print(self.cursor.execute(sql))
self.db.commit()
con=MysqlHelper()
con.hangshu('select * from {}'.format(tablename))
返回数量:如 1055