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],))
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.