Python Mysql_db对数据查询进行处理
生活随笔
收集整理的這篇文章主要介紹了
Python Mysql_db对数据查询进行处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
引用自: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')
#連接數(shù)據(jù)庫
cur=conn.cursor()
sql = '''select
start_date,
status,
taskid,
tablename
from
sync_task_info
where
status=0
order by
start_date desc
limit 5'''
#執(zhí)行查詢命令
cur.execute(sql)#按start_date字段降序查詢。
#打印查詢行數(shù)
print(cur.rowcount)
#打印一行記錄
# rs = cur.fetchone()
# print(rs)
# #打印接下來兩個(gè)記錄記錄
# 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()
?
轉(zhuǎn)載于:https://www.cnblogs.com/Presley-lpc/p/9224742.html
總結(jié)
以上是生活随笔為你收集整理的Python Mysql_db对数据查询进行处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php实现倒计时效果
- 下一篇: 洛谷P1057传球游戏题解