All of this data has been acquired and its all printing well but cannot be reflected in the database in SQLBrowser. When I printed the value of the variables it was all correct I had been using conn.commit
at the end of the code. But not even one value is obtained in the sqlite file it produced[The image shows how it looks in the sqlfile][1]
import ssl
import sqlite3
import json
ctx = ssl.create_default_context()
ctx.check_hostname= False
ctx.verify_mode = ssl.CERT_NONE
conn = sqlite3.connect('covid.sqlite')
cur = conn.cursor()
cur.executescript('''
DROP TABLE IF EXISTS Covid;
CREATE TABLE Covid (start_date TEXT, end_date TEXT, state TEXT, sex TEXT, age_group TEXT, covid_19_deaths INTEGER, total_deaths INTEGER, pneumonia_deaths INTEGER, influenza_deaths INTEGER, pneumonia_influenza_or_covid INTEGER)''')
conn.commit
hand = open('Covid.json').read()
data = json.loads(hand)
for item in data:
start_date = item['start_date']
end_date = item['end_date']
state = item['state']
sex = item['sex']
age_group = item['age_group']
covid_19_deaths = item['covid_19_deaths']
total_deaths = item['total_deaths']
pneumonia_deaths = item['pneumonia_deaths']
influenza_deaths = item['influenza_deaths']
pneumonia_influenza_or_covid = item['pneumonia_influenza_or_covid']
cur.execute('''INSERT INTO Covid (start_date)
VALUES ( ? )''', (start_date,) )
cur.execute('''INSERT INTO Covid (end_date)
VALUES ( ? )''', ( end_date,) )
cur.execute('''INSERT INTO Covid (state)
VALUES ( ? )''', ( state,) )
cur.execute('''INSERT INTO Covid (sex)
VALUES ( ? )''', ( sex,) )
cur.execute('''INSERT INTO Covid (age_group)
VALUES ( ? )''', ( age_group,) )
cur.execute('''INSERT INTO Covid (covid_19_deaths)
VALUES ( ? )''', ( covid_19_deaths,) )
cur.execute('''INSERT INTO Covid (total_deaths)
VALUES ( ? )''', ( total_deaths,) )
cur.execute('''INSERT INTO Covid (pneumonia_deaths)
VALUES ( ? )''', ( pneumonia_deaths,) )
cur.execute('''INSERT INTO Covid (influenza_deaths)
VALUES ( ? )''', ( influenza_deaths,) )
cur.execute('''INSERT INTO Covid (pneumonia_influenza_or_covid)
VALUES ( ? )''', ( pneumonia_influenza_or_covid,) )
cur.execute('''INSERT INTO Covid
(start_date,end_date,state,sex,age_group,covid_19_deaths,total_deaths,pneumonia_deaths,influenza_deaths,pneumonia_influenza_or_covid ) VALUES ( ?, ?, ? ,?, ?, ?, ?, ?, ?, ?)''',
(start_date,end_date,state,sex,age_group,covid_19_deaths,total_deaths,pneumonia_deaths,influenza_deaths,pneumonia_influenza_or_covid ))
conn.commit```
[1]: https://i.stack.imgur.com/RAdy1.png
I cannot find the mistakes i'm making in the code.
The code is not complete (no creation / initialisation of the data, and the cur.execute
statement seems incorrectly indented).
You also don't explain what the exact issue you observe is.
However there is a flagrant issue in your code: you iterate on data
for item in data:
but then rather than use item
you keep using data[0]
:
start_date = data[0]['start_date']
So you're not inserting all the stuff in data
, you're inserting the first row len(data)
times.
You also never call commit
, so it's not going to do anything:
conn.commit
PS: I reformatted the post so the code block is correct, you need the opening and closing "triple backticks" to be on their own line eg
```
for foo in bar:
...
```
not
```for foo in bar:
...```
which is what you wrote and renders to a broken snippet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.