How to include a boolean in an SQLite WHERE clause - android

I get no results. How can I include a boolean conditional in a where clause in SQLite?
I have tried these
"Select * from table where col = 1"
"Select * from table where col = '1'"
"Select * from table where col = true"
"Select * from table where col = 'true'"
"Select * from table where col = 'True'"
"Select * from table where col is True"
Nothing. I even tried including "true" as the whereArgs in a query function.
How can I fix it?

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
Source: SQLite
The first one then sounds correct.

SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE "stack_test" ("id" INTEGER, "bool_col" boolean);
sqlite> insert into stack_test values(1,'t');
sqlite> insert into stack_test values(2,'f');
sqlite> insert into stack_test values(3,'1');
sqlite> insert into stack_test values(4,'0');
sqlite> insert into stack_test values(5,1);
sqlite> insert into stack_test values(6,0);
sqlite> insert into stack_test values(7,banana);
Error: no such column: banana
sqlite> insert into stack_test values(7,'banana');
sqlite> .headers on
sqlite> select * from stack_test;
id|bool_col
1|t
2|f
3|1
4|0
5|1
6|0
7|banana
sqlite> select * from stack_test where bool_col=t;
Error: no such column: t
sqlite> select * from stack_test where bool_col='t';
id|bool_col
1|t
sqlite> select * from stack_test where bool_col=0;
id|bool_col
4|0
6|0
sqlite> select * from stack_test where bool_col=1;
id|bool_col
3|1
5|1
sqlite> select * from stack_test where bool_col=true;
Error: no such column: true
sqlite> select * from stack_test where bool_col=banana;
Error: no such column: banana
sqlite> select * from stack_test where bool_col='banana';
id|bool_col
7|banana
sqlite>
sqlite> .schema stack_test
CREATE TABLE "stack_test" ("id" INTEGER, "bool_col" boolean);
sqlite>

This works well
"Select * from table where col = 'true'"

There is no real Boolean in SQLite. If you created it using SQLite Administrator, here is how you do it:
Select * from table where col is 'Y'

Related

Can you set a column equal to another column in a table using SQLiteDatabse update()?

I need to make a column equal to another column in the table. I can't figure it out using the update method of my SQLiteDatabase.
I know the SQL statement is:
UPDATE coolTable SET columnA = columnB;
Do I put it in the ContentValues I pass the function? or the selection string?
You can use update() only to set literal values (as bind params), not any other kind of expression supported by sqlite SQL syntax.
Use execSQL() to execute your raw UPDATE query with column name expression.
execSQL() with error checking:
SQLiteDatabase db = getReadableDatabase();
try {
long count = DatabaseUtils.queryNumEntries(db, "pweb");
db.execSQL("update pweb set h_id = _id;");
long rows = DatabaseUtils.longForQuery(db, "SELECT changes()", null);
if(count != rows ) Log.e("wrong count","update failed");
}
catch(SQLException e){
Log.e("SQLException","update failed");
}
db.close();
but I was wondering if it is possible to use the database's update()
function instead of execSQL()
You can use sqlite3 to view and manipulate you data, and test out sql commands.
Create a table:
CREATE TABLE pweb (_id INTEGER PRIMARY KEY,h_id INTEGER ,sent INTEGER);
See the original rows:
sqlite> select * from pweb;
1|10|1
2|20|1
3|30|0
4|40|0
execute a column update:
sqlite> update pweb set h_id = _id;
See the changes to all rows:
sqlite> select * from pweb;
1|1|1
2|2|1
3|3|0
4|4|0
Something more complex ?
sqlite> update pweb set h_id = _id + 1;
result:
sqlite> select * from pweb;
1|2|1
2|3|1
3|4|0
4|5|0
See also Understanding SQLITE DataBase in Android:

Moving all values from table1 to table2( with 1 new column)

Am copying alll values from Table1 to Table2.
Note : In Table2 i have added extra one column at last position.
Table1 has 16 columns
Table2 has 17 columns.
I used the following query :
String s = "INSERT INTO customer_profile_details SELECT * FROM customer_profile";
db.execSQL(s);
It throws the exception as :
03-11 06:34:46.383: E/Copy table(24750): Failed to copy table
android.database.sqlite.SQLiteException: table customer_profile_details has 17 columns but 16 values were supplied (code 1): , while compiling: INSERT INTO customer_profile_details SELECT * FROM customer_profile
How to solve this, i need to pass empty value to new column.
Thanks in advance !
Try this:
String s = "INSERT INTO customer_profile_details SELECT cp.*, null FROM customer_profile cp";
db.execSQL(s);
INSERT INTO customer_profile_details (COL1, COL2, COL3, COL4 ... CoL17)
SELECT COL1, COL2, COL3, ... Col16 , "constant valid default value for 17 column or null if permitted"
FROM customer_profile;
Also it's better to have a default value constraint for the column 17 in table structure itself , to better handle the miss-leading

SQLite and Android Insert/Updates on SQLiteDatabase -CompiledStatements-

Pretend I have a table with 2 columns. _id and name. _id is the primary key and I do not want to set this value manually. I want to perform an insert of name="john," and let the program create my own _id. I am unclear what "index" to use when inserting and how many question marks to use. Does this code do the job? Should the index for john be 1 or 2?
String TABLENAME = "table";
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" VALUES(?);");
statement.bindString(1,"john");
statement.executeInsert();
Next, say I want to manually set my own _id value. Would I change the code to:
String TABLENAME = "table";
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" VALUES(?,?);");
statement.bindLong(1,666); //Manual _id.
statement.bindString(2,"john");
statement.executeInsert();
Your first example where you provide only the name will not work:
sqlite> create table test (i integer primary key autoincrement, j text);
sqlite> insert into test values ('asd');
Error: table test has 2 columns but 1 values were supplied
sqlite> insert into test values (null, 'asd');
sqlite> select * from test;
1|asd
sqlite> insert into test (j) values ('asd');
sqlite> select * from test;
1|asd
2|asd
so you need to identify the name column as the destination of the sole value this way, (or as you mentioned in your comment pass null):
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" (name) VALUES(?);");
Your second example should work fine.
This would apply to some table created this way:
create table SomeTable (_id integer primary key autoincrement, name text)
Then
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" VALUES(null,?);");
statement.bindString(1,"john");
Should also work.

Increase a column value in sqlite without the method execSQL

I'd like to update one of my table's column with the following query:
update TABLE set COLUMN_NAME= COLUMN_NAME+1;
using if posible the
update(String table, ContentValues values, String whereClause, String[] whereArgs)
method in SQLiteDatabase class in android
Is that posible?
Thanks in advance
though this is a bit overdue, i'm just adding an example that works...
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table test1(a int primary key, b int);
sqlite> begin transaction;
sqlite> insert into test1(a,b) values (1,4);
sqlite> insert into test1(a,b) values (2,5);
sqlite> insert into test1(a,b) values (3,4);
sqlite> commit;
sqlite> select * from test1;
1|4
2|5
3|4
sqlite> update test1 set b=b+1;
sqlite> select * from test1;
1|5
2|6
3|5
sqlite> update test1 set b=b+10;
sqlite> select * from test1;
1|15
2|16
3|15
sqlite>
You may have some luck with execSQL. Here's what I did to solve a similar problem:
db.beginTransaction();
try {
//do update stuff, including execSQL()
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
See also https://stackoverflow.com/a/5575277/6027

SQLite script vars

I need to mass populate my SQLite database — ideally using a script rather than code.
I would like to do this (MySQL syntax) but for SQLite but I'm not sure it you can have variables defined in scripts:
INSERT INTO `parent` (id, name) values(NULL, "some name!");
SET #parentId= last_insert_rowid();
INSERT INTO `child` (id, parentId, name, ) values (NULL, #parentId, 'some name!);
SQLite throws errors when I try to declare variables in my SQLite script. Can this be done in SQLite?
You can use the function last_insert_rowid() without a script var for this case:
insert into parent (id, name) values (NULL, 'some name!');
then:
insert into child (id, parentId, name) values (NULL, last_insert_rowid(), 'child name!');
transcript:
SQLite version 3.7.6.3
sqlite> create table parent (id integer primary key, name);
sqlite> create table child (id integer primary key, parentId integer, name);
sqlite> insert into parent (id, name) values (NULL, 'some name!');
sqlite> insert into child (id, parentId, name) values (NULL, last_insert_rowid(), 'child name!');
sqlite> select * from parent;
1|some name!
sqlite> select * from child;
1|1|child name!
sqlite>
If you need to keep the value around for a while (through multiple inserts for example) use a temporary table:
sqlite> create temp table stash (id integer primary key, parentId integer);
sqlite> insert into parent (id, name) values (NULL, 'another name!');
sqlite> replace into stash values (1, last_insert_rowid());
sqlite> insert into child (id, parentId, name) values (NULL, (select parentID from stash where id = 1), 'also a name!');
sqlite> select * from parent;
1|some name!
2|another name!
sqlite> select * from child;
1|1|child name!
2|2|also a name!
sqlite>
Unfortunately you can't declare such variable in SQLite script. Moreover AFAIK all statements will not be executed except the first one. Also look HERE

Categories

Resources