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
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  need help with data analysing with python and sqlite Hardcool 2 299 Jan-30-2024, 06:49 AM
Last Post: Athi
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 989 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Mysql and mysql.connector error lostintime 2 612 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Bulk loading of data using python shivamsvmsri 2 615 Sep-28-2023, 09:04 AM
Last Post: shivamsvmsri
  Python loop for posting data to web marciokoko 10 1,479 Aug-26-2023, 02:17 AM
Last Post: deanhystad
  How to detect abnormal data in big database python vanphuht91 5 1,064 Jun-27-2023, 11:22 PM
Last Post: Skaperen
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,667 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Python 3.11 data import question love0715 2 773 Mar-05-2023, 06:50 PM
Last Post: snippsat
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 897 Jan-21-2023, 05:01 PM
Last Post: nicholas

Forum Jump:

User Panel Messages

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