SQLite Forum

SQLite3 shell doing math operation on parameter substitution
Login
I believe it is a bug: SQLite3 (v3.37.0) doing math calculation on parameter setting instead of saving in database data as it is.

Test case:

------------------------- **DDL** ----------------------------------------

```
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "ips" (
  ipid      INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  ip        TEXT NOT NULL UNIQUE,
  block     INTEGER NOT NULL,
  country   TEXT,
  contact   TEXT,
  note      TEXT,
  created   TEXT DEFAULT CURRENT_TIMESTAMP,
  updated   TEXT DEFAULT CURRENT_TIMESTAMP
) STRICT;
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "ips" (
  ipid      INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  ip        TEXT NOT NULL UNIQUE,
  block     INTEGER NOT NULL,
  country   TEXT,
  contact   TEXT,
  note      TEXT,
  created   TEXT DEFAULT CURRENT_TIMESTAMP,
  updated   TEXT DEFAULT CURRENT_TIMESTAMP
) STRICT;
CREATE INDEX IF NOT EXISTS "idx_block" on "ips" ("block");
CREATE UNIQUE INDEX IF NOT EXISTS "idx_ip" on "ips" ("ip");
CREATE TRIGGER IF NOT EXISTS "update_timestamp"
AFTER UPDATE OF "ip", "country", "contact", "note" ON "ips" FOR EACH ROW
WHEN OLD.updated = NEW.updated
BEGIN
    UPDATE "ips" SET "updated" = datetime('now') WHERE ipid = NEW.ipid;
END;
COMMIT;
CREATE UNIQUE INDEX IF NOT EXISTS "idx_ip" on "ips" ("ip");
CREATE TRIGGER IF NOT EXISTS "update_timestamp"
AFTER UPDATE OF "ip", "country", "contact", "note" ON "ips" FOR EACH ROW
WHEN OLD.updated = NEW.updated
BEGIN
    UPDATE "ips" SET "updated" = datetime('now') WHERE ipid = NEW.ipid;
END;
COMMIT;
```

------------------------------------------------------------------------


Bellow is a simplified script that inserting data into database:

------------------------------------------------------------------------

```
#!/bin/sh                                                                                                                                                         
                                                                                                                                                                  
ip='184.82.96.0/19'                                                                                                                                               
block='TRUE'                                                                                                                                                      
country='TN'                                                                                                                                                      
contact='+66-2-615-3964' # it doing math operation on this parameter                                                                                                               
note='{abuse:"Password brute-forcers", {org:"AIS Fibre","abuse-email":"xxxx@ais.co.th"}}'                                                                    
                                                                                                                                                                  
################################################################################                                                                                  
echo "                                                                                                                                                            
.parameter set @ip          '${ip}'                                                                                                                               
.parameter set @block        ${block}                                                                                                                             
.parameter set @country     '${country}'                                                                                                                          
.parameter set @contact     '${contact}'                                                                                                                          
.parameter set @note        '${note}'                                                                                                                             
                                                                                                                                                                  
INSERT OR IGNORE INTO ips (ip,block,country,contact,note) VALUES (@ip,@block,@country,@contact,@note);" | sqlite3 test.db                                         
                                                                                                                                                                  
echo -------------------------------------------------------                                                                                                      
echo ".parameter set @ip '${ip}'                                                                                                                                  
SELECT * FROM ips WHERE ip = @ip; "  | sqlite3 -line test.db                                                                                                      
echo -------------------------------------------------------                                                                                                      
                                                                                                                                                                  
exit
```

------------------------------------------------------------------------


And here is result:

```

-------------------------------------------------                                                                                                                 
   ipid = 1                                                                                                                                                       
     ip = 184.82.96.0/19                                                                                                                                          
  block = 1                                                                                                                                                       
country = TN                                                                                                                                                      
contact = -4515                                                                                                                                                   
   note = {abuse:"Password brute-forcers", {org:"AIS Fibre","abuse-email":"xxxx@ais.co.th"}}                                                                 
created = 2022-01-10 22:59:40                                                                                                                                     
updated = 2022-01-10 22:59:40              
                                                                                                                       
-------------------------------------------------

```

**Bug:** instead of inserting parameter **@contact**, SQLite3 did first calculation on this parameter and saved inside of database result of arithmetic calculation instead of saving phone number (+66-2-615-3964 = -4515)