python 做⾃动化连接mysql 数据库
⽂章⽬录
前⾔
接⼝数据放到mysql存储,连接不同的数据库,做了判断处理
⼀、python 连接mysql
mysql是常见的关系型数据库,我使⽤mysql做⾃动化就是⽤mysql存放我的接⼝名⾸先你要有pymsql,需要你去下载: pip install pymsql
⼆、进阶使⽤
访问不同的接⼝数据库会建⽴多次连接,造成连接过多,我们采⽤长连接
import pymysql from utils .myLog import MyLog class ReadMysql (object ):  def __init__(self ):        self .log = MyLog ()    def read_testData (self , id ):        sql_word = f "select * from testdata where id = {id}"        # self .log .info (f "sql 语句是:{sql_word}")        try :        self .conn = pymysql .connect (host ='172.
29.28.123', user ="root", passwd ="123456", database ="test")        self .cursor = self .conn .cursor ()        self .log .info ("----------数据库链接成功----------------")        self .cursor .execute (sql_word )        res = self .cursor .fetchone ()        # self .log .info (f "获得的内容是:{res}")        return  res [1], res [3], res [4], res [5], res [6], res [7], res [8], res [9]    except Exception as e :        self .log .error (f "Error:{e}")    def stopCursor (self ):        self .cursor .close ()  # 先关闭游标        self .conn .close ()  # 再关闭数据库连接        self .log .info ("----------数据库断开链接成功----------------")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
mysql下载链接
26

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。