Search code examples
pythonpyqt4

Converting values from sqlite results


Issue Image and expected output

I am having trouble in converting from list to separate strings in python. Below is the code used to get the strings and the exception:

Code:

mycursor.execute("SELECT fid,contents,input_type  FROM frame_report1")

myresult = mycursor.fetchall()
print myresult
# myresult --->[(449, u'text1', u'checkbox'), (454, u'text2', u'textbox'), (455, u'text3', u'textbox')]
print type(myresult)#list type
for x,x1,x2 in myresult:
    g = int(''.join(map(str, x))) #int type
    l = ''.join( x1) #string type
    k = ''.join( x2) #string type

Exception:

g = ''.join(map(str, x))
TypeError: argument 2 to map() must support iteration

I want to convert values from myresult to the variables g, l, and k.

EDIT : @Matt B : thanks for the response, I have used the code mentioned by you but its not helpfull, we need to get the row content 449, 454 455 in separate String array, currently i am getting combined. please guide, thanks in adavance, please check the image attached Image showing issue image and expected image.

Code :

myresult = [(449, u'text1', u'checkbox'), (454, u'text2', u'textbox'), (455, u'text3', u'textbox')]
row = 0
for x, x1, x2 in myresult:
    g = int(''.join(map(str, x)))  # int type

    l = ''.join(x1)  # string type
    k = ''.join(x2)  # string type
    rcount_general = self.tableWidget.rowCount()
    self.tableWidget.insertRow(rcount_general)
    r = 0
    for i in range(rcount_general + 1):
        self.tableWidget.setItem(row, 0, QtGui.QTableWidgetItem(g))
        self.tableWidget.setItem(row, 1, QtGui.QTableWidgetItem(l))
        self.tableWidget.setItem(row, 2, QtGui.QTableWidgetItem(k))        
        r = r + 1
    row = row + 1

Solution

  • It is not necessary to use join, you just have to iterate with the help of enumerate:

    for row, result in enumerate(myresult):
        self.tableWidget.insertRow(self.tableWidget.rowCount())
        for col, value in enumerate(result):
            self.tableWidget.setItem(row, col, QtWidgets.QTableWidgetItem(str(value)))