syntax error in mariaDB that i cannot find - python

i am writing a code to write my gps variables in my database but i get this error everytime i run it:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Long, Status) VALUES (1, 50.821139333333335, 3.2815086666666664, 'Eind')' at line 1
the database has an id that is auto incremented and then the following values i'm trying to add, no clue what's going wrong. maybe you guys have an idea
database.py:
#staticmethod
def execute_sql(sqlQuery, params=None):
result = None
db, cursor = Database.__open_connection()
try:
cursor.execute(sqlQuery, params)
db.commit()
result = cursor.lastrowid
except connector.Error as error:
db.rollback()
result = None
print(f"Error: Data not stored.{error.msg}")
finally:
cursor.close()
db.close()
return result
datarepository.py:
#staticmethod
def create_gps(RouteID, Lat, Long, Status):
sql = "INSERT INTO gps (RouteID, Lat, Long, Status) VALUES (%s, %s, %s, %s)"
params = [RouteID, Lat, Long, Status]
return Database.execute_sql(sql, params)
app.py:
#socketio.on('F2B_GPS')
def gps_aan(toggle, status):
while(toggle != 1):
port = "/dev/serial0"
ser = serial.Serial(port, baudrate=9600, timeout=0.5)
dataout = pynmea2.NMEAStreamReader()
newdata = ser.readline()
if sys.version_info[0] == 3:
newdata = newdata.decode("utf-8", "ignore")
if newdata[0:6] == "$GPRMC":
newmsg = pynmea2.parse(newdata)
lat = newmsg.latitude
lng = newmsg.longitude
gps = "Latitude=" + str(lat) + "and Longitude=" + str(lng)
print(gps)
DataRepository.create_gps(1,lat,lng, status)
print("added")
toggle = 1
Thanks to anyone replying :)

ok i found it you cant use Long since that screws it up in mysql so i had to change it to longitude

Related

Troubles using async library : aiomysql (python - bot)

I'm trying to transform my standard database functions into aiomysql async functions (for a bot) but I don't really understand how does the async functions work...
Here's my actual code that I want to transform :
def connection(Database):
conn = mysql.connector.connect(host=Database[0],
user=Database[1],
password=Database[2],
database=Database[3])
c = conn.cursor()
return c, conn
def insertToTable(Database, insert, data):
c, conn = connection(Database)
try:
pass
c.execute(insert, data)
conn.commit()
except mysql.connector.IntegrityError as e:
#cnx.close()
def deleteFromTable(Database, query):
c, conn = connection(Database)
try:
c.execute(query)
c.commit()
except:
pass
def getMax(Database, columnName, tableName):
c, conn = connection(Database)
c.execute("SELECT MAX(" + columnName + ") FROM " + tableName)
result = c.fetchall()
for i in result:
if i[0] is None:
return 0
else:
return i[0]
My projects is separed in multiples files, I got some others basics requests that I didn't transform in function :
c, conn = dbconnect.connection(DB)
c.execute("update Tar SET va= (%s) WHERE x=1",(va_num))
conn.commit()
and some select fetchall/fetchone etc
I wrote that after reading the documentations and finding some (rare) examples :
import asyncio
import aiomysql
import setup as setup
loop = asyncio.get_event_loop()
#asyncio.coroutine
def exec_sql(insert, data):
pool = yield from aiomysql.create_pool(host=setup.DB_local[0], port=3306,
user=setup.DB_local[1], password=setup.DB_local[2],
db=setup.DB_local[3], loop=loop, autocommit=False)
with (yield from pool) as conn:
cur = yield from conn.cursor()
yield from cur.execute(insert, data)
yield from conn.commit()
conn.close
#pool.close()
#yield from pool.wait_closed()
insert = ("INSERT into discord_rights (discord_id, discord_name, discord_role, is_admin, is_caster, is_player)""VALUES (%s, %s, %s, %s, %s, %s)")
data = (10, "lea", 0, 0, 1, 1)
sql = loop.run_until_complete(exec_sql(insert, data))
#asyncio.coroutine
def get_one_sql(sql):
pool = yield from aiomysql.create_pool(host=setup.DB_local[0], port=3306,
user=setup.DB_local[1], password=setup.DB_local[2],
db=setup.DB_local[3], loop=loop, autocommit=False)
with (yield from pool) as conn:
cur = yield from conn.cursor()
yield from cur.execute(sql)
r = yield from cur.fetchone()
conn.close
return r
#pool.close()
#yield from pool.wait_closed()
sql = loop.run_until_complete(get_one_sql("SELECT * from discord_rights WHERE discord_id = 124545xxxxxxxx"))
print(sql)
But I'm not sure if this is a good way cause I create a new pool for every request, right ?
Can someone help me to build on of the function (importing the pool created in an another part of the code) or something better if I'm still wrong ?
Thx for your help and sorry for the long message, I prefered to show you my codes instead of nothing !

How to add exception to python function?

Good day. I wrote an application on python that collects call logs from the Avaya PBX and writes them to the mysql database. It works well, but sometimes the PBX sends an empty string for some reason and the program fails. I attach the screen and code below. I understand that you need to wrap the function in an exception: try except, but I don’t understand how to do it. Please tell me how to do this.enter image description here
def write_db(item, *agrs):
connection = pymysql.connect(host='localhost',
user='acdr',
password='it8ejokd',
db='avaya_cdr',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
DBTBL = "cdr102019"
DBFLD = "Date_call, Time_call, `Sec_dur`, `clg_num_in_tag`, `dialed_num`, dep_called, dep_dialed"
dep_num_call = find_dep(item[3].replace(' ', ''))
name_dep_call = name_dep(dep_num_call)
dep_num_dial = find_dep(item[4].replace(' ', ''))
name_dep_dial = name_dep(dep_num_dial)
item.append(name_dep_call)
item.append(name_dep_dial)
item[1] = item[1] + "00"
try:
with connection.cursor() as cursor:
sql = "INSERT INTO "+DBTBL+" ("+DBFLD+") VALUES (%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (item))
connection.commit()
finally:
connection.close()
# Задаем адрес сервера
SERVER_ADDRESS = ('', 5100)
# Настраиваем сокет
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(SERVER_ADDRESS)
server_socket.listen(5)
print('server is running, please, press ctrl+c to stop')
# Слушаем запросы и пишем в db
while True:
connection, address = server_socket.accept()
data = connection.recv(1024)
if not(b'\x00\x00\x00' in data) and not(b'1370' in data):
str = data.decode("utf-8")
item=[str[0:6],str[7:11],str[12:17],str[18:33],str[34:57]]
print(item)
write_db(item)
connection.close()
You'll have to catch the exception, so we can cater for a few types, but just to be sure and get you up and running, you could do the following :)
try:
with connection.cursor() as cursor:
sql = (
"INSERT INTO "+DBTBL+" ("+DBFLD+") VALUES "
"(%s,%s,%s,%s,%s,%s,%s)"
)
cursor.execute(
sql,
(item),
)
connection.commit()
except Exception as e:
print("Error occurred: %s"% e)
finally:
connection.close()
This should do the trick. I've used all four elements of try/except/else/finally here, with brief explanations of when they're executed.
try:
with connection.cursor() as cursor:
sql = "INSERT INTO "+DBTBL+" ("+DBFLD+") VALUES (%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (item))
except Exception: # If this code fails, ignore it
pass
else: # If the code inside 'try' succeeds, execute this code.
connection.commit()
finally: # Regardless of whether or not the code inside 'try' succeeds, execute this code
connection.close()

PyMySQL not Inserting Data

I have a piece of code which is taking Windows logs and inserting various pieces of information into an mySQL database. The code is running perfectly with no errors, but does not actually input the data into the table. The table remains blank. I pulled my mySQL syntax from an example with some modification, so I'm not entirely sure what is going wrong. I have a feeling it has either to do with the data types, or some changes I made to the syntax.
import sys
import pymysql
import pymysql.cursors
import win32evtlog # requires pywin32 pre-installed
import win32evtlogutil
import time
server = 'localhost' # name of the target computer to get event logs
logtype = 'System' # 'Application' # 'Security'
hand = win32evtlog.OpenEventLog(server,logtype)
flags =
win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
events = win32evtlog.ReadEventLog(hand, flags,0)
while True:
for event in events:
evt_tp = event.EventType
if evt_tp != (1 or 2 or 8):
eve_cat = str(('Event Category:', event.EventCategory))
eve_timegen = str(('Time Generated:', event.TimeGenerated))
eve_srcnm = str(('Source Name:', event.SourceName))
eve_id = str(('Event ID:', event.EventID))
eve_typ = str(('Event Type:', event.EventType))
data = event.StringInserts
if data:
print ('Event Data:')
for msg in data:
print(msg)
print(type(eve_cat))
print(type(eve_timegen))
print(type(eve_srcnm))
print(type(eve_id))
print(type(eve_typ))
print(type(data))
time.sleep(10)
else:
eve_cat = ('Event Category:', event.EventCategory)
eve_timegen = ('Time Generated:', event.TimeGenerated)
eve_srcnm = ('Source Name:', event.SourceName)
eve_id = ('Event ID:', event.EventID)
eve_typ = ('Event Type:', event.EventType)
data = event.StringInserts
print('There were no errors found')
print(eve_cat)
print(eve_timegen)
print(eve_srcnm)
print(eve_id)
print(eve_typ)
print(data)
time.sleep(10)
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='ptest',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName',
'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `Type` FROM `win_logs` WHERE `Category`=%s"
cursor.execute(sql, ('webmaster#python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
I can be very wrong.
But this is python.
Indentation matter.
Try just:
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName`, 'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))
I guess your cursor is out of with scope

Python/MySQLdb troubles on Raspberry Pi

I'm running into some trouble running python/mysqldb on my raspberry pi. This is a pretty simple script, so I'm not sure what I'm missing. The "SELECT * FROM..." runs with no problem, but I can't seem to update the table with new values. The script runs without throwing errors, but when I ctrl-C, it gives me this:
Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in bound method DictCursor.__del of MySQLdb.cursors.DictCursor object at 0x19dfd90
Here's my script:
dhost = "localhost"
duser = "root"
dname = "rpi"
dpass = "datPassword"
import MySQLdb
try:
con = MySQLdb.connect(dhost,duser,dpass,dname);
cur = con.cursor(MySQLdb.cursors.DictCursor)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
def websiteToSensor():
cur.execute("SELECT * FROM homeauto WHERE changedby = 'website'")
rows = cur.fetchall()
for row in rows:
cur.execute("UPDATE homeauto SET changedby = 'script' WHERE id = '%s'",(row["id"]))
return
while True:
websiteToSensor()
Does anyone have an idea as to why my table isn't updating? Thanks!
***EDIT: SOLUTION***
Thanks to Martijn Pieters, here's my new websiteToSensor() code:
def websiteToSensor():
cur = con.cursor(MySQLdb.cursors.DictCursor)
cur.execute("SELECT * FROM homeauto WHERE changedby = 'website'")
rows = cur.fetchall()
num = int(cur.rowcount)
if num > 0:
for row in rows:
cur.execute("UPDATE homeauto SET changedby = 'script' WHERE id = '%s'",(row["id"]))
con.commit()
cur.close()
con.commit()
else:
cur.close()
con.commit()
return
Try committing your changes:
def websiteToSensor():
cur.execute("SELECT * FROM homeauto WHERE changedby = 'website'")
rows = cur.fetchall()
for row in rows:
cur.execute("UPDATE homeauto SET changedby = 'script' WHERE id = '%s'",(row["id"]))
con.commit()
return

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