Using a for loop in python to generate mySQL insert statements - python

I am working on a python script to take an incoming String of data from several arduinos to read, split and insert that data into a database.
The problem is the sensor data varies in the number of values depending on what kind of sensor is used. I cannot figure out the proper way to loop through the separated pieces and insert them properly.
A '1' specifies 10HS sensor and need 1 space for the incoming value
The String comes in like this for 10HS sensors:
"Cucumber2015,Arduino01,1,20150918124200,25.3,75.5,1,12 .....
A '2' specifies 10HS sensor and need 1 space for the incoming value
"Cucumber2015,Arduino01,1,20150918124200,25.3,75.5,2,12,24,23 ......
The for loop should repeat until all the sensor values have an insert statement for their respective tables.
I have tried the code shown below and keep getting errors.
How can I accomplish this? Besides the syntax problem, am I going about this correctly?
My current error
File "serialToDbV3.py", line 50
index =index+1
^
SyntaxError: invalid syntax
Python code
#!/usr/bin/python
import serial
import MySQLdb
#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","python_user","password","IrrigationDB") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()
device = '/dev/ttyUSB0' #this will have to be changed to the serial port you are using
baudrate = 9600
def getSerialData():
try:
print "Trying...",device
arduino = serial.Serial(device, baudrate)
except:
print "Failed to connect on",device
try:
print "Trying to get data"
next(arduino)
data = arduino.readline() #read the data from the Arduino
pieces = data.split(",") #split the data by the tab
print "Data: %s" % data
print "Piece 0: ProjectID %s" % pieces[0]
print "Piece 1: ArduinoID %s" % pieces[1]
print "Piece 2: Plot# %s" % pieces[2]
print "Piece 3: SQLTime %s" % pieces[3]
print "Piece 4: AirTemp %s" % pieces[4]
print "Piece 5: Humidity %s" % pieces[5]
print "Piece 6: SensType %s" % pieces[6]
print "Piece 7: SensData %s" % pieces[7]
print "Piece 8: %s" % pieces[8]
print "Piece 9: %s" % pieces[9]
print "Piece 10: %s" % pieces[10]
#Here we are going to insert the data into the Database
try:
print "Trying insertion..."
cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Project`(`idProject`)VALUES (%s)", (pieces[0]))
cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Arduino`(`idArduino`,`FK_ProjectID`)VALUES (%s,%s)", (pieces[1],pieces[0]))
cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Plot`(`idPlot`,`FK_ArduinoID`)VALUES (%s,%s)", (pieces[2],pieces[1]))
cursor.execute("INSERT INTO `IrrigationDB`.`Reading`(`DateAndTime`,`airTempC`,`relativeHumidity`,`FK_PlotID`)VALUES (%s,%s,%s,%s)", (pieces[3],pieces[4],pieces[5],pieces[2]))
startingPosition = 6
for index in xrange(startingPosition, len(pieces), step):
if pieces[6] == 1 :
cursor.execute("INSERT INTO `IrrigationDB`.`10HS_Sensor`(`id10HS_Sensor`,`DielectricPermittivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1])
index =index+1
if pieces[6] == 2 :
cursor.execute("INSERT INTO `IrrigationDB`.`GS3_Sensor`(`idGS3_Sensor`,`DielectricPermittivity`,`soilTempC`,`electricalConductivity`)VALUES (%s,%s,%s)", (pieces[2],pieces[i+1],pieces[i+2],pieces[i+3]))
index=index+3
except MySQLdb.IntegrityError:
print "failed to insert data"
except:
print "Failed to get data from Arduino!"
val = 0
while val == 0 :
getSerialData()

Use proper exception reporting, at least until you get to end-user land.
try:
cursor.execute("INSERT IGNORE INTO `IrrigationDB`.`Project`(`idProject`)VALUES (%s)", (pieces[0]))
except Exception, e:
print "this is sql #6", e
raise
This will tell you more clearly what is going on, which you can then post here.

Related

Nmap scan using python and store in mysql

python code using nmap to scan a range of ip's and then store those ip's individually in mysql table.
how to scan when we need to scan a range of ip, e.g. 172.27.20.130-140. and store all ips in table of mysql. now a range is needed to be searched and each ip should be stored in table.
#NMAP SCANNING CODE
nmScan=nmap.PortScanner()
host=self._firstname.value # taking input from a pyforms gui input field
result=nmScan.scan(hosts=host, arguments='-sV -v -p 1-1024')
print('Host : %s (%s)' % (host, nmScan[host].hostname()))
print('State : %s' % nmScan[host].state())
for proto in nmScan[host].all_protocols():
print('----------')
print('Protocol : %s' % proto)
lport = nmScan[host][proto].keys()
#lport.sort()
for port in lport:
thisDict = nmScan[host][proto][port]
print ('port : %s\tstate : %s\tVersion:%s,v%s'% (port, nmScan[host][proto][port]['state'],thisDict['product'] ,thisDict['version']))
#INSERT into INPUT table CODE
try:
connection = mysql.connector.connect(host='localhost',
database='testdb',
user='root',
password='Pooja#123')
cursor = connection.cursor()
sql_insert_query = """ INSERT INTO input (sno,ip) VALUES (%s,%s)"""
so=cursor.lastrowid
sno=so
ip=self._firstname.value # taking input from a pyforms gui input field
insert_tuple = (sno,ip)
result = cursor.execute(sql_insert_query, insert_tuple)
print("inserted")
connection.commit()
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
print("Failed inserting record into input table {}".format(error))
finally:
#closing database connection.
if(connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
The ipaddress module is useful for this type of thing:
In [1]: import ipaddress
In [2]: addr = ipaddress.IPv4Address('172.27.20.130')
In [3]: print(addr)
172.27.20.130
In [4]: print(addr + 1)
172.27.20.131

Not inserting values in MySQL Table

We have two IR sensors interfaced with our RPi3, and we want to store the sensor data into the database.
This is the code we are trying to run.
db = MySQLdb.connect("localhost","Keerti","Keerti","test")
curs = db.cursor()
try:
print "module test"
time.sleep(2)
print "ready"
while True:
if GPIO.input(4):
print "motion detected 1"
curs.execute("INSERT INTO parktest(irname,irdata) VALUES('%f', '%f')" % (1,1))
time.sleep(1)
else:
print "no motion 1"
curs.execute("INSERT INTO parktest(irname,irdata) VALUES('%f', '%f')" % (1,0))
time.sleep(1)
if GPIO.input(14):
print "motion detected 2"
curs.execute("INSERT INTO parktest(irname, irdata) VALUES('%f','%f')" % (2,1))
time.sleep(1)
else:
print "no motion 2"
curs.execute("INSERT INTO parktest(irname, irdata) VALUES('%f','%f')" % (2,0))
time.sleep(1)
except KeyboardInterrupt:
print "quit"
GPIO.cleanup()
This, runs fine without any warnings or errors. However, it isn't storing anything in the table.
What might be the issue?
After execute(), you must use commit() to confirm all the pending changes.
Or you can use rollback() to cancel all the pendings.
Otherwise, nothing will be changed in database.

Loop to read data from a serial to a database

I want to make a loop to be storing data from serial database. The code I have goes through the first loop but fails to execute in the next loops. Here is the code:
import serial
import MySQLdb
import time
dbConn = MySQLdb.connect("localhost","root","root","testing") or die ("could not connect to database")
cursor = dbConn.cursor()
device = '/dev/ttyACM0'
max=30
start=time.time()
while True:
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
except:
print "Failed to connect on",device
try:
data = arduino.readline()
pieces = data.split("\t")
try:
cursor.execute("INSERT INTO productCount (line,count) VALUES (%s,%s)", (pieces[0],pieces[1]))
dbConn.commit()
cursor.close()
except MySQLdb.IntegrityError:
print "failed to insert data"
#finally:
#cursor.close()
except:
print "Failed to get data from Arduino!"
time.sleep(10)
print "looping..."
remaining=max+start-time.time()
print "%s seconds remaining" % int(remaining)
if remaining<=0:
cursor.close()
break
You closed the cursor inside while loop. in try and finally.Put this in break condition of while loop
cursor.close()
Put it outside, or put this inside while loop
cursor = dbConn.cursor()

Executing stored procedure in python

While calling the stored procedure of mysql using python, I am getting a syntax error.
The code for the stored procedure is as follows,
while True:
try:
date = time.strftime("%d/%m/%Y")
temp,humidity,light = main.reading()
args= (192.168.1.145, b8:27:eb:06:e4:4b, Temp_PI, temp)
cursor.callproc('SPR_IU_Sensor_Data',args)
conn.commit()
time.sleep(interval2)
except:
MySQLdb.Error, e:
conn.rollback()
print "Transaction aborted: %d: %s" % (e.args[0], e.args[1])
The error is as follows;
File "procedure.py", line 53
args= (192.168.1.145, b8:27:eb:06:e4:4b, Temp_PI, temp)
^
SyntaxError: invalid syntax
You need to quote the ip addresses, pass them in as strings:
args = ('192.168.1.145', 'b8:27:eb:06:e4:4b', Temp_PI, temp)
Python has no notion of an IP address literal notation.

MYSQL - server quits midway through a script

I am using mysqldb/python to push some data into a mysql db.
The script parses a bunch of XML files for the data.
The MySQL server seems to quit and give me a '#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)' error midway through the transactions - in a different place every time I run it (so I am assuming its not a specific piece of data that is making it fall over...)
It works perfectly until it reaches ~12 or 13 file and it gives me this error:
Error 2003: Can't connect to MySQL server on 'localhost' (10055)
Traceback (most recent call last):
File "sigFileParser.py", line 113, in <module>
doParser(sigfile_filename)
File "sigFileParser.py", line 106, in
doParser
doFormatsPush(packedFormats)
File "sigFileParser.py", line 27, in
doFormatsPush
sys.exit (1)
NameError: global name 'sys' is not defined
Once the error occurs I can not get into MySQL console or via PHOPmyadmin
If I leave if for a while, I can get back into MySQL
MySQL tables:
CREATE TABLE IF NOT EXISTS patterns
(Version int(3),
DateCreated DATETIME,
SigID int(4),
SigSpecificity CHAR(10),
ByteSeqReference CHAR(12),
MinFragLength int(4),
Position int(4),
SubSeqMaxOffset int(4),
SubSeqMinOffset int(4),
Pattern TEXT)
and
CREATE TABLE IF NOT EXISTS formats
(Version int(3),
DateCreated DATETIME,
FormatID int(4),
FormatName TEXT,
PUID TEXT,
FormatVersion TEXT,
FormatMIMEType TEXT,
InternalSignatureID int(4),
Extension TEXT,
HasPriorityOverFileFormatID int(4))
Py code
from lxml import etree
import re, os, MySQLdb
def doPatternPush(packedPatterns):
try:
db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles")
c = db.cursor()
c.execute('''INSERT INTO sigfiles.patterns
(Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedPatterns)
db.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
return (db)
def doFormatsPush(packedFormats):
try:
db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles")
c = db.cursor()
c.execute('''INSERT INTO sigfiles.formats
(Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedFormats)
db.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
return(db)
def doParser(sigfile_filename):
tree = etree.parse(sigfile_filename)
root = tree.getroot()
attributes = root.attrib
if 'DateCreated' in root.attrib:
DateCreated = (attributes["DateCreated"])
if 'Version' in root.attrib:
Version = (attributes["Version"])
##--------- get internal sig details ------------------
for a in range (len(root[0])): #loops for sig ID
attributes = root[0][a].attrib
SigID=(attributes["ID"])
SigSpecificity = (attributes["Specificity"])
for b in range (len(root[0][a])): # loops for sequence pattern inside each sig
attributes = root[0][a][b].attrib
if 'Reference' in root[0][a][b].attrib:
ByteSeqReference = (attributes["Reference"])
else:
ByteSeqReference = "NULL"
attributes = root[0][a][b][0].attrib
if 'MinFragLength' in root[0][a][b][0].attrib:
MinFragLength=(attributes["MinFragLength"])
else:
MinFragLength=''
if 'Position' in root[0][a].attrib:
Position=(attributes["Position"])
else:
Position=''
if 'SubSeqMaxOffset' in root[0][a][b][0].attrib:
SubSeqMaxOffset=(attributes["SubSeqMaxOffset"])
else:
SubSeqMaxOffsee = ''
if 'SubSeqMinOffset' in root[0][a][b][0].attrib:
SubSeqMinOffset=(attributes["SubSeqMinOffset"])
else:
SubSeqMinOffset = ''
Pattern = root[0][a][b][0][0].text
packedPatterns = [Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern]
doPatternPush(packedPatterns)
##-------- get format ID details-------------
for a in range (len(root[1])):
attributes = root[1][a].attrib
if 'ID' in root[1][a].attrib:
FormatID = (attributes['ID'])
else:
FormatID = "NULL"
if 'Name' in root[1][a].attrib:
FormatName = (attributes['Name'])
else:
FormatName = "NULL"
if 'PUID' in root[1][a].attrib:
PUID = (attributes['PUID'])
else:
PUID = "NULL"
if 'Version' in root[1][a].attrib:
FormatVersion = (attributes['Version'])
else:
FormatVersion = "NULL"
if 'MIMEType' in root[1][a].attrib:
FormatMIMEType = (attributes['MIMEType'])
else:
FormatMIMEType = "NULL"
InternalSignatureID,Extension,HasPriorityOverFileFormatID = ('', 'NULL', '')
for b in range (len(root[1][a])): #extracts the tags for each format ID
tagType = root[1][a][b].tag
tagText = root[1][a][b].text
tagType = re.sub('{http://www.nationalarchives.gov.uk/pronom/SignatureFile}', '', tagType)
if tagType == 'InternalSignatureID':
InternalSignatureID = tagText
elif tagType == 'Extension':
Extension = tagText
HasPriorityOverFileFormatID = ''
else:
HasPriorityOverFileFormatID = tagText
Extension = 'NULL'
packedFormats = [Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID]
doFormatsPush(packedFormats)
if __name__ == "__main__":
path = "C:\Users\NDHA\Desktop\droid sigs all"
for (path, dirs, files) in os.walk(path):
for file in files:
sigfile_filename = str(path)+"\\"+str(file)
doParser(sigfile_filename)
print sigfile_filename
db.close()
All the XML comes from here: http://www.nationalarchives.gov.uk/aboutapps/pronom/droid-signature-files.htm
The error you get tells you exactly what's wrong
NameError: global name 'sys' is not defined
You don't import sys in your python file.
As for the db connection, if your socket is not placed in /tmp/mysql.sock, you can specify where to look for it when you try to connect to the db using the unix_socket parameter.
Try:
db = MySQLdb.connect (unix_socket = 'path_to_sock', host = "localhost",
user = "root", passwd = "", db = "sigfiles")
Where you replace 'path_to_sock' with the actual path to the mysql sock.
Other stuff you should check in case that isn't the issue:
Check to make sure the username/password combination is correct
Try stopping and re-starting the mysqld service
Check the error log files for more specific errors
This is your first error:
Error 2003: Can't connect to MySQL server on 'localhost' (10055)
It seems you disconnect from MySQL at some point. Check your code and see if you're explicitly or implicitly getting disconnected from the server and also check if your MySQL server is still listening to connections... maybe you're killing the server from outside your app... who knows? :)

Categories

Resources