我在使用python时遇到了麻烦(让我说我是一个初学者...)我试图解析由api URL下载的以下json数据(数据的一部分)...
{
"sha": "cffff88d9f69932845ea770b09bfdfbdd3c23ed9",
"node_id": "MDY6Q29tbWl0NjUyNzU5NTM6Y2ZmZmY4OGQ5ZjY5OTMyODQ1ZWE3NzBiMDliZmRmYmRkMzJmZDAzYQ==",
"commit": {
"author": {
"name": "Anton",
"email": "user1@test1.com",
"date": "2018-09-18T08:46:12Z"
},
"committer": {
"name": "Anton",
"email": "user1@test1.com",
"date": "2018-09-18T08:46:12Z"
},
"message": "Release 2.0.0",
"author": {
"login": "Tony",
}
}
一旦我解析了这些数据,我想将它们存储在具有“ sha-date-author-message”列的“ users”表中(使用SQLite)
|安东| 2018-09-18T08:46:12Z |托尼版本2.0.0 |
(这是我期望python能够做到的...)
我已经使用请求库从api检索数据,但是当我尝试将数据存储到列中时python出错。我使用了for循环来存储数据...
api_url = requests.get('https://.......)
data_json = api_url.json()
sqlite_file = 'users.db'
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS users")
c.execute("CREATE TABLE IF NOT EXISTS users (sha TEXT, date TEXT, author
TEXT, message TEXT, is_external INTEGER)")
for item in data_json:
sha = item['sha']
date = item['commit']['author']['date']
author = item['author']['login']
message = item['commit']['message']
c.execute("INSERT INTO users (sha,date,author,message) VALUES (?,?,?,?)", sha, date, author, message)
conn.commit()
conn.close()
错误如下:
c.execute(“ INSERT INTO users(sha,date,author,message)VALUES (?,?,?,?)“,sha,date,author,消息)TypeError:函数最多需要 2个参数(给定5个参数)
我被困住了...感谢您的帮助!
答案 0 :(得分:1)
您需要将参数作为一个参数提供,因此使其可迭代-例如列表或元组。
c.execute("INSERT INTO users (sha,date,author,message) VALUES (?,?,?,?)", [sha, date, author, message])
-或-
c.execute("INSERT INTO users (sha,date,author,message) VALUES (?,?,?,?)", (sha, date, author, message))