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

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

Related

SQLITE syntax error code 1 when renaming a column name

I am migrating a Room database in my Android app. This is the migration code:
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
#Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE item RENAME itemInfoId TO itemId");
}
};
The error message
android.database.sqlite.SQLiteException: near "itemInfoId": syntax error (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE item RENAME itemInfoId TO itemId
I have also tried the SQL of "ALTER TABLE item RENAME COLUMN itemInfoId TO itemId", same error
android.database.sqlite.SQLiteException: near "COLUMN": syntax error (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE item RENAME COLUMN itemInfoId TO itemId
Rename keyword is available in SQLite version 3.25 which is not available for the latest android version. You will have to manually upgrade the table
1. Create item_tmp table with correct column value itemId
CREATE TABLE item_tmp(<column1> <data_type>, itemId <data_type>,.....)
2. Copy the data from item to item_tmp
INSERT INTO item_tmp(<column1>, <column2>,..)
SELECT <column1>, <column1>, ...
FROM item;
3. Drop table item
DROP TABLE item;
4. Rename the item_tmp table
ALTER TABLE item_tmp RENAME TO item;
i have faced same problem while using RENAME keyword in Sqlite.
it gives error in below android 10 device and working perfectly in android 11 and above.

sqlite exception error SELECT command

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

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%';

Android Sqlite: android.database.sqlite.SQLiteException: aggregate functions are not allowed in the GROUP BY clause

Having a weird issue here where a query executes just fine in Sqlite DB Browser on my desktop - results exactly as expected - but throws an exception when executing a Cursor in Android (4.4.2). I'm new to both Android and Sqlite so maybe there's a "gotcha" I haven't discovered yet.
Simple query:
select
max(_id) as '_id',
max(envid) as 'envid',
max(action) as 'action',
max(title) as 'title',
max(cast(version as INTEGER)) as 'version',
max(layout) as 'layout'
from template
where direction = 'return'
group by title
Exception:
E/AndroidRuntime(20731): Caused by:
android.database.sqlite.SQLiteException: aggregate functions are not
allowed in the GROUP BY clause (code 1): , while compiling: select
max(_id) as '_id', max(envid) as 'envid', max(action) as 'action',
max(title) as 'title', max(cast(version as INTEGER)) as 'version',
max(layout) as 'layout' from template where direction = 'return' group
by title
I'd expect to see that if I put an aggregate function in the group by clause...but I haven't.
This fixed it - which is all I really needed (see comment from Rohit5k2 above.)
select _id, envid, action, title, max(cast(version as INTEGER)) as 'version', layout from template where direction = 'return' group by title

android.database.sqlite.SQLiteException: no such column: ۱۵۳۶۱۹۸۶۸۴۰۰۸۵۴۲۵۴۱

all.
I've found in bugsense for Android application this exeception:
android.database.sqlite.SQLiteException: no such column: ۱۵۳۶۱۹۸۶۸۴۰۰۸۵۴۲۵۴۱ (code 1): , while compiling:
SELECT * FROM by_istin_android_xcore_source_DataSourceRequestEntity WHERE (_id = -۱۵۳۶۱۹۸۶۸۴۰۰
Seems like try to select with filter by arabic number.
Someone see it before?
If _id is a numeric column, you must change your code to generate numbers with ASCII digits.
If _id contains strings, you must change your code to 'quote' the string.

Categories

Resources