0
点赞
收藏
分享

微信扫一扫

Python+request 使用pymysql连接数据库mysql的操作《十》

時小白 2022-08-05 阅读 25

     使用指南。pymysql支持python2.7同时也支持python3.x。当前我用的是python2.7。所以过断选择了pymysql的使用,这里注意几点。一般我们连接数据库为了安全起见,都会要求按照ssl的方式进行连接,但是为了操作和使用的方便,可以跟开发沟通通过添加白名单的方式,连接某个网络,将此网络的IP添加到白名单中,就可以不用ssl的方式连接数据库,直接使用:dbname,host,username,password的方式连接数据库,方便操作。

 

连接数据库:

(查询数据)示例代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-11

import pymysql,sys
import readConfig
import json
#连接数据库
db = pymysql.connect(host=readConfig.host,user = readConfig.user,passwd=readConfig.passwd,db=readConfig.db) #db:库名
#创建游标
# cur = db.cursor()

#创建游标,结果将已字典的形式返回
cur = db.cursor(pymysql.cursors.DictCursor)

# sql = "select bs.uuid,bs.target_type,bs.source,bs.user_uuid,bs.agent_uuid from `behavior_share` bs join user u on u.uuid = bs.user_uuid where u.mobile = '12606666333'"
#查询lcj表中存在的数据
sql = "select bs.uuid,bs.agent_uuid from `behavior_share` bs join user u on u.uuid = bs.user_uuid where u.mobile = '12606666333'"

cur.execute(sql)
#fetchall:获取lcj表中所有的数据
ret1 = cur.fetchall()
print(ret1)

打印结果:

   

Python+request  使用pymysql连接数据库mysql的操作《十》_sql

 

 

进阶模式:

      将数据库的配置信息写到ini文件中,通过调用,在py文件中显示,方便配置信息的统一管理,也避免了后期更改数据库的host后,要修改很多地方。

(1)创建cfg.ini文件。写的内容如下:

[Test_Env_mysql]
##############此类信息向开发获取#################
host = ***********************
user = iber_php
passwd = *************
db = iber2_admin

 

(2)创建  readConfig.py 文件,读取ini的配置信息

#!/usr/bin/env python
# coding=UTF-8

'''此文件主要是获取cfg.ini中对应的配置信息'''
import os
import ConfigParser


cur_path = os.path.dirname(os.path.realpath(__file__))
configpath = os.path.join(cur_path,"cfg.ini")
conf = ConfigParser.ConfigParser()
conf.read(configpath)

#############获取到mysql的相关信息##################
host = conf.get("Test_Env_mysql","host")
user = conf.get("Test_Env_mysql","user")
passwd = conf.get("Test_Env_mysql","passwd")
db = conf.get("Test_Env_mysql","db")

 

(3)在使用的  attach_mysql.py 文件中直接调用

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-11

import pymysql,sys
import readConfig
import json
#连接数据库
db = pymysql.connect(host=readConfig.host,user = readConfig.user,passwd=readConfig.passwd,db=readConfig.db) #db:库名

..........剩下的信息更上方的((查询数据)示例代码如下:)写法一致。

 

数据库获取结果的几种常见方式;

示例一:

sql = "select bs.uuid,bs.agent_uuid from `behavior_share` bs join user u on u.uuid = bs.user_uuid where u.mobile = '12606666333'"

cur.execute(sql)
#fetchall:获取lcj表中所有的数据
ret1 = cur.fetchall()
print(ret1) #获取所有的查询结果,此处的类型是列表
print len(ret1) #获取所有结果的条数
print ret1[0] #获取所得结果为0下标的字典
print type(ret1[0]) #此处的类型是字典
print ret1[0]["uuid"] #获取所得结果为0下标的字典中的某个值

打印结果:

      

Python+request  使用pymysql连接数据库mysql的操作《十》_sql_02

 

实例二:

    获取某行的数据。

sql = "select bs.uuid,bs.agent_uuid from `behavior_share` bs join user u on u.uuid = bs.user_uuid where u.mobile = '12606666333'"

cur.execute(sql)


print cur.fetchmany(1) #获取查询结果的前一行的数据
print cur.fetchmany(2) #获取查询结果的前二行的数据

 

打印结果:

     

Python+request  使用pymysql连接数据库mysql的操作《十》_sql_03

  1.作者:Syw

2.本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

3.如果文中有什么错误,欢迎指出。以免更多的人被误导。



举报

相关推荐

0 条评论