python - 使用 python 创建 SQLite 数据库时出现意外错误
问题描述
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
which 指的是您在上面导入的模块 temp。也许您打算遍历temp_values
?您可能还需要将您的论点传递data
到temp_values
. 这是一个潜在的解决方案:
temp_values = list(tuple(data))
for item in temp_values:
...
希望这可以帮助!
推荐阅读
- mongodb - MongoDB ObjectID 生成规则影响插入顺序
- spring - 是否需要在Github中以客户端的spring应用程序名称命名属性文件
- javascript - 为什么在过滤简单对象数组时会发生突变?
- c# - 使用 Xamarin Forms 最新更新包含来自其他 XAML 文件的样式的方法是什么
- jquery - 数据表搜索框未显示
- ignite - 点燃集群和 Kubernetes 集成
- html - 如何将多张桌子变成一张桌子
- ios - lldb Xcode 没有响应 | Fabric .pcm 文件复制
- html - 选择中的 Onchange 属性未在 codeigniter 中触发
- javascript - 从 localhost:xxxx 加载时的内容安全策略 ( style-src )