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

Time:12-13

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

CodePudding user response:

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

  • Related