博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python查库写库例子
阅读量:5108 次
发布时间:2019-06-13

本文共 3522 字,大约阅读时间需要 11 分钟。

环境:

python2.7和python3.6都可以执行 

 

方法1:字段拼接

#!/usr/bin/python3import pymysqlimport smtplibimport datetimeimport timenow_time = datetime.datetime.now()yes_time = now_time + datetime.timedelta(days=-1)select_sql = "select id,user,host,db,command,time,state,info from tb_run_long_sql limit 1"select_mysqlserver="192.168.1.113"select_username="root"select_password= "yeemiao1117"select_dbname="db_admin"insert_mysqlserver="192.168.1.113"insert_username="root"insert_password= "yeemiao1117"insert_dbname="db_admin"def query_data() :    # 打开数据库连接    db = pymysql.connect(select_mysqlserver,select_username, select_password, select_dbname)    ##print(sqltext)    # 使用 cursor() 方法创建一个游标对象 cursor    cursor = db.cursor()    try:        cursor.execute(select_sql)        result_data = cursor.fetchall()    except Exception as e:        print(e)    db.close()    return result_datadef insert_data (querydata)  :    db = pymysql.connect(select_mysqlserver,select_username, select_password, select_dbname)    insert_sql="""insert into tb_run_long_sql_readonly(id,user,host,db,command,time,state,info) values (%s,%s,%s,%s,%s,%s,%s,%s)"""    cursor = db.cursor()    for row in querydata:        try:            l_id=row[0]            l_user="'" + row[1]+"'"            l_host="'" + row[2]+"'"            l_db="'"+ row[3] +"'"            l_command="'" + row[4]+ "'"            l_time=row[5]            l_state="'"+ row[6] + "'"            l_info="'" + str(row[7]) + "'"            insersql="insert into tb_run_long_sql_readonly ( id,user,host,db,command,time,state,info) values (%s,%s,%s,%s,%s,%s,%s)" % (l_id, l_user, l_host, l_db, l_command, l_time, l_state,l_info)            print(insersql)            ##cursor.execute("insert into tb_run_long_sql_readonly(id,user,host,db,command,time,state,info) values (%s,%s,%s,%s,%s,%s,%s,%s)"  %  (l_id,l_user,l_host,l_db,l_command,l_time,l_state,l_info))        except Exception as e:            print(e)    db.commit()    db.closeif __name__ == '__main__' :    querydata=query_data()    insert_data(querydata)

 

方法2:直接写入

#!/usr/bin/python3import pymysqlimport smtplibimport datetimeimport timenow_time = datetime.datetime.now()yes_time = now_time + datetime.timedelta(days=-1)select_sql = "select id,user,host,db,command,time,state,info from tb_run_long_sql where time>0 and info is not null and user not in ('dmladmin') and db not in ('db_admin') limit 100"select_mysqlserver="192.168.1.113"select_username="root"select_password= "123456"select_dbname="db_admin"insert_mysqlserver="192.168.1.113"insert_username="root"insert_password= "123456"insert_dbname="db_admin"def query_data() :    # 打开数据库连接    db = pymysql.connect(select_mysqlserver,select_username, select_password, select_dbname)    ##print(sqltext)    # 使用 cursor() 方法创建一个游标对象 cursor    cursor = db.cursor()    try:        cursor.execute(select_sql)        result_data = cursor.fetchall()        record_cnt = len(result_data)    except Exception as e:        print(e)    db.close()    return result_data,record_cntdef insert_data (querydata) :    db = pymysql.connect(insert_mysqlserver,insert_username, insert_password, insert_dbname)    insert_sql="""insert into tb_run_long_sql_readonly(id,user,host,db,command,time,state,info) values (%s,%s,%s,%s,%s,%s,%s,%s)"""    cursor = db.cursor()    cursor.executemany(insert_sql,querydata)    db.commit()    db.closeif __name__ == '__main__' :    (querydata,record_cnt) = query_data()    if  ( record_cnt > 0) :        insert_data(querydata)

 

转载于:https://www.cnblogs.com/hxlasky/p/11209011.html

你可能感兴趣的文章
ubuntu下开启root登陆
查看>>
HDOJ 2048 神、上帝以及老天爷(组合数学,全错位排列)
查看>>
'setw' : undeclared identifier
查看>>
输入任意个数字求和的小程序
查看>>
How Many Tables
查看>>
Go语言实践_实现一(客户端)对一(服务器端)聊天室
查看>>
开放API端口SIGN算法详细设计
查看>>
android pcm
查看>>
Linux下静态链接库的制作与使用
查看>>
Swift4 函数,例子代码
查看>>
012 内置锁和synchronized
查看>>
大型网站系统架构系列:负载均衡详解(二)
查看>>
如何从本地导入.nupkg文件
查看>>
Spring.NET 中配置 ashx 的映射,以及其它各种映射的处理
查看>>
Linux学习——ssh对称加密和非对称加密
查看>>
c# new的三种用法
查看>>
layui数据表格table在选项卡tabs中分页条不显示的解决
查看>>
Android中SQLite应用详解
查看>>
在oracle中创建自动增长字段
查看>>
LINQ操作符三:限制操作符
查看>>