You are on page 1of 1

import pyodbc

import json
import requests
#import json
#import pymysql

# f.write(r.content)

#json_data=open('tygiavang.json').read()
#son_obj=json.loads(json_data)

#con=pymysql.connect(host="localhost",user="messi",password="12345678",db='json')
#cursor=con.cursor()

#for item in json_obj:


# buy=item.get("buy")
# company=item.get("company")
# brand=item.get("brand")
# updated=item.get("updated")
# brand1=item.get("brand1")
# dayf=item.get("dayf")
# id=item.get("id")
# type=item.get("type")
# cursor.execute("insert into json_file(golds) value($$$ $$$ $$
$)",buy,sell,company,brand,updated,brand1,dayf,id,type)
#con.commit()
#con.close()

conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server}; SERVER=LAPTOP-


64RF2T4B\SQLEXPRESS; Database=Socket_Account; UID=messi; PWD=12345678;')
conn.timeout=60
conn.autocommit=True

#read json from file:


r=requests.get("https://tygia.com/json.php?
ran=0&rate=0&gold=1&bank=VIETCOM&date=now")
with open('tygiavang.json','wb') as f:
data=json.load(f)
with open("employee.json", "r", encoding='utf-8') as read_file:
data = json.load(read_file)
#covert json to string
json_string=json.dumps(data)
#call sp and trap:
try:
cursor=conn.cursor()
cursor.execute('EXEC GOLDS PRICE @json',json_string)
print('inserted data')
except pyodbc.Error as err:
print('Error!!! %s'% err)
except:
print('something else misrably')
conn.close()
print('close the connection')

You might also like