這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎樣利用python實(shí)現(xiàn)MySQL數(shù)據(jù)庫向sqlserver的同步,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),扎賚諾爾企業(yè)網(wǎng)站建設(shè),扎賚諾爾品牌網(wǎng)站建設(shè),網(wǎng)站定制,扎賚諾爾網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,扎賚諾爾網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
話不多說,直接上代碼。
#!/usr/bin/python # -*- coding:utf8 -*- # author: chenzhixin """ 一、安裝環(huán)境: python3 pip install pymysql pip install pymssql 二、實(shí)現(xiàn)功能: 將mysql的oa_2016.formmain_5027(手機(jī)打卡記錄)數(shù)據(jù),增量同步到sqlserver數(shù)據(jù)庫的kaoqin.CHECKINOUT中 三、運(yùn)行方法: a)定時(shí)任務(wù) [root@oadb1 shell]# crontab -l * * * * * python -W ignore /usr/local/shell/sync_mobile_kaoqin.py >> /var/log/sync_mobile_kaoqin.log 2>&1 b) 日志位置 tail -f /var/log/sync_mobile_kaoqin.log 四、測(cè)試sql: mysql執(zhí)行 select * from oa_2016.formmain_5027 sqlserver上執(zhí)行 select * from CHECKINOUT where sn='手機(jī)端打卡' """ from contextlib import contextmanager import pymysql as mysqldb import pymssql as mssqldb import time @contextmanager def get_mysql_conn(**kwargs): """ 建立MySQL數(shù)據(jù)庫連接 :param kwargs: :return: """ conn = mysqldb.connect(host=kwargs.get('host', 'localhost'), user=kwargs.get('user'), password=kwargs.get('password'), port=kwargs.get('port', 3306), database=kwargs.get('database') ) try: yield conn finally: if conn: conn.close() @contextmanager def get_mssql_conn(**kwargs): """ 建立sqlserver數(shù)據(jù)庫連接 :param kwargs: :return: """ conn = mssqldb.connect(server=kwargs.get('host'), user=kwargs.get('user'), password=kwargs.get('password'), database=kwargs.get('database') ) try: yield conn finally: if conn: conn.close def execute_mysql_select_sql(conn, sql): """ 執(zhí)行mysql的select類型語句 :param conn: :param sql: :return: """ with conn as cur: cur.execute(sql) rows = cur.fetchall() return rows def execute_mysql_sql(conn, sql): """ 執(zhí)行mysql的dml和ddl語句,不包括select語句 :param conn: :param sql: :return: """ with conn as cur: cur.execute(sql) def execute_mssql_sql(conn, sql): """ 執(zhí)行sqlserver的dml和ddl語句,不包含select語句 :param conn: :param sql: :return: """ with conn.cursor() as cur: cur.execute(sql) conn.commit() def get_mysql_kaoqin_data(conn): """ 獲取mysql的考勤數(shù)據(jù) :param conn: :return: """ sql = "select * from formmain_5027 where field0008 is null or field0008=''" mysql_kaoqin_data_rows = execute_mysql_select_sql(conn, sql) return mysql_kaoqin_data_rows def mysql_sync_to_sqlserver(mysql_conn, mssql_conn, data): """ 把mysql的考勤數(shù)據(jù)同步到sqlserver數(shù)據(jù)庫里面 :param mysql_conn: :param mssql_conn: :param data: :return: """ for index, row in enumerate(data, 1): ID=row[0] state=row[1] start_member_id=row[2] start_date=row[3] approve_member_id=row[4] approve_date=row[5] finishedflag=row[6] ratifyflag=row[7] ratify_member_id=row[8] ratify_date=row[9] sort=row[10] modify_member_id=row[11] modify_date=row[12] field0001=row[13] field0002=row[14] field0003=row[15] field0004=row[16] field0005=row[17] field0006=row[18] field0007=row[19] field0008=row[20] field0009=row[21] #向sqlserver插入數(shù)據(jù) insert_data = """ INSERT INTO [kaoqin].[dbo].[CHECKINOUT] ([USERID] ,[CHECKTIME] ,[CHECKTYPE] ,[VERIFYCODE] ,[SENSORID] ,[Memoinfo] ,[WorkCode] ,[sn] ,[UserExtFmt] ,[Synced]) VALUES ((select userid from USERINFO where BADGENUMBER='{userid}'), '{CHECKTIME}', 'I', 1, 1, NULL, 0, '手機(jī)端打卡', 0, null )""".format(userid=field0002, CHECKTIME=start_date) execute_mssql_sql(mssql_conn, insert_data) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) print('###############第{}條手機(jī)打卡記錄###############\n'.format(index), insert_data) marked_sql = "update formmain_5027 set field0008='synced' where id={}".format(ID) execute_mysql_sql(mysql_conn, marked_sql) def main(): mysql_conn_args = dict(user='root', host='127.0.0.1', password='*******', database='oa_2016') mssql_conn_args = dict(host='172.x.x.x', user='sa', password='********', database='kaoqin') with get_mysql_conn(**mysql_conn_args) as mysql_conn: mysql_data = get_mysql_kaoqin_data(mysql_conn) with get_mssql_conn(**mssql_conn_args) as mssql_conn: mysql_sync_to_sqlserver(mysql_conn, mssql_conn, mysql_data) if __name__ == '__main__': main()
定時(shí)任務(wù):
[root@oadb1 shell]# crontab -l #定時(shí)同步手機(jī)考勤給sqlserver * * * * * python -W ignore /usr/local/shell/sync_mobile_kaoqin.py >> /var/log/sync_mobile_kaoqin.log 2>&1
日志:
[root@oadb1 shell]# tail -100f /var/log/sync_mobile_kaoqin.log 2019-10-20 09:04:01 ###############第1條手機(jī)打卡記錄############### INSERT INTO [kaoqin].[dbo].[CHECKINOUT] ([USERID] ,[CHECKTIME] ,[CHECKTYPE] ,[VERIFYCODE] ,[SENSORID] ,[Memoinfo] ,[WorkCode] ,[sn] ,[UserExtFmt] ,[Synced]) VALUES ((select userid from USERINFO where BADGENUMBER='1234'), '2019-10-19 14:50:25', 'I', 1, 1, NULL, 0, '手機(jī)端打卡', 0, null )
上述就是小編為大家分享的怎樣利用python實(shí)現(xiàn)mysql數(shù)據(jù)庫向sqlserver的同步了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。