Increase int value in null column - sqlite

I made a stupid mistake and created a column like this:
CREATE TABLE mytable (mycol INTEGER, ...)
As you can see, I forgot to define a default value like "DEFAULT 0".
In my code, I need to raise the value in "mycol" by 1.
I was baffled when I found out that this code didn't have any effect.
UPDATE mytable SET mycol=(mycol+1)
The column value stays as it is. In my case "EMPTY" (=no value at all).
I would like to avoid re-creating the table if possible.
I would like to ask if there is any easy way to fix this in the SQL statement so that an EMPTY value is seen as 0 so that
UPDATE mytable SET mycol=(mycol+1)
on a column value of EMPTY would finally produce the new column value of 1.

You can use such as below if your column has null value:
UPDATE mytable SET mycol= ifnull(mycol,0)+1

Related

Issue with replacing NULL sqlite3 database column values with other types in Python 3?

I've run into a problem with the sqlite3 module in Python 3, where I can't seem to figure out how to replace NULL values from the database with other ones, mainly strings and integers.
This command doesn't do the job, but also raises no exceptions:
UPDATE table SET animal='cat' WHERE animal=NULL AND id=32
The database table column "animal" is of type TEXT and gets filled with NULLs where no other value has been specified.
The column "id" is primary keyed and thus features only unique integer row indices.
If the column "animal" is defined, not NULL, the above command works flawlessly.
I can replace existing strings, integers, and floats with it.
What am I overlooking here?
Thanks.
The NULL value in SQL is special, and to compare values against it you need to use the IS and IS NOT operators. So your query should be this:
UPDATE table
SET animal = 'cat'
WHERE animal IS NULL AND id = 32;
NULL by definition means "unknown" in SQL, and so comparing a column directly against it with = also produces an unknown result.

How to have a SQLite query with a variable part?

Hi I am trying to create a query for SQLite which has a variable part in it. By variable I mean that a certain part within the string can possibly contain a variable but also an empy value
I tried this but I am not sure whether this works.
SELECT * FROM table WHERE attr LIKE 'ABC% %DEF'
Adding onto my comment, check the below code to test your values.
SELECT CASE WHEN 'ABC G DEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END as test_space,
CASE WHEN 'ABCGGGDEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END AS test_all

SQL Server 2012 How to change the data type of a column from bit to datefield?

I have a table Person with a column called onvacation.
This column is of data type bit since it's a boolean in the code. It has values null, 0 and 1.
I would like to change the data type of this column from bit to datetime so that all values that are 1, are converted to a new date (could be current date). and 0 and null values would both be just null.
I tried following w3bschool's tutorial and did a query:
ALTER TABLE Person ALTER COLUMN onvacation datetime
But that gives an error 'DF____Person__onvac__59062A42' is dependent on column 'onvacation'.
you get this error because DF____Person__onvac__59062A42 sql object Depends on onvacation column.
You can Find Dependency of Person table by Right Click-->View Dependancy
remove that dependent object and try to alter column

Parametric recursive looped SQLite insert - do all columns have to be supplied?

I added a new column to my table, so there are now 4 instead of 3, and am now getting the following error when do a parametric insert (looped):
table 'test' has 4 columns but 3 values were supplied
Does this mean that you have to code your query for EVERY column the table has (as opposed to just the columns you want populated) when doing inserts, and that SQLite won't just add a default value if a column is missing from the query?
My query is:
"INSERT OR IGNORE INTO test VALUES (NULL, #col2, #col3)"
And this is the code that controls what's inserted in the recursive lopp:
sqlStatement.clearParameters();
var _currentRow:Object = _dataArray.shift();
sqlStatement.parameters["#col2"] = _currentRow.val2;
sqlStatement.parameters["#col3"] = _currentRow.val3;
sqlStatement.execute();
Ideally, I'd like column 4 to be left blank, without having to code it into the query.
Thanks for taking a look.
If you're inserting less values than there are columns, you need to explicitly specify the columns you are inserting to. For example
INSERT INTO test(firstcolumn,secondcolumn) VALUES(1,2);
Those columns that are not specified will get the default value, or NULL if there is no default value.

sqlite, UPDATE OR REPLACE

I do something like
UPDATE OR REPLACE someTable SET a=1, b=2 WHERE c=3
I expect if it doesnt exist it will be inserted into the DBs. But nothing happens and i get no errors. How can i insert data, replace it if it already exist and use a where for the condition (instead of replacing BC of a unique ID)
Careful, INSERT OR REPLACE doesn't have the expected behaviour of an "UPDATE OR REPLACE".
If you don't set the values for all fieds, INSERT OR REPLACE is going to replace them with default values, whereas with an UPDATE you expect to keep the old values.
See my answer here for an example: SQLite - UPSERT *not* INSERT or REPLACE
Try
INSERT OR REPLACE INTO [someTable] (a,b) VALUES(1,2) WHERE c = '3'

Resources