Python Forum
[Tkinter] field in db as text - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] field in db as text (/thread-34286.html)



field in db as text - rwahdan - Jul-15-2021

Hi,

I am trying to get a field from db which is a text but when trying to print it it gives me in form of list. how to print the value as string instead!

        conn = sqlite3.connect('emsat_data.db')
        c = conn.cursor()
        c.execute("SELECT candidate_answer from candidate_math_answers where exam_photo=?", (thephoto[count - 1],))
        conn.commit()
        records = c.fetchall()
        theanswer=str(records[0])
        conn.close()
        print(theanswer)
note that the number of records is one record only that is why it is not in loop. When I try this code I get:
Quote:('A. 2',)
and what I want is
Quote:A. 2



RE: field in db as text - GOTO10 - Jul-15-2021

As long as your are certain the format of your data will be consistent, you could use * to unpack the iterable and get the output you are looking for:
x=('A. 2',)
print(x)
print(*x)
Output:
('A. 2',) A. 2



RE: field in db as text - deanhystad - Jul-15-2021

Really? You are asking how to get an item from a tuple?


RE: field in db as text - rwahdan - Jul-15-2021

(Jul-15-2021, 11:40 AM)deanhystad Wrote: Really? You are asking how to get an item from a tuple?

No I am getting the value from database and its data type is text. My question is why it is printed as tuple and not as text??


RE: field in db as text - GOTO10 - Jul-15-2021

(Jul-15-2021, 01:38 PM)rwahdan Wrote:
(Jul-15-2021, 11:40 AM)deanhystad Wrote: Really? You are asking how to get an item from a tuple?
No I am getting the value from database and its data type is text. My question is why it is printed as tuple and not as text??

The fetchall() method returns a list of tuples. Your assignment on line 6 assigns the variable to the first value in the list, which is a tuple. You need to go another layer deep and pull the string from the tuple if that's what you are looking for:
theanswer=records[0][0]



RE: field in db as text - deanhystad - Jul-15-2021

This is the kind of question you should be answering yourself. The skills learned doing so will make you a better programmer.

The first question you should have asked yourself is "Why is this thing printing like a list?" It is actually printing like a tuple, but that isn't important for the purposes of this discussion.

When you have questions about the representation used by the print command the first thing you should do is verify that you know the type of the object. If you have a debugger set a breakpoint and ask. If not, add a print statement
print(type(records), records)
This print statement would tell you that records is a tuple and it would print the tuple. If there is only one match for the query I would expect to see something like this:
Output:
<class 'tuple'> (('A. 2',),)
To find out what is in the tuple I alter the print statement.
print(type(records), records, type(records[0]), records[0])
Output:
<class 'tuple'> (('A. 2',),) <class 'tuple'> ('A. 2',)
Another tuple. I change the print one last time to get the result.
print(type(records), records, type(records[0]), records[0], type(records[0][0]), records[0][0])
Output:
<class 'tuple'> (('A. 2',),) <class 'tuple'> ('A. 2',) <class 'str'> A. 2
Now I know that the cursor.fetchall() command returns a tuple of tuples, and the inner tuples contain the query result. I wonder if this is always the case or a peculiarity of my query so I reference the documentation.
Quote:fetchall()
Fetches all (remaining) rows of a query result, returning a list. Note that the cursor’s arraysize attribute can affect the performance of this operation. An empty list is returned when no rows are available.
from https://python.readthedocs.io/en/latest/library/sqlite3.html

Hmmm. Not all that useful. The docs says it returns a list, not a tuple. This kind of discrepancy happens quite often so I always try to verify what I read. Next I might try a query that returns multiple results or different types of results. I would analyze the return value in each case and improve my understanding of the tool I was using until I was at least proficient in it's use. This would slow my initial development, but a little study at the front end reaps great rewards in and overall reduction in development time and a huge reduction in debugging and testing time (because you now know enough to quickly spot errors and know what can go wrong so you can write effective tests).


RE: field in db as text - rwahdan - Jul-16-2021

(Jul-15-2021, 02:54 PM)deanhystad Wrote: This is the kind of question you should be answering yourself. The skills learned doing so will make you a better programmer.

The first question you should have asked yourself is "Why is this thing printing like a list?" It is actually printing like a tuple, but that isn't important for the purposes of this discussion.

When you have questions about the representation used by the print command the first thing you should do is verify that you know the type of the object. If you have a debugger set a breakpoint and ask. If not, add a print statement
print(type(records), records)
This print statement would tell you that records is a tuple and it would print the tuple. If there is only one match for the query I would expect to see something like this:
Output:
<class 'tuple'> (('A. 2',),)
To find out what is in the tuple I alter the print statement.
print(type(records), records, type(records[0]), records[0])
Output:
<class 'tuple'> (('A. 2',),) <class 'tuple'> ('A. 2',)
Another tuple. I change the print one last time to get the result.
print(type(records), records, type(records[0]), records[0], type(records[0][0]), records[0][0])
Output:
<class 'tuple'> (('A. 2',),) <class 'tuple'> ('A. 2',) <class 'str'> A. 2
Now I know that the cursor.fetchall() command returns a tuple of tuples, and the inner tuples contain the query result. I wonder if this is always the case or a peculiarity of my query so I reference the documentation.
Quote:fetchall()
Fetches all (remaining) rows of a query result, returning a list. Note that the cursor’s arraysize attribute can affect the performance of this operation. An empty list is returned when no rows are available.
from https://python.readthedocs.io/en/latest/library/sqlite3.html

Hmmm. Not all that useful. The docs says it returns a list, not a tuple. This kind of discrepancy happens quite often so I always try to verify what I read. Next I might try a query that returns multiple results or different types of results. I would analyze the return value in each case and improve my understanding of the tool I was using until I was at least proficient in it's use. This would slow my initial development, but a little study at the front end reaps great rewards in and overall reduction in development time and a huge reduction in debugging and testing time (because you now know enough to quickly spot errors and know what can go wrong so you can write effective tests).

Thanks a lot for the help you gave in details. I really appreciate it.