python 操作数据库查询相关

# !/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import pymysql

# 打开数据库连接
conn = pymysql.connect(host='127.0.0.1', user = "root", passwd="root", db="test", port=3306, charset="utf8")

#conn = pymysql.connect("连接地址","用户名","密码","数据库")

cur = conn.cursor()
# 使用execute方法执行SQL语句
# 使用execute方法执行SQL语句
def reg_UserID():
  query="SELECT COUNT(DISTINCT UserId)FROM reg where Time=%s"
  cur.execute(query,['2018/11/10 10:18'])
  rows = cur.fetchall()
  print('用户新增数为',rows)

def reg_Devices():
  query="SELECT COUNT(DISTINCT DeviceInfo)FROM reg where Time=%s"
  cur.execute(query,['2018/11/10 10:18'])
  rows = cur.fetchall()
  print('设备新增数为', rows)
 #累计用户数按设备维度统计
def reg_UserIDTotal():
  query="SELECT COUNT(DISTINCT DeviceInfo)FROM reg"
  cur.execute(query)
  rows = cur.fetchall()
  print('累计新增用户数(按设备维度)为',rows)


#用户数
def hall_UserID():
  query="SELECT COUNT(DISTINCT UserId)FROM hall where Time=%s"
  cur.execute(query,['2018/11/10 10:18'])
  rows = cur.fetchall()
  print('用户新增数为',rows)

#设备数
def hall_Devices():
  query="SELECT COUNT(DISTINCT DeviceInfo)FROM hall where Time=%s"
  cur.execute(query,['2018/11/10 10:18'])
  rows = cur.fetchall()
  print('设备新增数为', rows)

#大厅应用新增用户数,设备新增数
def hall_Appcation():
    query = "SELECT COUNT(DISTINCT UserId) from hall where Time=%s"
    query1 = "SELECT COUNT(DISTINCT DeviceInfo) from hall where Time=%s"
    cur.execute(query, [ '2018/11/10 10:18'])
    cur1.execute(query1, ['2018/11/10 10:18'])
    rows = cur.fetchall()
    rows1 = cur1.fetchall()
    print('大厅应用新增用户数为', rows)
    print('大厅应用新增设备数为', rows1)
#大厅总渠道数
def hall_Chanel():
 query="SELECT COUNT(DISTINCT Channel) from hall where GameCode=%s"
 cur.execute(query,['comf'])
 rows = cur.fetchall()
 print('大厅总渠道数为', rows)

 #渠道下的用户总数
def hall_ChaneldiffUser():
    query = "SELECT COUNT(DISTINCT UserId) from hall where Channel=%s and Time=%s"
    cur.execute(query, ['1000000009','2018/11/10 10:18'])
    rows = cur.fetchall()
    #02渠道
    cur1.execute(query, ['1000000002','2018/11/10 10:18'])
    rows1 = cur1.fetchall()
    print('09渠道大厅用户新增数为', rows)
    print('02渠道大厅用户新增数为', rows1)
    print('09渠道大厅用户活跃数为', rows)
    print('02渠道大厅用户活跃数为', rows1)

#渠道下的用户总数
def hall_ChaneldiffDevices():
    query = "SELECT COUNT(DISTINCT DeviceInfo) from hall where Channel=%s and Time=%s"
    cur.execute(query, ['1000000009','2018/11/10 10:18'])
    rows = cur.fetchall()
    #02渠道
    cur1.execute(query, ['1000000002','2018/11/10 10:18'])
    rows1 = cur1.fetchall()
    print('09渠道大厅设备新增数为',rows)
    print('02渠道大厅设备新增数为', rows1)
    print('09渠道大厅设备活跃数为', rows)
    print('02渠道大厅设备活跃数为', rows1)

#rummy设备及用户新增/ 设备与用户活跃/ 渠道  房间数据统计
def room_R():
    query = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Time=%s"
    query1 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Time=%s"
    cur.execute(query, ['1001', '2018/11/10 10:18'])
    cur1.execute(query1, ['1001', '2018/11/10 10:18'])
    #分渠道统计
    query2="SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query3="SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur2.execute(query2, ['1001','1000000002','2018/11/10 10:18'])
    cur3.execute(query3, ['1001','1000000002','2018/11/10 10:18'])
    query4 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query5 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur4.execute(query4, ['1001', '1000000009', '2018/11/10 10:18'])
    cur5.execute(query5, ['1001', '1000000009', '2018/11/10 10:18'])
    rows = cur.fetchall()
    rows1 = cur1.fetchall()

设备及用户新增
def room_T():
    query = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Time=%s"
    query1 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Time=%s"
    cur.execute(query, ['1002', '2018/11/10 10:18'])
    cur1.execute(query1, ['1002', '2018/11/10 10:18'])
    # 分渠道统计
    query2 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query3 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur2.execute(query2, ['1002', '1000000002', '2018/11/10 10:18'])
    cur3.execute(query3, ['1002', '1000000002', '2018/11/10 10:18'])
    query4 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query5 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur4.execute(query4, ['1002', '1000000009', '2018/11/10 10:18'])
    cur5.execute(query5, ['1002', '1000000009', '2018/11/10 10:18'])
    rows = cur.fetchall()
    rows1 = cur1.fetchall()
    rows2 = cur2.fetchall()
    rows3 = cur3.fetchall()
    rows4 = cur4.fetchall()
    rows5 = cur5.fetchall()
#设备及用户新增
def room_L():
    query = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Time=%s"
    query1 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Time=%s"
    cur.execute(query, ['1003', '2018/11/10 10:18'])
    cur1.execute(query1, ['1003', '2018/11/10 10:18'])
    # 分渠道统计
    query2 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query3 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur2.execute(query2, ['1003', '1000000002', '2018/11/10 10:18'])
    cur3.execute(query3, ['1003', '1000000002', '2018/11/10 10:18'])
    query4 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query5 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur4.execute(query4, ['1003', '1000000009', '2018/11/10 10:18'])
    cur5.execute(query5, ['1003', '1000000009', '2018/11/10 10:18'])
    rows = cur.fetchall()
#设备及用户新增
def room_S():
    query = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Time=%s"
    query1 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Time=%s"
    cur.execute(query, ['1004', '2018/11/10 10:18'])
    cur1.execute(query1, ['1004', '2018/11/10 10:18'])
    # 分渠道统计
    query2 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query3 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur2.execute(query2, ['1004', '1000000002', '2018/11/10 10:18'])
    cur3.execute(query3, ['1004', '1000000002', '2018/11/10 10:18'])
    query4 = "SELECT COUNT(DISTINCT UserId) from room where GameId=%s and Channel=%s and Time=%s"
    query5 = "SELECT COUNT(DISTINCT DeviceInfo) from room where GameId=%s and Channel=%s and Time=%s"
    cur4.execute(query4, ['1004', '1000000009', '2018/11/10 10:18'])
    cur5.execute(query5, ['1004', '1000000009', '2018/11/10 10:18'])
    rows = cur.fetchall()

if __name__ == '__main__':
    print("---------------------------注册数据------------------------------")
    reg_Devices()
    reg_UserID()
    reg_UserIDTotal()
    print("---------------------------大厅数据------------------------------")
    hall_UserID()
    hall_Devices()
    hall_Appcation()
    hall_Chanel()
    hall_ChaneldiffUser()
    hall_ChaneldiffDevices()
    print("---------------------------房间数据------------------------------")
    room_R()
    room_T()
    room_L()
    room_S()

其他说明: 参数化SQL请求时 拆分SQL 跟参数

1、query 定义sql 语句 参数化数据采用s% 代替需要的参数

2、cur.execute(query, [具体参数源]) 多个参数时 逗号区分开

3、cur.fetchall 获取所有数据项目

4、查询只有一条数据时:

cursor.fetchone():将只返回一条结果,返回单个元组。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值