I'm new to python and trying to work out how to insert some JSON into MySQL database in different python code. It works fine when run separately, but not works when i try to connect 2nd to 1st script. How to links 2nd python in 1st python script so it can works together?
I have 1st code like bellow, this code serves to send the image to API and generate a json file data.json
import glob
import argparse
import requests
import json
import time
import os
import cv2
import numpy as np
from pprint import pprint
import json_mysql
def main():
result = []
regions = ['id']
time_to_wait = np.inf
time_counter = 0
while True:
files = glob.glob(os.path.join("./image_dir*/*.jpg"))
files.sort(key=os.path.getmtime)
for file in files:
if os.path.isfile(file):
with open(file, 'rb') as fp:
response = requests.post(
'https://API/',
data=dict(regions=regions),
files=dict(upload=fp),
headers={'Authorization': 'Token ' + 'XXXX'})
result.append(response.json())
with open('data.json', 'w') as outfile:
json.dump(result, outfile)
time.sleep(1)
pprint(response.json())
os.remove(file)
time.sleep(1)
time_counter += 1
if time_counter > time_to_wait: break
print("waiting for file... ")
if __name__ == '__main__':
main()
json_mysql.create_db()
it generate json file like this:
And 2nd code is for create and store 'data.json' to MySQL database:
from urllib.request import urlopen
import urllib
import json
import sys
import pymysql
def dbconnect():
try:
db = pymysql.connect(
host="localhost",
user="root",
passwd="YYYY",
)
except Exception as e:
sys.exit("Can't connect to Database")
return db
def create_db():
db_name="plate_recognizer"
table_name="vehicles"
db = dbconnect()
cursor = db.cursor()
cursor.execute("SET sql_notes = 0;")
cursor.execute("CREATE DATABASE IF NOT EXISTS {}".format(db_name))
cursor.execute("SET sql_notes = 0;")
cursor.execute(
"""CREATE TABLE IF NOT EXISTS {}.{}(time varchar(150),plate varchar(20),region varchar(150), score varchar(20), filename varchar(50), tipe varchar(10));""".format(db_name, table_name))
cursor.execute("SET sql_notes = 1;")
with open('data.json') as f:
data = json.load(f)
for i in data:
cursor.execute(
"""INSERT INTO {}.{}(time, plate, region, score, filename, tipe) VALUES(%s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE plate =%s """.format
(db_name, table_name),
(i['timestamp'], i['results'][0]['plate'].upper(), i['results'][0]['region']['code'], i['results'][0]['score'], i['filename'], i['results'][0]['vehicle']['type'], i['results'][0]['plate'].upper()))
db.commit()
db.close()
if __name__ == '__main__':
create_db()
Thanks in advance.
