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()
Related
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.
I have write a script to update stock in my mysql db, but i get only the last record from the xml updated, i'm looking through a tunnel right know and love to have a fresh pair of eyes looking at it.
import requests
import xml.etree.ElementTree as ET
from lxml import etree
from getpass import getpass
from mysql.connector import connect, Error
r = requests.get('http://api.edc.nl/xml/eg_xml_feed_stock.xml')
root = ET.fromstring(r.content)
for x in root.iter('product'):
id = x.find('productid').text
qty = x.find('qty').text
try:
with connect(
host="my host",
user=input("Enter username: "),
database="my database",
password=getpass("Enter password: "),
) as connection:
query = "UPDATE `ps_stock_available` SET `quantity` = " + \
qty + " WHERE `id_product` = " + id + ";"
with connection.cursor() as cursor:
cursor.execute(query)
connection.commit()
#result = cursor.fetchall()
# for row in result:
print(query)
except Error as e:
print(e)
The code that upates the table needs to be inside the for loop. Otherwise it only runs once after the loop completes, and uses the last values of the variables.
query = "UPDATE `ps_stock_available` SET `quantity` = %s WHERE `id_product` = %s"
try:
with connect(
host="my host",
user=input("Enter username: "),
database="my database",
password=getpass("Enter password: "),
) as connection:
with connection.cursor() as cursor:
for x in root.iter('product'):
prod_id = x.find('productid').text
qty = x.find('qty').text
cursor.execute(query, (qty, prod_id))
connection.commit()
except Error as e:
print(e)
Don't use id as a variable, it's the name of a built-in function.
This is my first project with SQLite.
The code runs perfect I checked and the lines look perfect.
I supposed that due to lack of knowledge of SQLite I'm making a mistake.
Problem: The code runs perfect no problem. But when I finish it doesn't print the values or even save the values in the .db file.
Full Code:
import sqlite3
import datetime
import time
conn = sqlite3.connect('covid.db')
c = conn.cursor()
def create_table():
c.execute('''CREATE TABLE IF NOT EXISTS
covidTrack(
name TEXT,
email TEXT,
ph_number INTEGER,
datestamp TEXT,
keyword TEXT)''')
i_name = input('Please insert FULL NAME : \n ...')
i_email = input('Please insert EMAIL : \n ...')
i_number = input('Please insert PHONE NUMBER : \n ...')
print('Your data has been saved for acelerated contact, thank you.')
time.sleep(3)
def data_entry():
c.execute('INSERT INTO covidTrack VALUES(?,?,?)',
(i_name, i_email, i_number))
conn.commit()
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
def read_from_db():
c.execute('''SELECT * FROM covidTrack
WHERE datestamp
BETWEEN "2021-02-06 14:50:00" AND "2021-02-06 15:00:00"''')
conn.commit()
for row in c.fetchall():
print(row)
create_table()
data_entry()
dynamic_data_entry()
read_from_db()
c.close()
conn.close()
I suppose if something wrong with the way I use conn.commit().
import sqlite3
import datetime
import time
conn = sqlite3.connect('covid.db')
c = conn.cursor()
def create_table():
c.execute('''CREATE TABLE IF NOT EXISTS
covidTrack(
name TEXT,
email TEXT,
ph_number INTEGER,
datestamp TEXT,
keyword TEXT)''')
i_name = input('Please insert FULL NAME : \n ...')
i_email = input('Please insert EMAIL : \n ...')
i_number = input('Please insert PHONE NUMBER : \n ...')
print('Your data has been saved for acelerated contact, thank you.')
time.sleep(3)
def data_entry():
date, keyword = dynamic_data_entry()
c.execute('INSERT INTO covidTrack VALUES(?, ?, ?, ?, ?)', (i_name, i_email, i_number, date, keyword))
conn.commit()
def dynamic_data_entry():
keyword = 'nameofvenue'
date = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%M-%D %h:%m:%s'))
return date, keyword
def read_from_db():
c.execute('''SELECT * FROM covidTrack''')
conn.commit()
create_table()
data_entry()
read_from_db()
for row in c.fetchall():
print(row)
c.close()
conn.close()
change the code below (make the commit call part of the function that insert the data). Do it in dynamic_data_entry as well
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
to
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
You do not actually commiting your executes. Move conn.commit after actual executes.
My usecase is to write create a temp table in the postgres database and fetch records from it and insert into a different table.
The code i used is:
import psycopg2
import sys
import pprint
from __future__ import print_function
from os.path import join,dirname,abspath
import xlrd
import os.path
newlist = []
itemidlist = []
def main():
conn_string = "host='prod-dump.cvv9i14mrv4k.us-east-1.rds.amazonaws.com' dbname='ebdb' user='ebroot' password='*********'"
# print the connection string we will use to connect
# print "Connecting to database" % (conn_string)
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
dealer_id = input("Please enter dealer_id: ")
group_id = input("Please enter group_id: ")
scriptpath = os.path.dirname('__file__')
filename = os.path.join(scriptpath, 'Winco - Gusti.xlsx')
xl_workbook = xlrd.open_workbook(filename, "rb")
xl_sheet = xl_workbook.sheet_by_index(0)
print('Sheet Name: %s' % xl_sheet.name)
row=xl_sheet.row(0)
from xlrd.sheet import ctype_text
print('(Column #) type:value')
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
#print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
num_cols = xl_sheet.ncols
for row_idx in range(0, xl_sheet.nrows): # Iterate through rows
num_cols = xl_sheet.ncols
id_obj = xl_sheet.cell(row_idx, 1) # Get cell object by row, col
itemid = id_obj.value
#if itemid not in itemidlist:
itemidlist.append(itemid)
# execute our Query
'''
cursor.execute("""
if not exists(SELECT 1 FROM model_enable AS c WHERE c.name = %s);
BEGIN;
INSERT INTO model_enable (name) VALUES (%s)
END;
""" %(itemid,itemid))
'''
cursor.execute("drop table temp_mbp1")
try:
cursor.execute("SELECT p.model_no, pc.id as PCid, g.id AS GROUPid into public.temp_mbp1 FROM products p, \
model_enable me, products_clients pc, groups g WHERE p.model_no = me.name \
and p.id = pc.product_id and pc.client_id = %s and pc.client_id = g.client_id and g.id = %s"\
% (dealer_id,group_id)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
cursor.execute("select count(*) from public.temp_mbp1")
# retrieve the records from the database
records = cursor.fetchall()
# print out the records using pretty print
# note that the NAMES of the columns are not shown, instead just indexes.
# for most people this isn't very useful so we'll show you how to return
# columns as a dictionary (hash) in the next example.
pprint.pprint(records)
if __name__ == "__main__":
main()
The try except block in between the program is not throwing any error but the table is not getting created in the postgres database as i see in the data admin.
The output shown is:
Please enter dealer_id: 90
Please enter group_id: 13
Sheet Name: Winco Full 8_15_17
(Column #) type:value
[(3263,)]
Thanks,
Santosh
You didn't commit the changes, so they aren't saved in the database. Add to the bottom, just below the pprint statement:
conn.commit()
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()