用python操作mysql数据库(之通过类实现,方便添加功能)

# -*- coding: utf-8 -*-
"""
Created on Sun Nov 27 18:57:44 2016

@author: toby
"""
import MySQLdb
'test_db'
class MysqlHelper(object):
    
    def __init__(self,hosts,users,password,dbname):
        self.conn = MySQLdb.connect(host=hosts,user=users,passwd=password,db=dbname)
        self.cur = self.conn.cursor(MySQLdb.cursors.DictCursor)
    #获取数据库所有数据
    def Get_all_data(self):
        sql = "select * from user"
        self.cur.execute(sql)
        data = self.cur.fetchall()
        self.cur.close()
        self.conn.close()
        return data

 
a = MysqlHelper('127.0.0.1','root','1qaz#EDC','test_db')
print a.Get_all_data()

网页名称:用python操作mysql数据库(之通过类实现,方便添加功能)
分享网址:http://lszwz.com/article/pshsee.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款