您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎樣利用python實現mysql數據庫向sqlserver的同步,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
話不多說,直接上代碼。
#!/usr/bin/python # -*- coding:utf8 -*- # author: chenzhixin """ 一、安裝環境: python3 pip install pymysql pip install pymssql 二、實現功能: 將mysql的oa_2016.formmain_5027(手機打卡記錄)數據,增量同步到sqlserver數據庫的kaoqin.CHECKINOUT中 三、運行方法: a)定時任務 [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 四、測試sql: mysql執行 select * from oa_2016.formmain_5027 sqlserver上執行 select * from CHECKINOUT where sn='手機端打卡' """ from contextlib import contextmanager import pymysql as mysqldb import pymssql as mssqldb import time @contextmanager def get_mysql_conn(**kwargs): """ 建立MySQL數據庫連接 :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數據庫連接 :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): """ 執行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): """ 執行mysql的dml和ddl語句,不包括select語句 :param conn: :param sql: :return: """ with conn as cur: cur.execute(sql) def execute_mssql_sql(conn, sql): """ 執行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的考勤數據 :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的考勤數據同步到sqlserver數據庫里面 :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插入數據 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, '手機端打卡', 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('###############第{}條手機打卡記錄###############\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()
定時任務:
[root@oadb1 shell]# crontab -l #定時同步手機考勤給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條手機打卡記錄############### 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, '手機端打卡', 0, null )
上述就是小編為大家分享的怎樣利用python實現mysql數據庫向sqlserver的同步了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。