Python Forum
Storing MySQL BIT Data Type data in python variable
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing MySQL BIT Data Type data in python variable
#1
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
Reply
#2
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.
Reply
#3
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 .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data cleaning and analysys in Python siriuslight 5 1,127 Jun-13-2026, 08:27 AM
Last Post: noisefloor
  python to read the data from oracle melinda 3 1,663 Aug-18-2025, 10:24 AM
Last Post: Larz60+
  Tkinter: Problem with storing data from one window to the other iforire 1 1,003 May-11-2025, 01:31 PM
Last Post: deanhystad
  tkinter only storing last element/data from entry widget bertschj1 8 1,969 May-06-2025, 11:54 PM
Last Post: deanhystad
  Changing client.get() method type based on size of data... dl0dth 1 1,322 Jan-02-2025, 08:30 PM
Last Post: dl0dth
  LLM data I/O - for storing notes Alkanet 0 830 Dec-19-2024, 09:08 AM
Last Post: Alkanet
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 1,352 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  A question on Boolean Data Type Hudjefa 5 2,034 Aug-13-2024, 11:03 AM
Last Post: Hudjefa
  Python script to extract data from API to database melpys 0 1,803 Aug-12-2024, 05:53 PM
Last Post: melpys
  To fetch and iterate data from CSV file using python vyom1109 3 1,887 Aug-05-2024, 10:05 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020