Python Forum
Python - help with getting JSON from one DB and load to another DB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - help with getting JSON from one DB and load to another DB
#5
Thing is, I need/want to accomplish this in Python only and have been trying to insert only one row with one column. I have created 2 equal tables in Oracle and MSSQL, like this:

#create table with test data in Oracle
create table test_t (
onecol varchar2 (50)
)
INSERT INTO test_t
(onecol)
VALUES
('testinginsert');
And

#create table in MSSQL to insert data to
create table test_t (
onecol varchar (50)
)
commit;
And here is updated code: It is giving me 'pyodbc.Error: ('07002'. '[07002]' [Microsoft][ODBC Driver 17 for SQL Server]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')'

#Importing modules

import cx_Oracle
import pyodbc
import json
import MySQLdb
import pytds


#Create Orcle DB connection

dsn_tns = cx_Oracle.makedsn('zz', 'zzz', 'zz')
conn = cx_Oracle.connect(user='xx', password='xx', dsn=dsn_tns)
c = conn.cursor()


#Allows to pass date objects if there are any to avoid JSON not serializable error

class DatetimeEncoder(json.JSONEncoder):
    def default(self, obj): # pylint: disable=method-hidden
        try:
            return super(DatetimeEncoder, obj).default(obj)
        except TypeError:
            return str(obj)

cursor_orcl = conn.cursor()
cursor_orcl.arraysize= 50000

#Option 1:
# Each row in the database becomes one dictionary and each field in the row a key-value pair, producing JSON output
#r = [dict((cursor_orcl.description[i][0], value) \
#               for i, value in enumerate(row)) for row in cursor_orcl.fetchall()]    
#cursor_orcl.connection.close()
#Checking JSON
# print(json.dumps(r,cls=DatetimeEncoder, indent=2))

#Option 2:
cursor_orcl.execute("""
                    Select * from test_t where rownum <=1
            """)

rows = cursor_orcl.fetchall()
columns = [desc[0] for desc in cursor_orcl.description]
result = []

for row in rows:
    row = dict(zip(columns, row))
    result.append(row) 

#Checking JSON output
print ((result))

 
#Create MSSQL connectionc
server = 'aaaa'
database = 'aaa'
username = 'aaa'
password = 'aaa'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

cursor_insert = cnxn.cursor()
cursor_insert.arraysize= 50000
sql = (
    "INSERT INTO dbo.test_t (onecol) " + " VALUES("
    "SELECT onecol " +
    "FROM OPENJSON(?))"
      )

cursor_insert.execute(sql)   
cursor_insert.close ()
cnxn.commit ()
Reply


Messages In This Thread
RE: Python - help with getting JSON from one DB and load to another DB - by qIekm - Apr-16-2020, 07:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 452 Oct-31-2023, 10:43 AM
Last Post: piyushd
  JSON Dump and JSON Load foxholenoob 8 1,151 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Python Split json into separate json based on node value CzarR 1 5,678 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  How to load log.txt directly into python codes? sparkt 6 3,002 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Load table from Oracle to MYSQL using python himupant94 0 1,667 May-12-2020, 04:50 PM
Last Post: himupant94
  difficulties to chage json data structure using json module in python Sibdar 1 2,101 Apr-03-2020, 06:47 PM
Last Post: micseydel
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,917 Dec-11-2019, 07:50 AM
Last Post: parsley
  python and load balancer pythonFresher 1 2,294 Jul-18-2019, 07:23 AM
Last Post: pythonFresher
  Load JSON file data into mongodb using pymongo klllmmm 1 11,889 Jun-28-2019, 12:47 AM
Last Post: klllmmm
  json.load causing IndexError: list index out of range WallyZ 5 6,242 May-20-2019, 01:11 PM
Last Post: WallyZ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020