简体   繁体   English

如何使用Python将从MySQL表中选择的行列成功插入到目标MSSQL表中?

[英]How can I successfully insert row columns selected from MySQL table into a destination MSSQL table using Python?

This python script connects to MySQL database and MssQL 2008 R2 database. 此python脚本连接到MySQL数据库和MssQL 2008 R2数据库。 MySQL database runs on Linux Ubuntu 11.04 . MySQL数据库在Linux Ubuntu 11.04上运行。 MssQL 2008 runs on Windows . MssQL 2008Windows运行。 The script runs from Linux (Ubuntu 11.04). 该脚本从Linux(Ubuntu 11.04)运行。

            #!/usr/bin/python

            import pymssql as ms
            import MySQLdb as mdb
            import sys

            //Connection to MSSQL
            #Connection to MSSQL
            connMSSQL=ms.connect(host='192.168.8.52', user='sa', password='hostailpw321', database='hostail', as_dict=True)

            //Connection to MySQL
            #Connection to MySQL
            connMySQL=mdb.connect('localhost', 'root', 'trail123', 'trail');

            //Cursor to MSSQL
            #Cursor to MSSQL
            curMSSQL=connMSSQL.cursor()

            //Cursor to MySQL
            #Cursor to MySQL
            curMySQL=connMySQL.cursor(mdb.cursors.DictCursor)
            curMySQL.execute('SELECT * FROM clinics_mapping')

            //Get Data from MySQL 
            #Get Data from MySQL server
            for rowMySQL in curMySQL:
            #print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
            #curMSSQL.executemany("INSERT INTO clinics values(%s,%s)", [(rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])]) 
                    names = rowMySQL['Clinic_name']
                    codes = rowMySQL['Clinic_code']
                    qryINS="INSERT INTO clinics(name,code)values('%s','%s')" %(str(names),str(codes))
                    curMSSQL.execute(qryINS)
            #print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
            #When I print qryINS I get a query that executes perfect in MSSQL 2008 R2 query editor      
                    print qryINS

            //Close MSSQL connection
            #Close MSSQL connection
            connMSSQL.close()

            //Close MySQL connection
            #Close MySQL connection
            connMySQL.close()

The problem got resolved once I called commit() as follows connMSSQL.commit() immediately after curMSSQL.execute(qryINS) line 一旦我在curMSSQL.execute(qryINS)行之后立即按如下方式调用connMSSQL.commit(),就解决了该问题

        #!/usr/bin/python

        import pymssql as ms
        import MySQLdb as mdb
        import sys

        #Connection to MSSQL
        connMSSQL=ms.connect(host='192.168.8.52', user='sa', password='hostailpw321', database='hostail', as_dict=True)

        #Connection to MySQL
        connMySQL=mdb.connect('localhost', 'root', 'trail123', 'trail');

        #Cursor to MSSQL
        curMSSQL=connMSSQL.cursor()

        #Cursor to MySQL
        curMySQL=connMySQL.cursor(mdb.cursors.DictCursor)
        curMySQL.execute('SELECT * FROM clinics_mapping')

        #Get Data from MySQL server
        for rowMySQL in curMySQL:
        #print (rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])
        #curMSSQL.executemany("INSERT INTO clinics values(%s,%s)", [(rowMySQL['Clinic_name'],rowMySQL['Clinic_code'])]) 
                names = rowMySQL['Clinic_name']
                codes = rowMySQL['Clinic_code']
                qryINS="INSERT INTO clinics(name,code)values('%s','%s')" %(str(names),str(codes))
                curMSSQL.execute(qryINS)
                connMSSQL.commit()

                """
                I had not called commit() which persists your data if you had not put autocommit to True.I have called it as connMSSQL.commit() in my script.
                """

        #Close MSSQL connection
        connMSSQL.close()

        #Close MySQL connection
        connMySQL.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有可变列数的Python中插入到MySQL表中? - How can I INSERT into a MySQL table in Python with a variable number of columns? 如何使用python更快地将行插入mysql表? - How can I insert rows into mysql table faster using python? 如何使用 Python 将单个数据插入 Mysql 表中? - How can i insert a single data into a Mysql table using Python? 如何在Python中的MySQL表中插入一行? - how do I insert a row to a MySQL table in Python? 如何将表存储在变量中,每行作为元素和分隔符,以便在Python中使用BeautifulSoup来区分列? - How can I store a table in a variable with each row as an element and a delimiter to distinguish columns using BeautifulSoup in Python? 如何使用pymysql通过存储过程将pyodbc mssql查询返回的列表插入到mysql中 - How can I insert a list returned from pyodbc mssql query into mysql through stored procedure using pymysql 如何使用python将变量插入MySQL表 - How to insert variable to MySQL table using python 无法使用Python在表中插入或在MySQL中创建表 - Can't insert in a table or create a table in MySQL using Python 我如何将dataframe(在python中)的数据插入到greenplum表中? - How i can insert data from dataframe(in python) to greenplum table? 如何使用Python3从sqlite表中删除某些行/行? - How can I delete certain row/rows from sqlite table using Python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM