简体   繁体   中英

Using Like statement in SQLITE on android studio

I have a SQLite database containing informations about cars. I want to sellect cars that are branded BMW. I have this statement:

String selectQuery = "SELECT  * FROM " +Note.TABLE_NAME+ " where " + "BRAND" + " like "+ "BMW";

The app crashes and logs shows:

no such column: BMW (code 1): , while compiling: SELECT  * FROM adds where BRAND like BMW

When i am using this one the app works:

 String selectQuery = "SELECT  * FROM " +Note.TABLE_NAME+ " where " + "AGE" + " like "+ "10";

How do i use Like with a string after that?

You need to surround the value that you are retrieving with single quotes.

Try this instead:

String selectQuery = "SELECT * FROM " +Note.TABLE_NAME+ " where " + "BRAND" + " like 'BMW'";

Alternatively, you don't need to put each word of the query in their own quotes. You could just use:

String selectQuery = "SELECT * FROM " +Note.TABLE_NAME+ " where BRAND like 'BMW'";

For the statement:

SELECT  * FROM tablename where BRAND like BMW

SQLite considers BMW as a column name (which of course does not exist) so the code fails.
It should be:

SELECT  * FROM tablename where BRAND like 'BMW'

But for the statement:

SELECT  * FROM tablename where AGE like 10

SQLite does not consider 10 as a column name, because it is a literal value which is implicitly converted to TEXT : '10' and the code works without problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM