##
import requests
import sqlite3
URL ="https://jsonplaceholder.typicode.com/posts"
response = requests.get(URL)
print(response.status_code)
# print(response.text)
# print('type of response:',type(response)) #<class 'requests.models.Response'>
# print('type',type(response.json())) #list
# print(type(response.json()[0])) #<class 'dict'>
myjson = response.json()
conn = sqlite3.connect('./databases/sqlite_json_ex.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS json_ex(id INTEGER PRIMARY KEY, userId text, title text, body text )")
myData =[]
for data_dict in myjson:
t = (data_dict['id'], data_dict['userId'], data_dict['title'], data_dict['body'])
myData.append(t)
c.executemany("INSERT INTO json_ex(id, userId,title,body) VALUES (?,?,?,?)",myData)
conn.commit()
Leave a Comment