SQLite Forum

Automatic change True value to 1

Automatic change True value to 1

(1) By Illu (Illu__) on 2021-03-30 18:56:23 [link] [source]

Hi Guys,
i don't know what i did wrong; i try to build some header functions for my python application so i don't call every statement in a function;

            CurrentElo      TEXT,
            Rank            TEXT,
            EloToday        TEXT,
            WinStreak       TEXT,
            TotalMatches    TEXT,
            MatchesWon      TEXT  

        sqlite3db.TExecSqlInsert(DBNAME, """
                            INSERT INTO CFG_STATS_FACEIT
                            VALUES (?, ?, ?, ?, ?, ?)""", FACEIT_List)

def TExecSqlInsert(database_name: str, statement: str, bind: list = None):
        conn = sqlite3.connect(database_name)
        c = conn.cursor()
        if bind:
            if bind.__class__.__name__ in ('list', 'tuple'):
                c.execute(statement, bind)
                c.execute(statement, [bind])
    except sqlite3.Error as err:

(True, True, True, True, True, True)

does someone see any errors ?

Cheers and thx

(2) By Keith Medcalf (kmedcalf) on 2021-03-30 19:48:46 in reply to 1 [link] [source]

TExecSqlInsert is a function, not a method of a class. However if one removes the useless instance variable it works as coded. What exactly is the problem?

(3) By Illu (Illu__) on 2021-03-31 06:28:04 in reply to 2 [link] [source]

Hey Keith,

my problem is i insert the vlaues 'True' into a Text Column. and SQLITE3 is converting this to a 1. I was 100% sure the "boolean" function isn't avaible in SQLITE3.

(4) By Stephan Beal (stephan) on 2021-03-31 06:42:38 in reply to 3 [source]

I was 100% sure the "boolean" function isn't avaible in SQLITE3.

True and false are keywords in sqlite:

$ sqlite3
sqlite> select true;
sqlite> select false;

(5) By Harald Hanche-Olsen (hanche) on 2021-03-31 07:57:51 in reply to 4 [link] [source]

This is possibly a redundant remark, but it's worth keeping in mind the difference between the keyword true and the string 'true'. The latter is not converted to 1.

(6) By Keith Medcalf (kmedcalf) on 2021-03-31 08:17:24 in reply to 3 [link] [source]

True is 1, and False is 0. The sqlite3 Python wrapper is translating the Python Boolean values (True, False) to something understood by sqlite3 -- the integers 1 and 0 respectively.

Since you are inserting those integer values into a column with TEXT affinity, sqlite3 is converting the integer 1 and 0 to the strings '1' and '0' respectively.

If you insert the value 'True' then you will get that value, however True is not 'True'. True is a python boolean type (1, since that is not 0) and 'True' is a text string (and 'True' is actually False in sqlite3, because when the string 'True" is converted to a number, that number is 0, which is False).