Python SQLite insert data from variables - python

I am trying to add the contents of variables into a SQLite DB but I am getting an error of
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
My code is:-
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()
The variables contain the JSON key pairs as I need to insert some of them into the DB for processing but a different project.
The stacktrace is:
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's comment fixed it! I needed to incude the DB transactions in the for loop as below:
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()

Related

Save PLC EasyModbus data

I need to save data from a PLC to a PostgreSQL database for which I have this code:
#!/usr/bin/env python
'''
Created on 12.09.2016
#author: Stefan Rossmann
'''
# #UnresolvedImport
from easymodbus.modbusClient import *
# #UnresolvedImport
import psycopg2
conn = psycopg2.connect(host="127.0.0.1",
port="5432",
user="americo",
password="123456",
database="rapidscada")
modbusClient = ModbusClient('192.168.5.1', 502)
#modbusClient = ModbusClient('COM4')
modbusClient.connect()
#discreteInputs = modbusClient.read_discreteinputs(3, 1)
holding_registers = modbusClient.read_holdingregisters(3, 1)
print (holding_registers)
cursor = conn.cursor()
cursor.execute("INSERT INTO mitablita (datito, detalle) VALUES(%s, %s)", (holding_registers, 'tg1010_2'))
conn.commit() # <- We MUST commit to reflect the inserted data
cursor.close()
conn.close()
modbusClient.close()
It gives me data type error. This is strange because the data to save and the table field are both integers.

Reset row_factory attribute of an Python sqlite3 object

How can I reset the "row_factory" attribute of an sqlite3 connection object after setting it to "sqlite3.Row"? For instance:
import sqlite3
con = sqlite3.connect("db.dat", isolation_level=None)
con.row_factory = sqlite3.Row
cur = con.execute("insert into test1 (name, last) values (?, ?)", ("John","doe"))
cur = con.execute("insert into test1 (name, last) values (?, ?)", ("Liz","doe"))
cur.execute("select * from test1")
cur.fetchone() # gets {'name':'john', 'last':'doe'}
# Now I want to get a tuple when calling fetchone
con.row_factory = None
cur.fetchone() #Throw error
delattrib(con, "row_factory") # causes segmentation fault on Windows
I can't find an answer anywhere and the only solution (other than casting the dictionary) is to reopen the db connection. I read the documentation for the sqlite3 module, but it doesn't mention anything to reset the attribute back to its default state.
Thank you

How do you insert SQL data into a database using Python?

I am attempting to create a program that reads 3 sensors from a raspberry pi sense hat, and insert those values into a sql database, using python. When I execute the program, no errors are given, but when I try to select the data using python, or even going into the database itself, the data is nowhere to be found, I believe the data isn't being properly written. How would I go about correcting this?
import sqlite3
import datetime
from sense_hat import SenseHat
sense = SenseHat()
sensePressure = int(sense.get_pressure())
senseTemp = int(sense.get_temperature() * 1.8 + 32 - 18.85)
senseHumidity = int(sense.get_humidity())
currDateTime = datetime.datetime.now()
COUNT = 1
conn = sqlite3.connect('sense.db')
c = conn.cursor()
c.execute("INSERT INTO atmos (ITEM, DATETIME, TEMPERATURE, HUMIDITY, PRESSURE) VALUES (?, ?, ?, ?,
?)", (COUNT, currDateTime, senseTemp, sensePressure, senseHumidity))
conn.commit()
conn.close()
If you are writing to any SQL database, you will be better of using the pyodbc library it is quite effective and very easy to use :
import pyodbc
connection = pyodbc.connect('Driver={SQL Server};'
'Server=server_name;'
'Database=db_name;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.execute("INSERT INTO atmos (ITEM, DATETIME, TEMPERATURE, HUMIDITY, PRESSURE)
VALUES (?, ?, ?, ?, ?)", COUNT, currDateTime, senseTemp,
sensePressure, senseHumidity)

sqlite3.OperationalError: near "'...'": syntax error

INPUT FILE:
$ cat inputs.csv
'18-01-2019', 296.0
'18-01-2019', 296.0
'18-01-2019', 296.0
CODE:
import csv
import sqlite3
import pprint
conn = sqlite3.connect('metrics.db')
c = conn.cursor()
def read_file(filename):
with open(filename, 'r') as f:
yield from f
for row in read_file('inputs.csv'):
data = row.split(',')
values = '({}, {})'.format(data[0], data[1].strip())
print('Values are: {}'.format(values))
try:
query = '\'INSERT INTO metric_db VALUES (?, ?)\', {}'.format(values)
print('Query is: {}'.format(query))
c.execute(query)
except sqlite3.IntegrityError as e:
pass
conn.commit()
conn.close()
OUTPUT ERROR:
Values are: ('18-01-2019', 296.0)
Query is: 'INSERT INTO metric_db VALUES (?, ?)', ('18-01-2019', 296.0)
Traceback (most recent call last):
File "write_to_db.py", line 21, in <module>
c.execute(query)
sqlite3.OperationalError: near "'INSERT INTO metric_db VALUES (?, ?)'": syntax error
I thought this was easier. Quite a few SO threads on this error. But I am still not there yet :(
Change your query statement:
query = 'INSERT INTO metric_db VALUES {}'.format(values)
EDIT
To Avoid SQL injection and use correct date format:
import csv
import sqlite3
import pprint
from datetime import datetime
conn = sqlite3.connect('metrics.db')
c = conn.cursor()
def read_file(filename):
with open(filename, 'r') as f:
yield from f
for row in read_file('inputs.csv'):
data = row.split(',')
values = '({}, {})'.format(data[0], data[1].strip())
print('Values are: {}'.format(values))
date_readed = datetime.strptime(data[0], '%d-%m-%Y').strftime('%Y-%m-%d')
try:
query = 'INSERT INTO metric_db VALUES (?, ?)'
c.execute(query,(date_readed,data[1],))
except sqlite3.IntegrityError as e:
pass
conn.commit()
conn.close()
Check your schema.sql file. just try to copy-paste this file from tutorial.

Database file created, no data/schema

The .db file gets created inside the provided path. The printed output shows in the terminal window, but there is no data/schema inside the .db file. No errors come up, syntax seems right. Here's the code. I'm at a loss as to why,conn.commit() are in the correct areas. Any information would be greatly appreciated.
from scapy.all import *
from scapy.layers import dhcp
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
runtime = logging.getLogger('scapy.runtime')
runtime.setLevel(logging.ERROR)
loading = logging.getLogger('scapy.loading')
loading.setLevel(logging.ERROR)
from scapy.layers.l2 import Ether
from scapy.layers.all import BOOTP
from scapy.layers.all import DHCP, DHCPTypes, DHCPOptions, DHCPRevOptions
from scapy import route
import urllib
import urllib3
from urllib.request import urlopen
import os
import sqlite3
from datetime import datetime
import sys
DIR_NAME = os.path.dirname(__file__)
db_path = os.path.join(DIR_NAME, "/home/dtman/Desktop/Secure_DHCP /secureDHCP-DB.db")
conn = sqlite3.connect(db_path)
c = conn.cursor()
conn.commit()
def make_table():
c.execute('CREATE TABLE IF NOT EXISTS secureDHCP(mac VARCHAR(50), vendorId VARCHAR(50), time VARCHAR(50), oData VARCHAR(50)')
conn.commit()
c.close()
conn.close()
def the_data():
mac = src_mac()
vendorId = vendor()
time = timeData()
oData = optData()
c.execute("INSERT INTO secureDHCP (mac, vendorId, time, oData) VALUES (?, ?, ?, ?)",
(src_mac, vendor, timeDate, optData))
conn.commit()
s=socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))
print("Input network interface")
interface = input()
def pkt_data(pkt):
src_mac = pkt.getlayer(Ether).fields['src']
url = 'http://api.macvendors.com/' + src_mac
r = urlopen(url)
vendor = r.read()
timeData = str(datetime.now())
full_options = pkt.getlayer(DHCP).fields['options']
dhcp_options = [o for o in full_options if isinstance(o, tuple)]
for x in dhcp_options:
if x[0] in ('message-type', 'requested_addr', 'hostname'):
optData = x[1]
print('MAC: {} /// Vendor: {} /// Time {} /// optData: {}'.format(src_mac, vendor, timeData, optData))
sniff(iface=interface, prn=pkt_data, filter='udp port (67 or 68)', store=0)
Output in terminal:
`MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: 3
MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: 192.168.1.4
MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: b'HOSTNAME'`
first of all, you didn't call the method like make_table().Second,you call make_table()
and inside it,why close the conn?how yourthe_data()insert the date?Please give the min sample.something like below will never wrong.
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS secureDHCP(mac VARCHAR(50), vendorId VARCHAR(50), time VARCHAR(50), oData VARCHAR(50)')
conn.commit()
c.execute("INSERT INTO secureDHCP (mac, vendorId, time, oData) VALUES (?, ?, ?, ?)",
(src_mac, vendor, timeDate, optData))
conn.commit()
c.close()
conn.close()

Categories

Resources