首页 > 编程语言 > 详细

python查库写库例子

时间:2019-07-18 19:24:55      阅读:130      评论:0      收藏:0      [点我收藏+]

环境:

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

 

方法1:字段拼接

#!/usr/bin/python3

import pymysql
import smtplib
import datetime
import time

now_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_data

def 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.close

if __name__ == __main__ :
    querydata=query_data()
    insert_data(querydata)

 

方法2:直接写入

#!/usr/bin/python3

import pymysql
import smtplib
import datetime
import time

now_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_cnt

def 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.close

if __name__ == __main__ :
    (querydata,record_cnt) = query_data()
    if  ( record_cnt > 0) :
        insert_data(querydata)

 

python查库写库例子

原文:https://www.cnblogs.com/hxlasky/p/11209011.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!