sqlite exception error SELECT command - android

Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT _id, FROM TRACKS WERE _id=9
String test = "SELECT _id, FROM TRACKS WERE _id="+"9";
Cursor cursor = database.rawQuery(test, null);
Did not see the Point :( any help
Maybe there is a error in my Statement but it didn't work

No comma after id and WERE should be WHERE.
Also consider using [rawQuery()](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)] with selectionArgs to avoid having to concatenate query and values as you did in +id=+9.

SELECT _id, FROM TRACKS WERE _id="+"9"
'WERE' should be WHERE

Related

"WITH" clause Query not working in android

this is my rawQuery
WITH authorRating(aname, rating) AS SELECT aname, AVG(quantity) FROM book GROUP BY aname
error is in Logcate
android.database.sqlite.SQLiteException: near "WITH": syntax error (code 1):
WITH is supported since SQLite 3.8.3, and that is not shipped with all Android versions.
Anyway, this query is not a valid WITH clause, and lacks the actual query.

Why can't I perform a simple count in a subquery?

I'm trying to write a simple query that fetches all the columns of a specific table, but also adds a count of rows from a related table.
Here is the query:
SELECT
todos.*,
(SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count
FROM todos
Here is the error:
Caused by: android.database.sqlite.SQLiteException: near "*": syntax error (code 1): , while compiling: SELECT todos.*, (SELECT COUNT(assets.*) FROM assets WHERE assets.parent_id = todos._id) AS asset_count FROM todos
try replace COUNT(assets.*) for COUNT(*) or COUNT(assets._id)
my second suggestion, try running the inside select like a single one
SELECT COUNT(*) FROM assets WHERE assets.parent_id = 1 <-- any _id you have

Android DB Update near "WHERE": syntax error

I know there are already too many questions about this, and believe me I've done my part reading the answers to find a solution. Unfortunately, I can't find my mistake
Here's my code:
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("eat_it", 1);
values.put("expired", 1);
String[] args = {food.name, "0"};
db.update("foods", values, "name=? AND expired=?", args);
db.close();
What I want to do:
Find a record in the table foods WHERE the value of column name = food.name and the value of column expired = 0. If found, SET the record's eat_it column to 1 and expired column to 1. If no record found, don't do anything
Here's my create table syntax:
CREATE TABLE IF NOT EXISTS foods (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
...
eat_it INTEGER DEFAULT 0,
expired INTEGER DEFAULT 0
);
Here's the error message:
...
Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: UPDATE foods SET expired=?,eat_it=? WHERE name ='Taco Salad'WHERE expired = 0
...
Thanks for your help
====UPDATE====
Apparently after I restart my computer it worked. I think my emulator was not working properly/ still has the old code. Thanks everyone for helping
Error message returned per your post
near "WHERE": syntax error (code 1): , while compiling: UPDATE foods
SET expired=?,eat_it=? WHERE name ='Taco Salad'WHERE expired = 0
It's clear from the error statement that you have two WHERE clause in your query and so the error.
UPDATE foods SET expired=?,eat_it=?
WHERE name ='Taco Salad'
WHERE expired = 0 <-- Here
Your UPDATE statement should look like
UPDATE foods SET expired=?,eat_it=?
WHERE name ='Taco Salad'
AND expired = 0
You can use the where statement without using the 'whereArgs' like this:
db.update("foods", values, "name="+food.name+" AND expired=0", null);
Also, since you're updating internal values, you can also try a simple query:
db.rawquery("UPDATE foods SET eat_it=1, expired=1 WHERE name="+food.name+" AND expired=0");
Although there should be nothing wrong with what you wrote there, you can try those two options to make it work.

getting sqLiteException when useing getContentResolver().query()

I'm trying to get data from sqlite database.Tried using thiscontext.getContentResolver().query() method and I got following SqlException
near ":23": syntax error (code 1): , while compiling: SELECT wol_wait, mac_addr, _id, address, name, wol_port, user, timeout, pass FROM hosts WHERE (mac_addr=00:23:15:97:ce:a0) ORDER BY name ASC
this is the code that I’m using
Cursor c=context.getContentResolver().query(HostProvider.Hosts.CONTENT_URI,null, Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null,HostProvider.Hosts.NAME + " ASC");
how can I fix this issue?
String literals such as MAC addresses need to be in 'single quotes', or better yet, use ? placeholders and variable binding. Replace
Hosts.MAC_ADDR +"=" +String.valueOf(mhost.mac_addr),null
with
Hosts.MAC_ADDR +"=?", new String[] { String.valueOf(mhost.mac_addr) }
The String.valueOf is possibly not needed.

SQLite exception in LIKE query

I've got search query "aa bb" and also there is "aa bb cc" row in table. So my sql query should return that row. This is how I do:
select * from company
where
(ASCII_NAME like %'aa'%
and ASCII_NAME like %'bb'%)
and CITY_ID=0 and parent=-1;
But my log says:
android.database.sqlite.SQLiteException: near "%": syntax error (code
1): , while compiling: select * from company where (ASCII_NAME like
%'aa'% and ASCII_NAME like %'bb'%) and
CITY_ID=0 and parent=-1
What is wrong?
Your syntax is wrong
select * from company
where
(ASCII_NAME like '%aa%'
and ASCII_NAME like '%bb%')
and CITY_ID=0 and parent=-1;
Include % sign in single quotes.
Like:
select emp_name from employee where emp_name like '%s%';

Categories

Resources