簡體   English   中英

使用python創建SQLite數據庫時出現意外錯誤

[英]Unexpected error when creating a SQLite database using python

import json
import urllib
import sqlite3

import temp


def loading():
    url = 'https://jobs.github.com/positions.json?page=1'  # URL for API 1-5json_obj = urllib.urlopen(url)
    response = urllib.urlopen(url)
    data = json.load(response)  # loads the url and set it into data variable

    for item in data[0].keys():
        print(item)
        return data  # Get the keys


# def loading():
# print " LOADING API(s)"
# urllib.urlopen('https://jobs.github.com/positions.json?page=1')
# temp = json.dumps(data[1])
# print (json.dumps(data[1]))
# print (" ")
def createDB(data):
    conn = sqlite3.connect('test.db')
    c = conn.cursor()
    # Create table
    c.execute('''CREATE TABLE example
        (description text, title text, url text, company_logo text, company text, id integer primary key, company_url text, how_to_apply text,
        location text, type text, created_at timestamp)''')
    temp_values = list(tuple())
    for item in temp:

        list_of_values = [v for k, v in item.items()]
        tuple_of_values = tuple(list_of_values)
        temp_values.append(tuple_of_values)
        c.executemany('INSERT INTO table_name VALUES (?,?,?,?,?,?,?,?,?,?,?)', temp_values)


def main():
    data = loading()
    createDB(data)


main()

我運行了它創建數據庫的代碼,但似乎沒有任何內容,這也是我得到的錯誤文件

回溯(最近一次調用):文件“/Users/issac_rodriguez/PycharmProjects/N/Sprint2/database.py”,第 45 行,在 main() 文件“/Users/issac_rodriguez/PycharmProjects/N/Sprint2/database.py” ,第 42 行,在主 createDB(data) 文件“/Users/issac_rodriguez/PycharmProjects/N/Sprint2/database.py”中,第 32 行,在 createDB 中為 temp 中的項目:TypeError: 'module' object is not iterable

看看你的createDB()函數的循環。 您嘗試遍歷temp ,它指的是您在上面導入的模塊 temp。 也許您打算遍歷temp_values 您可能還需要將參數data傳遞到temp_values 這是一個潛在的解決方案:

temp_values = list(tuple(data))
for item in temp_values:
...

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM