Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with MySQL booleans in python

I am querying a MySQL database in python and selecting a boolean value -- so the response from MySQL is either the string 'True' or 'False'. I would like to execute further code based on the boolean value from MySQL.

E.g.

data = 'False'  # assume this is what was returned by MySQL.
if data:
  print 'Success'  #really this would be where I would execute other if True.
else:
  print 'Fail'  #really this would be where I would execute other code if False

But I can't do this because

if data:

will always return True

So how do I convert the string that MySQL is returning into a boolean in python?

Currently I have:

data = 'False'  # assume this is what was returned by MySQL.
if data == 'True':
  print 'Success'
else:
  print 'Fail'

I believe there must be a better way to do this in python -- likely there is something simple I am missing.


1 Answers

Boolean in MySQL is TINYINT(1). Checking for 1 or 0 might work

like image 95
Imran Avatar answered May 08 '26 02:05

Imran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!