Python + request connection using pymysql mysql database operation "ten"

     user's guidance. pymysql support python2.7 also supports python3.x. Currently I use python2.7. So after breaking chosen to use pymysql, where attention to several points. Generally, we connect to the database for security reasons, we will be asked to connect as ssl way, but in order to operate and easy to use, can communicate with developers by way of adding white list, a network connection, add this IP network to the white list , the mode can not connect to the database ssl, direct use: dbname, host, username, password database connection manner, easy to operate.

 

Connect to the database:

(Inquiry data) following sample code:

# ! / Usr / bin / Python the env 
# - * - Coding: UTF-. 8 - * - 
# the Author: Lucky, Time: 2019-06-11 

Import pymysql, SYS
 Import readconfig
 Import JSON
 # connection database 
db = pymysql.connect ( = readConfig.host Host, the User = readConfig.user, passwd = readConfig.passwd, db = readConfig.db) # db: library name 
# create a cursor 
# CUR = db.cursor () 

# create a cursor, the result would have been in the form of a dictionary return 
CUR = db.cursor (pymysql.cursors.DictCursor) 

# SQL = "the SELECT bs.uuid, bs.target_type, bs.source, bs.user_uuid, bs.agent_uuid from the Join the User` behavior_share` bs = U ON u.uuid bs.user_uuid where u.mobile = '12606666333' "
# Query data table lcj present 
SQL = " SELECT bs.uuid, bs.agent_uuid` behavior_share` BS from the Join User U = ON u.uuid bs.user_uuid WHERE u.mobile = '12,606,666,333' " 

cur.execute (SQL) 
# or fetchall: Get all the data in the table lcj 
RET1 = cur.fetchall ()
 Print (RET1)

Print Results:

   

 

 

Advanced mode:

      The configuration information is written to the database ini file, by calling in the py file display, easy to configure unified management of information, but also to avoid late changes after the host database, to modify many places.

(1) create cfg.ini file. Wrote as follows:

[Test_Env_mysql]
 # ############# obtain such information to develop ################# 
Host = ******** *************** 
the User = iber_php 
passwd = ************* 
db = iber2_admin

 

(2) create readConfig.py file, read configuration information of 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下标的字典中的某个值

打印结果:

      

 

实例二:

    获取某行的数据。

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)   #获取查询结果的前二行的数据

 

打印结果:

     

 

Guess you like

Origin www.cnblogs.com/syw20170419/p/11006132.html