123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- # author : zhaolongyue
- import pymysql
- class Data_get():
- @staticmethod
- def connectdb(host,port,user,password,database):
- # print("连接到mysql服务器")
- db = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
- print("数据库已连接!")
- return db
- @staticmethod
- def createtb(db, sql):
- # 使用cursor()方法获取操作游标
- cursor = db.cursor()
- # 使用 execute() 方法执行 SQL 查询
- cursor.execute(sql)
- print("数据库已创建!")
- # 获取mysql数据
- @staticmethod
- def insert_data(db,sql):
- # 使用cursor()方法获取操作游标
- cursor = db.cursor()
- # SQL语句
- try:
- # 使用 execute() 方法执行 SQL 查询
- cursor.execute(sql)
- #执行sql语句
- db.commit()
- except:
- #如果发生错误则回滚
- db.rollback()
- print("执行语句失败")
- @staticmethod
- def select_data(db,sql):
- # 使用cursor()方法获取操作游标
- cursor = db.cursor()
- # SQL语句
- try:
- # 使用 execute() 方法执行 SQL 查询
- cursor.execute(sql)
- # 执行sql语句
- db.commit()
- #获取所有记录列表
- results = cursor.fetchall()
- return results
- except:
- # 如果发生错误则回滚
- db.rollback()
- print("执行语句失败")
- # 关闭数据库
- @staticmethod
- def closedb(db):
- db.close()
|