Python Mysql_db对数据查询进行处理
引用自:https://www.cnblogs.com/itdyb/p/5700614.html
一、脚本如下
#!/usr/bin/python2.7
# coding=utf-8
import time
from datetime import datetime
import MySQLdb
endtime = datetime.now()
def mysqlCmd():
conn=MySQLdb.connect(host='rm-bp1h53532m383312xo.mysql.rds.aliyuncs.com',port=3306,db='py_sync',user='pycf',passwd='1qaz_2wsx',charset='utf8')
#连接数据库
cur=conn.cursor()
sql = '''select
start_date,
status,
taskid,
tablename
from
sync_task_info
where
status=0
order by
start_date desc
limit 5'''
#执行查询命令
cur.execute(sql)#按start_date字段降序查询。
#打印查询行数
print(cur.rowcount)
#打印一行记录
# rs = cur.fetchone()
# print(rs)
# #打印接下来两个记录记录
# rs = cur.fetchmany(2)
# print(rs)
#打印接下来的所有记录
rs = cur.fetchall()
print(rs)
print(len(rs))
print(type(rs))
for starttime in rs:
starttime = datetime.strptime(starttime[0], "%Y-%m-%d %H:%M:%S")
if (endtime-starttime).seconds/3600 < 4:
print(1)
else:
pass
conn.commit()
cur.close()
conn.close()
if __name__ == "__main__":
mysqlCmd()