繁体   English   中英

Python SQLite从变量插入数据

[英]Python SQLite insert data from variables

我正在尝试将变量的内容添加到SQLite数据库中,但出现错误

sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

我的代码是:-

import requests
import json
import eventlet
import os
import sqlite3

#Get the currect vuln_sets
response = requests.get('https://vulners.com/api/v3/search/stats/')
vuln_set = json.loads(response.text)
vuln_type = vuln_set['data']['type_results']
for k in vuln_type:
    vuln_bulletinfamily = vuln_set['data']['type_results'][k]['bulletinFamily']
    vuln_name = vuln_set['data']['type_results'][k]['displayName']
    vuln_count = vuln_set['data']['type_results'][k]['count']



con = sqlite3.connect('vuln_sets.db')
with con:
    cur = con.cursor()
    con.row_factory = sqlite3.Row


    cur.execute("REPLACE INTO vuln_sets (vulntype, displayname, bulletinfamily, count) values (?, ?, ?, ?)", (vuln_type, vuln_bulletinfamily, vuln_name, vuln_count))
    con.commit()

变量包含JSON密钥对,因为我需要将其中一些插入数据库以进行处理,但需要一个不同的项目。

stacktrace是:

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    cur.execute("REPLACE INTO vuln_sets (vulntype, displayname, bulletinfamily, count) values (?, ?, ?, ?);", (vuln_type, vuln_bulletinfamily, vuln_name, vuln_count))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

@roganjosh的评论已修复! 我需要在for循环中包含数据库事务,如下所示:

import requests
import json
import eventlet
import os
import sqlite3

#Get the currect vuln_sets
response = requests.get('https://vulners.com/api/v3/search/stats/')
vuln_set = json.loads(response.text)
vuln_type = vuln_set['data']['type_results']
for k in vuln_type:
    vuln_bulletinfamily = vuln_set['data']['type_results'][k]['bulletinFamily']
    vuln_name = vuln_set['data']['type_results'][k]['displayName']
    vuln_count = vuln_set['data']['type_results'][k]['count']
    con = sqlite3.connect('vuln_sets.db')
    with con:
        cur = con.cursor()
        con.row_factory = sqlite3.Row    
        cur.execute("REPLACE INTO vuln_sets (vulntype, displayname, bulletinfamily, count) values (?, ?, ?, ?)", (k, vuln_name, vuln_bulletinfamily, vuln_count))
        con.commit()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM