简体   繁体   English

我的代码没有运行,我正在尝试从 json 文件中检索数据。 我不知道我的 sqlite3 查询的问题

[英]My code is not running, I am trying to retrieve data from json file. i don't know the problem with my sqlite3 query

All of this data has been acquired and its all printing well but cannot be reflected in the database in SQLBrowser.所有这些数据都已获取并打印良好,但无法反映在 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.当我打印变量的值时,我一直在代码末尾使用conn.commit是正确的。 But not even one value is obtained in the sqlite file it produced[The image shows how it looks in the sqlfile][1]但是在它生成的 sqlite 文件中连一个值都没有得到[图片显示了它在 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).代码不完整(没有创建/初始化数据, cur.execute语句似乎缩进不正确)。

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但是,您的代码中有一个明显的问题:您迭代data

for item in data:

but then rather than use item you keep using data[0] :但是,您继续使用data[0]而不是使用item

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.因此,您不是在data中插入所有内容,而是在插入第一行len(data)次。

You also never call commit , so it's not going to do anything:你也永远不会调用commit ,所以它不会任何事情:

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 PS:我重新格式化了帖子,所以代码块是正确的,你需要打开和关闭“三重反引号”在自己的行上,例如

```
for foo in bar:
    ...
```

not不是

```for foo in bar:
    ...```

which is what you wrote and renders to a broken snippet.这是您编写并呈现为损坏的片段的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想知道我的代码没有运行的原因 - I am trying to know the reason my code is not running web 数据抓取问题我不知道如何从文件中导出信息。html 到我的 python 程序 - web scraping problem with data i don't know how to export information from file.html to my python programme 我正在尝试通过网络抓取网页并将数据存储在 CSV 文件中。 但我似乎无法让我的代码工作 - I'm trying to web scrape a webpage and store the data in a CSV file. But I can't seem to get my code to work 我正在尝试标记文本/json 文件。 不知道为什么,但只有第一条推文被标记化。 代码在下面 - I am trying to tokenize the text / json file. Dont know why , but only the first tweet is getting tokenized. the code is below 我的 web 抓取代码遇到问题 我真的不知道问题出在哪里 - I'm facing a problem with my web scraping code I don't really know the problem is 我在通过代码运行csv文件时遇到问题,但我不理解错误消息 - I am having trouble running my csv file through my code and I don't understand the error message 我的代码有语法错误,我不知道问题是什么? - I have syntax error on my code and I don't know what the problem is? 为什么不能从sqlite3上的数据库中删除? - WHy can't i delete from my database on sqlite3? 在scrapy中,我试图检索链接列表,然后在单个scrapy文件中从这些链接中刮取数据。 我的csv文件返回xpath列表。 - In scrapy I'm trying to retrieve a list of links then scrape data from these links in a single scrapy file. My csv file returns a list of xpaths. 我的代码运行良好,但我不知道为什么它显示 16 而不是 (16, 2)? - My code is running fine but I don't know why it shows 16 instead of (16, 2)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM