Python RPA 数据库篇 - MySQL 篇 1 - 连接 mysql 数据库

代码

mysql 库

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @virtualenv:workon
# @contact: contact information
# @Desc:python 简单使用数据库
__author__ = '未昔/AngelFate'
__date__ = '2019/8/27 18:45'
import pymysql

数据信息

self.mysql_ip = 'localhost'
self.mysql_user = 'root'
self.mysql_pwd = 'root'
self.mysql_database = 'test'
self.mysql_charest = 'utf8'

连接数据库

 def conn_mysql(self):
        db = pymysql.connect(self.mysql_ip, self.mysql_user, self.mysql_pwd, self.mysql_database, charset = self.mysql_charest)
        return db

查询数据

 def select_mysql(self):
	db = self.conn_mysql()

        cursor = db.cursor()


        cursor.execute("SELECT VERSION()")


        data = cursor.fetchone()

        print("Database version : %s " % data)

使用数据库一定要记得关闭

结果

Database version : 5.5.40 
None
···