# Author: yeshengbao # -- coding: utf-8 -- # @Time : 2018/4/16 19:23 import pymysql # 创建连接 conn = pymysql.connect(host='localhost', port=3306, user='root', password="123456", db="test", charset='utf8') # 获取游标 cursor = conn.cursor() # 修改 # sql_insert = """update user set name='zyx' where id=2""" #(name='zyx'为修改的内容) where为过滤 # cursor.execute(sql_insert) # 查找 sql_select = """select * from user where name like 'y%'""" aaa = cursor.fetchall() # fetchall:将返回所有结果,返回二维元组,如(('id','title'),('id','title')),
cursor.execute(sql_select)
for i in aaa:
ids = i[0] names = i[1]
sql_insert = """insert into users(id, name)values({0},"{1}")""".format(ids, names) # 记住在sql语句里尽量使用双引号, 不然可能会引起报错,详情, 看mysql手册
conn.commit()
conn.close()
查询某个月的数据(查询17年10月份数据)
select * from exam where date_format(starttime,'%Y-%m')='2017-10'
查询指定时间段的数据
select fullName,addedTime FROM t_user where addedTime >='2017-1-1 00:00:00' and addedTime < '2018-1-1 00:00:00';