Sorry for the long question. I’ve done very little programming on any language.
I tried using Python 3.11.2 as well as some versions of 3.10 and 3.9 with the same error.
When I try to create a text variable that contains a query to be sent to an SQL database, I get the beginning of the query messed up with the end.
Basically, I get:
q=f'select thetype,thetxt,thebin from {tbl} where theid="{oneitem}";'
That should contain select thetype,thetxt,thebin from thetable where theid="item1";
but ends up containing ";lect thetype,thetxt,thebin from thetable where theid="item1
The following code worked perfectly:
from array import *
tbl="thetable"
def toprint(t):
thelist=t.split("\n")
for oneitem in thelist:
q=f'select thetype,thetxt,thebin from {tbl} where theid="{oneitem}";'
print("todolist query:\t\t"+q)
print("\n")
return 0
# MAIN STARTS HERE
q=f'select thetype,thetxt,thebin from {tbl} where theid="thefirst";'
print("Main query:\t\t"+q+"\n")
thetxt="""abc
ABC
123
xyz
XYZ"""
thestatus=toprint(thetxt)
The output is:
Main query: select thetype,thetxt,thebin from thetable where theid="thefirst";
todolist query: select thetype,thetxt,thebin from thetable where theid="abc";
todolist query: select thetype,thetxt,thebin from thetable where theid="ABC";
todolist query: select thetype,thetxt,thebin from thetable where theid="123";
todolist query: select thetype,thetxt,thebin from thetable where theid="xyz";
todolist query: select thetype,thetxt,thebin from thetable where theid="XYZ";
But that was only a very simplified code.
Another bit of code that is less simplified and shows the error is the following:
import os
from os import path
import shutil
import sys
import random
from math import *
import locale
from datetime import date
import mariadb
from array import *
cur="" # Database select result cursor
tbl="thetable"
firstone="thefirst"
def dbconnect():
result=[0,"",""]
conn=-1
try:
conn=mariadb.connect(user="theuser",password="thepasswd",host="127.0.0.1",port=3306,database="thedb")
except mariadb.Error as e:
result[0]=-1
result[1]=e
result[2]=conn
return result
def dbcursor(conn):
cur = conn.cursor()
result=[0,"",cur]
return result
def askquery(q):
result=[0,"",""]
try:
cur.execute(q)
except mariadb.Error as e:
result[0]=-1
result[1]=e
result[2]=cur
return result
def dbdisconnect(conn):
conn.close()
def chkres(result):
if result[0]==-1:
print(result[1])
quit()
# type functions start below
def todolist(lstxt):
result=[0,"",""]
if lstxt=="":
return result
thelist=lstxt.split("\n")
for oneitem in thelist:
q=f'select thetype,thetxt,thebin from {tbl} where theid="{oneitem}";'
if q[0]==';':
q=f'select thetype,thetxt,thebin from {tbl} where theid="{oneitem}";'
print("todolist query:\t\t"+q)
result=askquery(q)
chkres(result)
cur=result[2]
datatuple=cur.fetchone()
thetype=datatuple[0]
thetxt=datatuple[1]
thebin=datatuple[2]
result=picktype(thetype,thetxt,thebin)
chkres(result)
print("\n")
return result
def picktype(thetype,thetxt,thebin):
if thetype=='direct':
result[2]=thetxt
print(thetext)
elif thetype=='todolist':
result=todolist(thetxt)
chkres(result)
else:
result=[-1,'Unknown type '+thetype+'"']
chkres(result)
return result
# MAIN LOOP STARTS HERE
result=dbconnect()
chkres(result)
conn=result[2]
result=dbcursor(conn)
chkres(result)
cur=result[2]
q=f'select thetype,thetxt,thebin from {tbl} where theid="{firstone}";'
print("Main query:\t\t"+q)
result=askquery(q)
chkres(result)
cur=result[2]
startuple=cur.fetchone()
thetype=startuple[0]
thetxt=startuple[1]
thebin=startuple[2]
result=picktype(thetype,thetxt,thebin)
chkres(result)
dbdisconnect(conn)
quit()
The output is:
Main query: select thetype,thetxt,thebin from thetable where theid="thefirst";
";dolist query: select thetype,thetxt,thebin from thetable where theid="item1
Traceback (most recent call last):
File "/home/theuser/./test.py", line 102, in <module>
result=picktype(thetype,thetxt,thebin)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/theuser/./test.py", line 79, in picktype
result=todolist(thetxt)
^^^^^^^^^^^^^^^^
File "/home/theuser/./test.py", line 66, in todolist
thetype=datatuple[0]
~~~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
Note that the first output works and the second doesn’t, while on the shorter example both worked.
I am trying to figure out if there’s something related to how I delimit strings, if there’s some variable running out of memory, some python configuration issues or anything else, but I can’t think of any solution.
Do you know what I am doing wrong, please?
Or can you point me in the right direction, please?
Thank you in advance.