-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathApiDb.py
More file actions
38 lines (34 loc) · 1.08 KB
/
ApiDb.py
File metadata and controls
38 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
import sqlite3
import json
import csv
import sys
import os
import random
scriptDir = os.path.dirname(os.path.realpath(__file__)) # current working Folder/Directory
dbFilepAth = scriptDir + os.path.sep + "api.db"
connection = sqlite3.connect(dbFilepAth)
crsr = connection.cursor()
sql_command = """ CREATE TABLE IF NOT EXISTS apitest (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userId varchar(255),
title varchar(255) ,
completed varchar(255)
); """
# execute the statement
crsr.execute(sql_command)
connection.commit()
n = random.randint(1, 50)
url = "https://jsonplaceholder.typicode.com/todos/{}".format(n)
x = requests.get(url)
if x.status_code == 200 :
data = json.loads(x.text)
print(data)
del data['id']
columns = ', '.join(data.keys())
placeholders = ', '.join('?' * len(data))
sql = 'INSERT INTO apitest ({}) VALUES ({})'.format(columns, placeholders)
print(sql)
values = tuple(data.values())
crsr.execute(sql,values)
connection.commit()