Python 连接 MySQL

来源:互联网 发布:知之深爱之切电子书 编辑:程序博客网 时间:2024/05/20 23:56
#coding=utf-8# 引入模块import MySQLdbimport reimport timeimport sysreload(sys)sys.setdefaultencoding('utf8')# 获取时间,可用于构造SQL语句begin_date = time.strftime("%Y-%m-%d",time.localtime(time.time()-24*60*60))end_date = time.strftime("%Y-%m-%d",time.localtime(time.time()-24*60*60))begin_partition = time.strftime("%Y%m%d",time.localtime(time.time()-24*60*60))end_partition = time.strftime("%Y%m%d",time.localtime(time.time()-24*60*60))# 连接MYSQLconn= MySQLdb.connect(        host='192.168.1.111',        port = 3306,        user='zjf',        passwd='zjf@2017!@#',        db ='mqdata',        charset='utf8'        )cur = conn.cursor()# 设置查询语句push=cur.execute("select id,code from dg_tj_map where id between 9915 and 9918")# 提交查询语句info = cur.fetchall()# 打印结果print(info)关闭连接cur.close()conn.commit()conn.close()
原创粉丝点击