温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 数据库 > 
  • 用python操作mysql数据库(之通过类实现,方便添加功能)

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

发布时间:2020-06-07 00:10:50 来源:网络 阅读:483 作者:TtrToby 栏目:数据库
# -*- 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()


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI