Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with condition: if a predefined text exists in the database field, then the if condition is satisfied

My idea is to insert a condition in the if, if in the specified field in the database it says "smartphone". I have a problem inserting this condition in the if. I would like to obtain that if in the device field of the database it says "smartphone", then the if condition is true. I’m not getting errors, but Hello doesn’t print

        conn = sqlite3.connect('database')
        cur = conn.cursor()  

        cursor.execute("SELECT device FROM info")
        result = cursor.fetchone()

        if result == "smartphone":
            print("Hello")

The database is:

CREATE TABLE "info" (
    "id"    INTEGER,
    "address"   INTEGER,
    "number_phone"  INTEGER,
    "device"    INTEGER,
    PRIMARY KEY("id")
);

The row, for example, is: id,Street Chelsea 23, 0123456, smartphone

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the problem is that your device field on the table is type INTEGER not VARCHAR so the record would never be type string

Edit 1

after update the datatype to TEXT try fetching the first value from you record, example:

if result[0] == "smartphone":
  print("Hello")

if that doesn’t work try printing your result on screen to understand the value of your query

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading