Python Forum
Storing MySQL BIT Data Type data in python variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Storing MySQL BIT Data Type data in python variable (/thread-14983.html)



Storing MySQL BIT Data Type data in python variable - krushna - Dec-28-2018

I need a script to store the values of mysql statement . Below is the snippet I wrote .

>>import pymysql
>>username='kbr_dev'
>>sel_query=("select name,online_status from user where name = '%s' " %(username))
>>mysqlconnnect_test = pymysql.connect(host = 'localhost', user = 'root', password='root123', db='dev')
>>cursor = mysqlconnect_test.cursor()
>>cursor.execute(sel_query)
>>records = cursor.fetchall()
>>print(records)
With the above code, I get 3 type of result with different username .

(('kbr_dev', None),)

(('kbr1_dev', '\x01'),)

(('kbr2_dev', '\x00'),)
How to store the 2nd value (None/00/01) to a variable.

the below command works for last 2 answer but not the first one .

>>ord(records[0][1])
Any help is highly appreciable .

Note :- In mysql, the online_status column contains 3 type of values i.e. 0,1 or NULL .

Thanks and regards


RE: Storing MySQL BIT Data Type data in python variable - micseydel - Dec-28-2018

What do you want for the first one? None? Because None has no ordinal value.

I'd create a dictionary and just index into it - keys are the three possible values you expect.


RE: Storing MySQL BIT Data Type data in python variable - krushna - Dec-31-2018

I have to store the 2nd value and then add a condition e.g.

if 2nd_value == 'None':
   online_status='offline'
elif 2nd_value == 0 :
   online_status='Offline'
elif 2nd_value == 1
   online_status='Online'
Here the issue is the query is failing with below code when the value is None .

2nd_value=(ord(records[0][1]))
Let me know if any better approach to get my task done .