python用pymysql调用myslql数据库查询部分
2018-12-06 17:52
483 查看
查询部分 单独记录一下 方便以后查找,
# 整表查询,返回所有数据 def mysql_query_table(self, table): db, cursor = self.mysql_creat_connect() sql = "SELECT * FROM {} ".format(table) cursor.execute(sql) res = [] for x in cursor.fetchall(): res.append(x) self.mysql_close_connect(db, cursor) # logging.info(res) return res # 输入sql语句查询,按类型返回数据 def mysql_query_sql(self, sql, date=[], resmodel='all', many=2): db, cursor = self.mysql_creat_connect() cursor.execute(sql, date) if resmodel == 'all': res = cursor.fetchall() elif resmodel == 'one': res = cursor.fetchone() else: res = cursor.fetchmany(many) self.mysql_close_connect(db, cursor) return res # 输入输入指定时间段查询 def mysql_query_time(self, table, starttime, endtime, field): db, cursor = self.mysql_creat_connect() sql = """SELECT * FROM {0} WHERE {3} BETWEEN '{1}' AND '{2}' """.format(table, starttime, endtime, field) cursor.execute(sql) res = [] for x in cursor.fetchall(): res.append(x) self.mysql_close_connect(db, cursor) # logging.info(res) return res # 查询某天的数据 def mysql_query_time_day(self, table, day, field): db, cursor = self.mysql_creat_connect() sql = """SELECT * FROM {0} WHERE {1} BETWEEN '{2} 00:00:00' AND '{2} 23:59:59' """.format(table, field, day) cursor.execute(sql) res = [] for x in cursor.fetchall(): res.append(x) self.mysql_close_connect(db, cursor) # logging.info(res) return res
简单的封装一下:
# 查询某天的数据 def query_time_day(self, table, day, field='DTIME'): res = self.mysql_query_time_day(table, day, field) return res
查询一下
x = mysqlexample() y = x.query_time_day('atest_doubledate', '2018-12-5') for i in y: pass logging.info(i)
INFO:root:(16, datetime.datetime(2018, 12, 5, 13, 41, 58), 82.4066) INFO:root:(17, datetime.datetime(2018, 12, 5, 13, 59, 33), 595.532) INFO:root:(18, datetime.datetime(2018, 12, 5, 14, 0, 4), 913.284) INFO:root:(19, datetime.datetime(2018, 12, 5, 14, 18, 47), 968.804) INFO:root:(20, datetime.datetime(2018, 12, 5, 14, 22), 987.586) INFO:root:(21, datetime.datetime(2018, 12, 5, 16, 27, 25), 243.352) INFO:root:(40, datetime.datetime(2018, 12, 5, 17, 19, 4), 210.541) INFO:root:(37, datetime.datetime(2018, 12, 5, 17, 15, 21), 26.8079)
ok,完全没有问题。
···················································································································································································
查询某天的sql语句可以更改成以下这样 table 是表明 DTIME是字段 day是日期,dateddiff是两个日期之间相差几天。
#查看某天的数据 sql = SELECT * FROM table WHERE ( datediff ( DTIME , day ) = 0 ) # 查看进7天的数据,查看最近多少天都可以按照这个更改,改一下日期就好了。 sql = "SELECT * FROM atest_doubledate WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <=DTIME "
查询时 碰到了一个小坑,也可能是我没有注意, table 是不能用引号引起来的,如果引起来会报错1064,
阅读更多相关文章推荐
- python调用数据库并查询
- python调用数据库并查询
- python调用数据库并查询
- python数据库sqlite基础(一)-------数据库创建,表的建立,数据录入,数据查询
- 关于Python爬虫之获取海量表情包+存入数据库+搭建网站通过关键字查询表情包
- python调用mysql命令查询和插入表数据(python练手)
- 替换表全表字段的部分内容,查询一个数据库有 多少表 show tables
- 【PythonDjango后台实例 第二章】Python3.6.1+Pyserial+PyMySQL 实现读取串口并存入数据库
- python查询功能,查询文件内容,获取需要的部分并返回,很实用的
- Python手机开发调用DLL实现部分ADB功能
- Python 3.x 连接数据库示例(pymysql 方式)
- python-django 数据库查询方法总结
- 数据库部分---高级查询;
- python读取文本数据写入到数据库及查询优化
- 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划
- python操作数据库(pymysql 和 SQLAchemy)
- python中的查询数据库内容中用到的fetchone()函数和fetchall()函数
- python django 数据库查询
- 学习python 第三季:编写简单简单连接数据库并执行查询操作
- Python的Django框架-数据库查询(增删改查)