For-loop exiting prematurely - python

I'm having an issue where a python for loop is exiting prematurely. The loop iterates over SQL database entries by location.
If location is found, it will grab the IP address of those entries and send a configuration. If the job fails, or throws an exception in my case, within the exception it will go to a function to update the status, date/time. The expected result currently is an exception.
When it comes back to the original function, it exits the loop and doesn't go to the next DB entry (which I know is there.) If I remove the function from within the exception then everything works fine.
Current results. It shows the loop working from function to function. I should see twice, or 6 lines:
Return to Function,
Back to function,
Finished,
Return to Function,
Back to function,
Finished,
Here's what I'm getting
1. IP
2. Status
3. Location
4. Main
Selection: 3
Location: Test
Return to Function,
Back to function,
Finished,
Code shown below:
def db_entry(Device, status):
int = 0
while int < 1:
c.execute('SELECT * FROM Automation WHERE Device=?', (Device,))
if bool(c.fetchone()) == True:
c.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?"(time.strftime("%H:%M:%S +0000"), date.today(), status, Device))
mydb.commit()
int = int + 1
else:
c.execute("INSERT INTO Automation VALUES ('%s','NETCONF','POC', '%s', '%s', '%s')" % (Device, status, date.today(), time.strftime("%H:%M:%S +0000")))
mydb.commit()
int = int + 1
else:
print("Return to Function")
def send_single_configuration(file):
status_1 = "Success"
status_2 = "Fail"
print("\n")
view_database()
print("\n")
retries = 0
print("1. IP")
print("2. Status")
print("3. Location")
print("4. Main")
print("\n")
input_selection = input("Selection: ")
print("\n")
if input_selection == "3":
location = input("Location: ")
for row in c.execute('SELECT * FROM Automation WHERE Location=?', (location, )):
try:
m = manager.connect(row[0], 830, "cisco", "cisco", {'name': 'csr'})
config_file = open(file=file).read()
m.edit_config(config_file, target="running")
db_entry(row[0], status_1)
print("\n")
print("Configuration Complete!")
print("\n")
except AttributeError:
db_entry(row[0], status_2)
print("Back to function")
else:
print("Finished")
Traceback (most recent call last):
File "C:/Users/chris.oberdalhoff/PycharmProjects/Automation/venv/SQL.py", line 412, in <module>
main()
File "C:/Users/chris.oberdalhoff/PycharmProjects/Automation/venv/SQL.py", line 82, in main
snmp_configuration()
File "C:/Users/chris.oberdalhoff/PycharmProjects/Automation/venv/SQL.py", line 326, in snmp_configuration
send_single_configuration(SNMP_file)
File "C:/Users/chris.oberdalhoff/PycharmProjects/Automation/venv/SQL.py", line 395, in send_single_configuration
m.edit_config(config_file, target="running")
AttributeError: 'NoneType' object has no attribute 'edit_config'
More Testing:
If i remove everything but a print statement from the db_entry function which gave the expected result.
I'm thinking something withing this line of code or something with the module/progam interaction.
c.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE
Device=?",(time.strftime("%H:%M:%S +0000"), date.today(), status,
Device))

It was the sqlite3 module. I found an old answer from 8 years ago here, python sqlite3 for loop update.
sqllitle3 module doesn't allow for multi-threading.
I created another connection object and used it to update the DB entry
c = mydb.cursor() # Find the location
d = mydb.cursor() # Update the entry
for row in c.execute('SELECT * FROM Automation WHERE Location=?', (location, )):
try:
device_connect(row[0])
for capability in m.server_capabilities:
continue
d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_1, row[0],))
mydb.commit()
except AttributeError:
d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_2, row[0],))

Related

Python3: cassandra.cluster.NoHostAvailable: ("Unable to connect to any servers using keyspace 'test'", ['127.0.0.1']) when using execute_async future

I am trying to fetch data from Cassandra from a specific table and trying to insert it into another table in Cassandra after making some changes. Both the tables are located in keyspace "test". When I am trying to get the data from the first table everything works fine and it is able to fetch the data. However, in the future handler which handles the output of the first query, I am trying to insert the data into another table under the same Cassandra instance and it is gettingting failed. I am getting an error from the application stating "cassandra.cluster.NoHostAvailable: ("Unable to connect to any servers using keyspace 'test'", ['127.0.0.1'])" . I am not sure where I am going wrong
import threading
from threading import Event
from cassandra.query import SimpleStatement
from cassandra.cluster import Cluster
hosts=['127.0.0.1']
keyspace="test"
thread_local = threading.local()
cluster_ = Cluster(hosts)
def get_session():
if hasattr(thread_local, "cassandra_session"):
print("got session from threadlocal")
return thread_local.cassandra_session
print(" Connecting to Cassandra Host " + str(hosts))
session_ = cluster_.connect(keyspace)
print(" Connecting and creating session to Cassandra KeySpace " + keyspace)
thread_local.cassandra_session = session_
return session_
class PagedResultHandler(object):
def __init__(self, future):
self.error = None
self.finished_event = Event()
self.future = future
self.future.add_callbacks(
callback=self.handle_page,
errback=self.handle_error)
def handle_page(self, rows):
for row in rows:
process_row(row)
if self.future.has_more_pages:
self.future.start_fetching_next_page()
else:
self.finished_event.set()
def handle_error(self, exc):
self.error = exc
self.finished_event.set()
def process_row(row):
print(row)
session_ = get_session()
stmt = session_.prepare(
"INSERT INTO test.data(customer,snr,rttt, event_time) VALUES (?,?,?,?)")
results = session_.execute(stmt,
[row.customer, row.snr, row.rttt,row.created_time])
print("Done")
session = get_session()
query = "select * from test.data_log"
statement = SimpleStatement(query, fetch_size=1000)
future = session.execute_async(statement)
handler = PagedResultHandler(future)
handler.finished_event.wait()
if handler.error:
raise handler.error
cluster_.shutdown()
However, when I try to execute the python file the application is throwing an error "cassandra.cluster.NoHostAvailable: ("Unable to connect to any servers using keyspace 'test'", ['127.0.0.1'])" from getSession() call from "process_row" method. Clearly, the first call to Cassandra is getting succeeded without any issues. There is no connectivity issue and the Cassandra instance is running fine locally. I am able to query the data using cqlsh. If I call the process_row method outside the future handler everything is working fine, I am not sure what needs to be done to make it happen from the Future Handler.
Connecting to Cassandra Host ['127.0.0.1']
Connecting and creating session to Cassandra KeySpace test
Row(customer='abcd', snr=100, rttt=121, created_time=datetime.datetime(2020, 8, 8, 2, 26, 51))
Connecting to Cassandra Host ['127.0.0.1']
Traceback (most recent call last):
File "test/check.py", , in <module>
raise handler.error
File "cassandra/cluster.py", line 4579, in cassandra.cluster.ResponseFuture._set_result
File "cassandra/cluster.py", line 4777, in cassandra.cluster.ResponseFuture._set_final_result
File "test/check.py"", in handle_page
process_row(row)
File "test/check.py"", in process_row
session_ = get_session()
File "/test/check.py"", in get_session
session_ = cluster_.connect(keyspace)
File "cassandra/cluster.py", line 1715, in cassandra.cluster.Cluster.connect
File "cassandra/cluster.py", line 1772, in cassandra.cluster.Cluster._new_session
File "cassandra/cluster.py", line 2553, in cassandra.cluster.Session.__init__
cassandra.cluster.NoHostAvailable: ("Unable to connect to any servers using keyspace 'test'", ['127.0.0.1'])
Process finished with exit code 1
Ok so Cassandra recommends the following:
Use at most one Session per keyspace, or use a single Session and explicitely specify the keyspace in your queries
https://www.datastax.com/blog/4-simple-rules-when-using-datastax-drivers-cassandra
In your code you try to create a session every time the read query has retrieved some rows.
To force the code to use at most one session we can create a queue where the child thread sends the row to the main thread and the main thread handles it further by executing the insert query. We do this in the main thread because I've experienced issues by executing queries in child thread.
callback_queue = Queue()
session = cluster_.connect(keyspace)
session.row_factory = dict_factory # because queue doesn't accept a Row instance
class PagedResultHandler(object):
...
def handle_page(self, rows):
for row in rows:
callback_queue.put(row) # here we pass the row as a dict to the queue
...
def process_rows():
while True:
try:
row = callback_queue.get() # here we retrieve the row as a dict from the child thread
stmt = session.prepare(
"INSERT INTO test.data(customer,snr,rttt, event_time) VALUES (?,?,?,?,?)")
results = session.execute(stmt,
[row['customer'], row['snr'], row['rttt'], row['created_time']])
print("Done")
except Empty:
pass
query = "select * from test.data_log"
statement = SimpleStatement(query, fetch_size=1000)
future = session.execute_async(statement)
handler = PagedResultHandler(future)
process_rows() # for now the code will hang here because we have an infinite loop in this function
handler.finished_event.wait()
if handler.error:
raise handler.error
cluster_.shutdown()
This will get it to work, but I would replace the while True else you will get into an infinite loop.
Ok so in that case we do 2 things, we can use multithreading and batch inserting. I think if we batch insert parallelism is not required, because that will speed things up from the client side fast enough. multithreading wouldn't add much more speed to it as it is not a cpu intensive task.
session = cluster_.connect(keyspace)
session.row_factory = dict_factory
class Fetcher:
def __init__(self, session):
self.session = session
query = "select * from test.data_log"
self.statement = SimpleStatement(query, fetch_size=1000)
def run(self):
rows = self.session.execute(self.statement)
temp_rows = []
total = 0
for row in rows:
temp_rows.append(row)
if len(temp_rows) == 1000:
handler = PagedResultHandler(self.session, temp_rows)
handler.start()
temp_rows = []
handler = PagedResultHandler(self.session, temp_rows)
handler.start()
def handle_error(self, err=None):
print(err)
class PagedResultHandler(threading.Thread):
def __init__(self, session, rows):
super().__init__()
self.session = session
self.error = None
self.rows = rows
self.finished_event = Event()
def run(self):
batch = BatchStatement(consistency_level=ConsistencyLevel.QUORUM)
stmt = session.prepare("INSERT INTO test.data(id, customer,snr,rttt, event_time) VALUES (?,?,?,?,?)")
for row in self.rows:
batch.add(stmt, [1, row['customer'], row['snr'], row['rttt'], row['created_time']])
results = session.execute(batch)
print(results)
Fetcher(session).run()
This does script does both batch inserting and multithreading, but again multithreading seems unnecessary.

Python 3.7.0 c.execute(v_sql,[Part_no, Rev_no]) does not recognize my variables

I have been trying to retrieve data from an Oracle DB using Oracle_CX library. However, when I hard code c.execute(v_sql,['02502-9001-12','SS']) it will return me the right data. However, when I have to pass the variable to the function c.execute(v_sql,[Part_No,Rev_no]) does not recognize the values the output is (none)
What I tried to do is passing the values to Part_No and Rev_no
only when I hard code the data I can receive the right data. I even print before c.execute() is happening to see if the Part_No and Rev_no is printed and it happened. The actual error is that I am not receiving the right data (none) I suppose to receive AAAW07AAFAAF5/PAAq. Looks like the values of def why_(Part_no, Rev_no) do not get populated it to c.execute(v_sql,[Part_No,Rev_no])
c.execute(v_sql,[Part_No,Rev_no])
c.execute(v_sql,['02502-9001-12','SS'])
import cx_Oracle
import os
def why__(Part_No, Rev_no):
dsn_tns = cx_Oracle.makedsn('XXXXX', 'XXXX', service_name='XXXXX')
conn = cx_Oracle.connect(user=r'XXXX', password='XXXX', dsn=dsn_tns)
c = conn.cursor()
#u_sql =('SELECT PART_REV_CFP.GET_OBJKEY(:Part_No,:Rev_no) from dual')
v_sql =('SELECT PART_REV_CFP.GET_OBJKEY(:Part_No,:Rev_no) from dual')
print(Part_No)
print(Rev_no)
c.execute(v_sql,[Part_No,Rev_no])
#c.execute(v_sql,['02502-9001-12','SS'])
R = c.fetchone()
print('before the loop')
i=0
for row in R:
print('inside the loop')
i+=1
print(i)
print(str(row))
print('after the loop')
conn.close()
return row
Part_No = input("Enter the obs part num: ")
Rev_no = input("Enter the obs part rev: ")
why__(Part_No,Rev_no)
print('outside of the method')
print(why__(Part_No,Rev_no))
OutPut when c.execute(v_sql,[Part_No,Rev_no]) --IN other words no hard coding the values of Part_no and Rev_no
Enter the obsolete part number: 02502-9001-12
Enter the obsolete part revision: ss
before the loop
inside the loop
1
(None,)
after the loop
***Repl Closed***
Output when c.execute(v_sql,['02502-9001-12','SS'])
Enter the obsolete part number: 02502-9001-12
Enter the obsolete part revision: ss
02502-9001-12
ss
before the loop
inside the loop
1
AAAW07AAFAAF5/PAAq
after the loop
outside of the method
02502-9001-12
ss
before the loop
inside the loop
1
AAAW07AAFAAF5/PAAq
after the loop
AAAW07AAFAAF5/PAAq
***Repl Closed***

Operands missing in MySQL statement

I'm getting an arguments error from the MySQL module looking for 4 operands. I just don't see which operands it's looking for. It works for some case types and not others. Line numbers of error (trimmed at bottom which refers to MySQL library):
Traceback (most recent call last):
File "/Users/christoph/PycharmProjects/physicianWorkQueueProject/physicianWorkQueueProject.py", line 158, in <module>
parse()
File "/Users/christoph/PycharmProjects/physicianWorkQueueProject/physicianWorkQueueProject.py", line 138, in parse
c.execute(getPhysiciansql_cmd)
...
mysql.connector.errors.DataError: 1241 (21000): Operand should contain 4 column(s)
#!/usr/bin/python
##################################################
# This is a prototype pathologist work queue management system
# The program calls out to a database of pathologists, their specialities,
# and an index of case types and their intended specialists
#
# Usage: Run at your command line. You will then enter case numbers (which aren't validated (currently))
# and case types (which are validated). The program will distribute the entered case as follows:
#
# If the case is intended for generalists, a system that amounts to names being pulled from a hat
# is employed. When a name is selected (at random), the case is entered into that pathologists' work queue.
# That pathologists' name is not returned to the pool.
# The cycle will then repeat with a random name picked every time, in this same way, for generalist-requiring cases,
# until the entirety of names have been pulled. At this point, all names are returned to the pool and the whole cycle
# begins again
#
# For the specialist requiring cases, the name-draw system is bypassed and the case is directly entered
# into the pathologists' work queue.
# 16 Aug 2018
# My Real Name
##################################################
import mysql.connector as mariaDB
import time
import pandas as pd
from random import choice
def distributefairly(inputCaseNumber, inputcasetype):
# function distrbutefairly does a draw out of a hat, with each name being pulled and the pot shrinking until none are
# left at which point all names are added back
c.execute("SELECT physicianName FROM physicianNamesSpecialties;")
originalPhysicianList = c.fetchall()
physicianList = originalPhysicianList
# print("counter at:",count)
chosenPhysician = choice(physicianList)
pos = physicianList.index(chosenPhysician)
physicianList.pop(pos)
cleanedUpChosenPhysician = chosenPhysician[0]
insert(cleanedUpChosenPhysician)
print("This case is going to", cleanedUpChosenPhysician + ".")
select()
increment()
if len(physicianList) == 0:
reset()
def reset():
# this resets the counter the distributefairly module calls
global count
global physicianList
count = 0
c.execute("SELECT physicianName FROM physicianNamesSpecialties;")
physicianList = c.fetchall()
def increment():
# increments the counter of the distributefairly module
global count
count +=
def print_count():
print(count)
def insert(cleanedUpPhysResult):
# adds inputted cases into the workQueue table
global inputCaseNumber
global inputCaseType
ts = time.gmtime()
readableTs = time.strftime("%Y-%m-%d %H:%M:%S", ts)
c2.execute("INSERT INTO workQueue (name, caseNumber, timestamp, tableCaseType) values (%s,%s,%s,%s)", (cleanedUpPhysResult, inputCaseNumber, readableTs, str(inputCaseType)))
conn2.commit()
def select():
# this outputs the workQueue after every addition
sql = "SELECT * FROM workQueue"
c2.execute(sql)
rWQ = c2.fetchall()
print(pd.DataFrame(rWQ, columns= ['Name','Case Number','Time Stamp','Specialty','Row ID'])) # .set_index('Row ID')
def startup():
# create()
global inputCaseType
global inputCaseNumber
inputCaseNumber = input("Enter Case Number: ")
inputCaseType = input("Enter case type (use proper abbreviations): ")
def parse():
global inputCaseNumber
global inputCaseType
caseInputsql_cmd = "SELECT specialtyRequiredToProcess,Description FROM caseTypes WHERE caseTypeName='{}'".format(inputCaseType)
c.execute(caseInputsql_cmd)
rows_returned = c.fetchall()
if not rows_returned:
print("No match to table of specimen types returned. Check the case type abbreviation and try again.")
return
else:
for row in rows_returned:
r = row[0]
d = row[1]
print("This is a", r, "service case. It is a", d,"type case.")
if r != "GENERALIST":
getPhysiciansql_cmd = "SELECT physicianName FROM physicianNamesSpecialties WHERE (specialty, specialty2, specialty3, specialty4) ='{}'".format(r)
c.execute(getPhysiciansql_cmd)
physResult = choice(c.fetchall())
cleanedUpPhysResult = physResult[0]
print("This case is going to", cleanedUpPhysResult+".")
insert(cleanedUpPhysResult)
select()
else:
distributefairly(inputCaseNumber, inputCaseType)
conn = mariaDB.connect(host='xxxxx', user='xxxxx',password='xxxxxx',db='lookupDB')
conn2 = mariaDB.connect(host='xxxxxxl', user='xxxxx',password='xxxxxxx',db='workQueue')
c = conn.cursor()
c2 = conn2.cursor()
count = 0
while True:
startup()
parse()
This line of code .execute(...) will fail:
getPhysiciansql_cmd =
"SELECT physicianName FROM physicianNamesSpecialties WHERE (specialty, specialty2, specialty3, specialty4) ='{}'".format(r)
c.execute(getPhysiciansql_cmd)
My Var r holds this data:
print("This is a", r, "service case. It is a", d,"type case.")
This is a THORACIC service case. It is a TRANSBRONCHIAL WANG NEEDLE ASPIRATION type case.
My Var getPhysiciansql_cmd holds this data:
getPhysiciansql_cmd = SELECT physicianName FROM
physicianNamesSpecialties WHERE (specialty, specialty2, specialty3, specialty4) ='THORACIC'
I think this is just a matter of how I'm using the WHERE clause. I saw somewhere that the WHERE with multiple fields is written, you use parens around the fields. I've experimented in reducing this to the bare minimum and using the OR operator in the WHERE clause and I have successfully received a proper response.
import mysql.connector as mariaDB
conn = mariaDB.connect(host='xxxxx', user='xxxx',password='xxxxx',db='lookupDB')
conn2 = mariaDB.connect(host='xxxxx', user='xxxx',password='xxxxxx',db='workQueue')
c = conn.cursor()
c2 = conn2.cursor()
inputSpecialty = input("specialty? ")
r = inputSpecialty
c.execute("SELECT physicianName FROM physicianNamesSpecialties WHERE specialty = %s OR specialty2 = %s OR specialty3 = %s OR specialty4 = %s", (r,r,r,r))
physResult = c.fetchall()
cleanedUpPhysResult = physResult
print(cleanedUpPhysResult)
Output:
specialty? THORACIC
[('Song',), ('Han',), ('He',), ('Goldfischer',)]

Automatic Stack Trace not being given in threads other than the main thread

I have a rather large program which loads some data from an excel file and populates a form, this can take a long time due to the size of the file so I have been moving the loading function onto a separate thread, the only problem is for some reason in this new thread I am not getting an automatic stack trace in the console whenever an error occurs. It has just been failing silently which is making debugging it a real pain.
I am using pydev in eclipse, I wrote the following test case to be sure everything is working correctly.
from PyQt4 import QtCore
class OtherThread(QtCore.QThread):
def __init__(self):
super(OtherThread, self).__init__()
def run(self):
try:
print(1/0)
except Exception as e:
print("exception caught in other thread: \n{0}".format(e))
class MainThread():
def __init__(self):
self.otherThread = OtherThread()
def run(self):
try:
print(1/0)
except Exception as e:
print("exception caught in main thread: \n{0}".format(e))
self.otherThread.run()
def main():
mainThread = MainThread()
mainThread.run()
if __name__ == '__main__':
main()
When I run this both exceptions are caught properly and when I comment out the try block in the tread object it also works just fine, I get my stack trace as expected. I am really at a loss as to what is going on. Is there something I could have done to cause this behavior?
Here is the code of the program I am working on.
def run(self):
print("excel thread running")
workbook = xlrd.open_workbook(self.path)
worksheet = workbook.sheet_by_name('PNA Form')
#
currentRow = 13 # start grabbing pna data
numRowStart = currentRow
newPartCol= 0
oldPartCol = 10
descriptionCol = 2
#
numberOfRows = worksheet.nrows - 1
#
print("number of rows = {0}".format(numberOfRows))
PNA = []
#
current_color = False
while (currentRow < numberOfRows):
print("about to parse excel rows")
newPartCell = int(worksheet.cell(currentRow,newPartCol).value)
oldPartCell = int(worksheet.cell(currentRow,oldPartCol).value)
descriptionCell = QtCore.QString(worksheet.cell(currentRow,descriptionCol).value)
print("excel rows parsed: {0}, {1}, {2}, {3}".format(oldPartCell,newPartCell,descriptionCell,current_color))
print("running line excel row {0}: {1}".format(currentRow, str(descriptionCell)))
if not self.isStrikethrough(currentRow,0): #make sure the line does not have strike through
#self.guiHandel.BOMVal.addPNARow(oldPN = oldPartCell, newPN = newPartCell, disc = descriptionCell)
print("about to emit pna row tracker for {0}".format(descriptionCell))
self.addPNARowTracker.emit(oldPartCell,newPartCell,descriptionCell)
print("thread still running after pna row tracker emit")
if (oldPartCell != "" and not self.isStrikethrough(currentRow,0)):
PNA.append((num(oldPartCell),num(newPartCell)))
current_color = not current_color
#self.guiHandel.pnaVerticalLayoutScroll.addWidget(PNACell(oldPartCell,newPartCell,descriptionCell,color = current_color))
print("about to emit addPNARow: {0}, {1}, {2}, {3}".format(oldPartCell,newPartCell,descriptionCell,current_color))
self.addPNARow.emit(oldPartCell,newPartCell,descriptionCell,current_color)
#self.guiHandel.widgetStack.append(PNACell(oldPartCell,newPartCell,descriptionCell,color = current_color))
print("thread still running after add pna row emit")
currentRow += 1
#self.guiHandel.pbar.setValue(int(100*(currentRow-13)/numberOfRows))
print("currentRow =",currentRow)
self.updateProgress.emit(int(100*(currentRow-numRowStart)/(numberOfRows-numRowStart)))
print(PNA)
self.done.emit()
Here is the console output when it fails.
slot add pna row tracker called
running is about to return
about to emit addPNARow: 28458820, 28489881, INST CSTR-ASM,DIESEL,KM,UP,GAT, False
thread still running after add pna row emit
('currentRow =', 29slot add pna row called)
about to parse excel rows
Added addPNARow: 28458820, 28489881, INST CSTR-ASM,DIESEL,KM,UP,GAT, False
excel progress update called ------- progress = 20
When running through a debugger it stops at this line:
newPartCell = int(worksheet.cell(currentRow,newPartCol).value)
I tried wrapping it in a try block but it never got to the exception. The cell it is trying to read is blank.
What is going on here? Any ideas would be greatly appreciated.
Answer to the question found here: error in pyqt qthread not printed
Basically you need to manually encapsulate the entire run method of the QThread object and then manually rethrow errors to stderr

SQLite 3: sqlite InterfaceError: Error binding parameter 0 - probably unsupported type

I am using sqlite3 python modules and th following code returns the error
> Exception in Tkinter callback Traceback (most recent call last):
> File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
> return self.func(*args) File "C:\MonitorSoft\MonitorSoft.py", line 199, in LoadSQL
> CurFiles.execute('SELECT * FROM Files WHERE CheckSum = ?', (WinRedactor.Table.item(WinRedactor.Table.selection()[0],
> option='values')[3],))
>
> sqlite3.InterfaceError: Error binding parameter 0 - probably
> unsupported type.
def LoadSQL(event):
con = sqlite3.connect('C:/MonitorSoft/SoftMon.db')
CurPackets = con.cursor()
CurFiles = con.cursor()
CurFilesIn = con.cursor()
curFilesPackets = con.cursor()
CurPackets.execute('SELECT * FROM Packets WHERE PacketName = ?', (WinRedactor.Combobox.get(),))
for RowPackets in CurPackets:
if WinRedactor.Combobox.get() !='NULL':
while WinRedactor.Table.selection() != '':
CurFilesIn.execute('INSERT INTO Files (id, FilePath, FileName, Size, CheckSum) VALUES(NULL, ?, ?, ?, ?)',((WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[0]), (WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[1]), (WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[2]),(WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[3])))
con.commit()
CurFiles.execute('SELECT * FROM Files WHERE CheckSum = ?', (WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[3],))
for RowFiles in CurFiles:
if RowFiles[4] == (WinRedactor.Table.item(WinRedactor.Table.selection()[0], option='values')[3]):
curFilesPackets.execute('INSERT INTO FilesPackets (IDFile, IDPacket) VALUES(?,?)',((RowFiles[0]), (RowPackets[0])))
con.commit()
WinRedactor.Table.delete(WinRedactor.Table.selection()[0])
con.commit()
con.close()
BtnLoadSQL = Button(WinRedactor)
BtnLoadSQL["text"] = "LOAD SQL"
BtnLoadSQL.bind("<Button-1>",LoadSQL)
BtnLoadSQL.pack()
What the problem?
Concluded the data as you wrote.
The data are the same everywhere.
The error appears randomly on different lines.
Here's an example of what gives the output as you wrote:
selection is ('I04C', 'I04D', 'I04E', 'I04F', 'I050', 'I051', 'I052', 'I053', 'I054', 'I055', 'I056', 'I057', 'I058', 'I059', 'I05A', 'I05B', 'I05C', 'I05D', 'I05E', 'I05F', 'I060', 'I061', 'I062', 'I063', 'I064', 'I065', 'I066', 'I067', 'I068', 'I069', 'I06A', 'I06B', 'I06C', 'I06D', 'I06E', 'I06F', 'I070', 'I071', 'I072', 'I073', 'I074', 'I075', 'I076', 'I077', 'I078', 'I079', 'I07A', 'I07B', 'I07C', 'I07D', 'I07E', 'I07F', 'I080', 'I081', 'I082', 'I083', 'I084', 'I085', 'I086', 'I087', 'I088', 'I089', 'I08A', 'I08B', 'I08C', 'I08D', 'I08E', 'I08F', 'I090', 'I091', 'I092', 'I093', 'I094', 'I095', 'I096', 'I097', 'I098', 'I099', 'I09A', 'I09B', 'I09C', 'I09D', 'I09E', 'I09F', 'I0A0', 'I0A1', 'I0A2', 'I0A3', 'I0A4', 'I0A5', 'I0A6', 'I0A7', 'I0A8', 'I0A9', 'I0AA', 'I0AB', 'I0AC', 'I0AD', 'I0AE', 'I0AF', 'I0B0', 'I0B1', 'I0B2', 'I0B3', 'I0B4', 'I0B5', 'I0B6', 'I0B7', 'I0B8', 'I0B9', 'I0BA', 'I0BB', 'I0BC', 'I0BD', 'I0BE', 'I0BF', 'I0C0', 'I0C1', 'I0C2', 'I0C3', 'I0C4', 'I0C5', 'I0C6', 'I0C7', 'I0C8', 'I0C9', 'I0CA')
first selection is I04C
values are ('C:\\WINDOWS\\Resources\\Themes\\Luna\\Shell\\Metallic', 'shellstyle.dll', '362496', '05b3f32c7f3bd125446d024a30373c9d')
checksum: 05b3f32c7f3bd125446d024a30373c9d
selection is ('I04D', 'I04E', 'I04F', 'I050', 'I051', 'I052', 'I053', 'I054', 'I055', 'I056', 'I057', 'I058', 'I059', 'I05A', 'I05B', 'I05C', 'I05D', 'I05E', 'I05F', 'I060', 'I061', 'I062', 'I063', 'I064', 'I065', 'I066', 'I067', 'I068', 'I069', 'I06A', 'I06B', 'I06C', 'I06D', 'I06E', 'I06F', 'I070', 'I071', 'I072', 'I073', 'I074', 'I075', 'I076', 'I077', 'I078', 'I079', 'I07A', 'I07B', 'I07C', 'I07D', 'I07E', 'I07F', 'I080', 'I081', 'I082', 'I083', 'I084', 'I085', 'I086', 'I087', 'I088', 'I089', 'I08A', 'I08B', 'I08C', 'I08D', 'I08E', 'I08F', 'I090', 'I091', 'I092', 'I093', 'I094', 'I095', 'I096', 'I097', 'I098', 'I099', 'I09A', 'I09B', 'I09C', 'I09D', 'I09E', 'I09F', 'I0A0', 'I0A1', 'I0A2', 'I0A3', 'I0A4', 'I0A5', 'I0A6', 'I0A7', 'I0A8', 'I0A9', 'I0AA', 'I0AB', 'I0AC', 'I0AD', 'I0AE', 'I0AF', 'I0B0', 'I0B1', 'I0B2', 'I0B3', 'I0B4', 'I0B5', 'I0B6', 'I0B7', 'I0B8', 'I0B9', 'I0BA', 'I0BB', 'I0BC', 'I0BD', 'I0BE', 'I0BF', 'I0C0', 'I0C1', 'I0C2', 'I0C3', 'I0C4', 'I0C5', 'I0C6', 'I0C7', 'I0C8', 'I0C9', 'I0CA')
first selection is I04D
values are ('C:\\WINDOWS\\Resources\\Themes\\Luna\\Shell\\NormalColor', 'shellstyle.dll', '361472', '23ecf1c97b1eb5d94a25dc677ec464e5')
checksum: 23ecf1c97b1eb5d94a25dc677ec464e5
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\MonitorSoft\MonitorSoft.py", line 213, in LoadSQL
CurFiles.execute('SELECT * FROM Files WHERE CheckSum = ?', (checksum,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
This is the last two conclusions, at last, as you can see there is a bug.
To solve this, the very first step should be to print out all of your immediate values, so you can verify they are what you think they are. As a side effect, this makes your select statement much easier to read.
For example:
selection = WinRedactor.Table.selection()
print("selection is", selection)
first_selection = selection[0]
print("first selection is", first_selection)
values = WinRedactor.Table.item(first_selection, option='values')
print("values are", values)
checksum = values[3]
print("checksum:", checksum)
CurFiles.execute('SELECT * FROM Files WHERE CheckSum = ?', (checksum,))
My guess is, you'll be surprised at what you see. At the very least, you've have more data to put into your question so that we know precisely what the data is.

Categories