操作数据库样例

# -*- coding:utf-8 -*-
import pymysql
import time
#创建数据库链接
conn = pymysql.connect(
host='rm-bp1vu5d84dg12c6d59o.mysql.rds.aliyuncs.com',
port=3306,
user='root',
passwd='wYf092415*',
db='pays_test',
charset='utf8',
)
#创建游标
cursor=conn.cursor()

def consumer(name):
print("%s 用户发言" %name)
#global cursor
while True:
baozi = yield
print("用户[%s]第[%s]次发言!" %(name,baozi))
# 执行SQL,并返回受影响的行数
#群内发言
TIME_START = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
for i in range(2):
# 添加群成员
effect_row2 = cursor.execute(
"INSERT INTO `t_talkgroup_members`(`user_id`,`group_id`,`join_time`,`creation_time`) VALUES ('%s',%d,'%s','%s')" % (
name, baozi, TIME_START, TIME_START))
meassge_sendtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
effect_row3 = cursor.execute("insert into `t_talkgroup_meassge`(`sender`,`group_id`,`meassge_text`,`case_begintime`,`meassge_sendtime`) values ('%s',%d,'用户%s第%s%d发言','%s','%s')" %(name,baozi,name,baozi,i,TIME_START,meassge_sendtime))
print(effect_row3)

def producer(name):
c = consumer('A')
c2 = consumer('B')
c3 = consumer('C')
c.__next__()
c2.__next__()
c3.__next__()
print("用户准备发言!")
for i in range(3):
# 获取群聊创建时间
TIME_START = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(TIME_START)
# 创建群:
effect_row1 = cursor.execute(
"insert into `t_talkgroup`(`title`,`status`,`begintime`,`endtime`,`case_id`) values ('万达抢劫案罪犯%s第%s次犯罪','1','%s',NULL,'201806%s%s')" % (
name, i, TIME_START, i, name))
# 查询群groupid
effect_row11 = cursor.execute("select group_id from t_talkgroup where case_id='201806%s%s'" % (i, name))
groupid = cursor.fetchall()
groupid = groupid[0][0]
print(groupid)

time.sleep(0.1)
print("发言:。。。。。!")
c.send(groupid)
c2.send(groupid)
c3.send(groupid)
print("%i:::::::::::::::::::::" %i)
producer("alex")
conn.commit()

# 关闭游标
cursor.close()
# 关闭连接
conn.close()

猜你喜欢

转载自www.cnblogs.com/wzsx/p/9204781.html
今日推荐