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():将只返回一条结果,返回单个元组。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Python来实现数据库查询可以使用Python的内置模块MySQLdb,可以实现对MySQL数据库的访问和操作。另外,还可以使用ORM(对象关系映射)框架,如SQLAlchemy、Django ORM等,也可以实现对数据库的查询。 ### 回答2: 使用Python实现数据库查询可以借助Python的数据库连接模块,如SQLite3、MySQL Connector等。以下是一个简单的示例: 首先,在Python代码中导入数据库连接模块,比如SQLite3: import sqlite3 然后,连接数据库: connection = sqlite3.connect('database.db') 接下来,创建游标对象,用于执行SQL查询语句: cursor = connection.cursor() 之后,可以根据需要执行不同的查询操作,比如查询所有数据: cursor.execute("SELECT * FROM table_name") data = cursor.fetchall() 这样就可以获取到查询结果,可以根据需要进行后续处理。 完成查询操作后,需要关闭游标和数据库连接: cursor.close() connection.close() 以上是一个简单的用Python实现数据库查询的步骤,具体的操作还会根据具体的数据库和查询需求而有所差异。 ### 回答3: Python是一种强大的编程语言,可以使用Python来实现数据库查询操作Python提供了许多用于处理数据库的库和模块,其中最常用的是SQLite3和MySQLdb。 要使用Python进行数据库查询,首先需要连接到数据库。可以使用SQLite3库来连接SQLite数据库,或者使用MySQLdb库来连接MySQL数据库。连接数据库的代码如下: ```python import sqlite3 # 连接到SQLite数据库 conn = sqlite3.connect('database.db') # 连接到MySQL数据库 # conn = MySQLdb.connect(host='localhost', user='username', passwd='password', db='database') # 创建游标对象 cursor = conn.cursor() ``` 连接到数据库后,就可以执行查询操作了。可以使用游标对象的`execute()`方法来执行SQL语句,并使用游标对象的`fetchall()`方法来获取查询结果。以下是一个示例代码: ```python # 执行查询操作 cursor.execute('SELECT * FROM table_name') # 获取查询结果 results = cursor.fetchall() # 输出查询结果 for row in results: print(row) ``` 以上代码中的`table_name`是要查询的表名。查询结果以元组的形式返回,每一行数据为一个元组。 最后,记得要关闭数据库连接,释放资源。可以使用连接对象的`close()`方法来关闭数据库连接。 ```python # 关闭数据库连接 conn.close() ``` 通过以上步骤,就可以使用Python实现数据库查询操作了。这样可以方便地从数据库中获取数据,并进行后续的处理和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值