Query i added :
String query= "SELECT * FROM " + GURBANI_TABLE+" where "+TYPE+"='"+type+
"' order by "+FILE_NAME;
Output returned:
Kirtan (Vol 1).mp3
Kirtan (Vol 10).mp3
Kirtan (Vol 11).mp3
...
But needed:
Kirtan (Vol 1).mp3
Kirtan (Vol 2).mp3
Kirtan (Vol 3).mp3
...
Required to be sorted by Vol numbering. But not by alphabetical order.
You can take the substring of the column that starts after '(Vol ' and add 0 so that it is converted to an integer and sort by that:
String query= "SELECT * FROM " + GURBANI_TABLE + " WHERE " + TYPE + " = ' " + type +
"' ORDER BY " +
"SUBSTR(" + FILE_NAME + ", 1, INSTR(" + FILE_NAME + ", '(Vol '))," +
"SUBSTR(" + FILE_NAME + ", INSTR(" + FILE_NAME + ", '(Vol ') + LENGTH('(Vol ')) + 0";
But it would be better to write the query with ? placeholders instead of concatenating variables:
String query= "SELECT * FROM " + GURBANI_TABLE + " WHERE " + TYPE + " = ? " +
"ORDER BY " +
"SUBSTR(" + FILE_NAME + ", 1, INSTR(" + FILE_NAME + ", '(Vol '))," +
"SUBSTR(" + FILE_NAME + ", INSTR(" + FILE_NAME + ", '(Vol ') + LENGTH('(Vol ')) + 0";
Cursor c = db.rawQuery(query, new String[] {type})
See a simplified demo in SQLite.
i have small problem, i googled it many times but i couldn't get it...
i'm using a query (sqlite) to retrieve some data ..
in this query their is Date() used to increase certain date dynamic number of days (coming from column in the same table) ..
if i put this days static (1,2,3,.....) it works fine but if i put column name it doesn't.
this query fails and i want it to work:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";
this works fine :
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "','2 day') ";
where
targetDate: date entered by user to get events of that date
DATE: date of every event in the table
REPETETIONS: dynamic number (column in the same table)
the problem is using REPETITIONS in the date function...
create statement
String SQL_CREATE_EVENT_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TITLE + " TEXT ," +
DATE + " TEXT , " +
IS_NOTIFY + " INTEGER , " +
NOTIFICATION_TIME + " TEXT ," +
REPEAT + " INTEGER ," +
REPEAT_DURATION + " INTEGER ," +
REPETITIONS + " INTEGER ," +
CERTAIN_DATE + " TEXT ," +
NOTE + " TEXT ," +
IS_SPOKEN + " INTEGER " +
" );";
and this is the selecting statement
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE " + DATE + " = '" + targetDate
+ "' OR (( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '0' ) AND " + DATE + " <= '" + targetDate + "')"
+ " OR( " + REPEAT + " = '1' AND " + REPEAT_DURATION + " = '3' ) AND " + DATE + " <= '" + targetDate + "' AND " + CERTAIN_DATE + " >= '" + targetDate + "'"
+ " OR (" + REPEAT + " = '1' AND " + REPEAT_DURATION + "= '2' ) AND " + targetDate + " >= '" + DATE + "' AND "+ targetDate+ " <= date('" + DATE + "','" + REPETITIONS + " days')";
The variable REPETITIONS in this statement:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "',' " + REPETITIONS + " day') ";
should be a number and not a column name.
The date function in SQLite has various syntaxes but you use this one:
SELECT date('2014-10-23','+7 day');
you must supply a number before day.
Edit try this:
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME + " WHERE "
+ DATE + " = date('" + targetDate + "', " + REPETITIONS + " || ' day') ";
In sqlite date() function, the NNN days is a string modifier and not an expression. Therefore you cannot use column names or even || string concatenation there.
You can format the modifier string in your code and place it in the SQL. This requires additional query to the database, which is likely not what you want.
If you can assume each day is 24 hours (which is not always true, consider e.g. DST events), you could try something like
... date(strftime('%s', '" + targetDate + "', " + REPETITIONS + "*24*60*60, 'unixepoch') ...
where strftime('%s', ...) converts to seconds and date(..., 'unixepoch') converts back to datestamp.
But seriously I'd consider redesigning the schema to better support your functional requirements.
My database has "diary" table and "food" table. In diary table I have following columns: date, food1,food2, ...food10. In food table I have: id, name, allergen1, allergen2, allergen3. So basically you log up to 10 foods a day to diary. Now I'm trying to build a query which would let me find last date that I've eaten food with allergen3. So that's the query I came up with, which you probably know is not working ;-)
Cursor findDateRes = db.rawQuery("SELECT date(" + DIARY_COL_DATE + ") FROM " + DIARY_TABLE
+ " INNER JOIN " + FOOD_TABLE + " food1 ON (food1." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F1 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food2 ON (food2." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F2 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food3 ON (food3." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F3 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food4 ON (food4." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F4 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food5 ON (food5." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F5 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food6 ON (food6." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F6 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food7 ON (food7." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F7 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food8 ON (food8." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F8 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food9 ON (food9." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F9 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food10 ON (food10." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F10 +
") WHERE 1 NOT IN (food1." + FOOD_COL_5 +
",food2." + FOOD_COL_5 +
",food3." + FOOD_COL_5 +
",food4." + FOOD_COL_5 +
",food5." + FOOD_COL_5 +
",food6." + FOOD_COL_5 +
",food7." + FOOD_COL_5 +
",food8." + FOOD_COL_5 +
",food9." + FOOD_COL_5 +
",food10." + FOOD_COL_5 +
") ORDER BY date(" + DIARY_COL_DATE + ") ASC LIMIT 1", null);
"FOOD_COL_1" stands for id and "FOOD_COL_5" stands fro allergen3.
Thank you for looking. Any input will be appreciated.
Could you please help what wrong with this query when calling from Java. When I run the same query in PL/SQL developer it runs perfectly but not when i call from java,.
CRE_DTTM is in DATE datatype
String query4="SELECT NVL(SUM(cur_amt),0) FIRST_PAY" +
" FROM ci_ft ft " +
"WHERE sa_id IN "+
" (SELECT sA_id FROM ci_Sa WHERE acct_id='"+acctid.getIdValue()+"' )" +
"AND TRUNC(crE_dttm)>= (SELECT MAX(crE_Dttm) FROM ci_bill" +
" WHERE accT_id='"+acctid.getIdValue()+"' AND crE_dttm<= (SELECT add_months(To_Date('2016-03-10','YYYY-MM-DD'), -1) FROM dual ) )" +
"AND fT_type_flg IN ('PS','PX')AND ft.cRE_dttm <= (" +
" CASE" +
" WHEN ( to_Date(" +
" (SELECT MAX(crE_Dttm)" +
" FROM ci_bill" +
" WHERE accT_id ='"+acctid.getIdValue()+"'" +
" AND crE_dttm < To_Date('2016-03-10','YYYY-MM-DD')" +
" ) , 'YYYY-MM-DD') = to_Date (" +
" (SELECT MAX(crE_Dttm)" +
" FROM ci_bill" +
" WHERE accT_id='"+acctid.getIdValue()+"'" +
" AND crE_dttm<=" +
" (SELECT add_months(To_Date('2016-03-10','YYYY-MM-DD'), -1) FROM dual" +
" ) ) ,'YYYY-MM-DD'))" +
" THEN To_Date('2016-03-10','YYYY-MM-DD')" +
" ELSE" +
" ( SELECT MAX(crE_Dttm)" +
" FROM ci_bill" +
" WHERE accT_id='"+acctid.getIdValue()+"'" +
" AND crE_dttm < To_Date('2016-03-10','YYYY-MM-DD')) " +
" END)";
com.splwg.base.api.sql.PreparedStatement ps4 = createPreparedStatement(query4);
logger.info(ps4);
ps4.execute();
if(!ps4.list().isEmpty())
{
first_pay=new BigDecimal(ps4.list().get(0).get("FIRST_PAY").toString());
logger.info("first_pay=="+first_pay);
}
}
The error is:
ORA-01830: date format picture ends before converting entire input string site:community.oracle.com
Please note the PreparedStatement is from com.splwg.base.api.sql.PreparedStatement
I think you are passing different date formats in your where clause. Please try matching the date format of crE_dttm with To_Date('2016-03-10','YYYY-MM-DD')
e.g. to_date(crE_dttm, 'YYYY-MM-DD') < To_Date('2016-03-10','YYYY-MM-DD')
Thanks
Sabiha
Im trying to populate a listview with data from 3 tables using join, and one table to check wether a user is part of a group. But im having trouble getting all the groups for a given user. Heres the tables
Group table
_id | group_name | group_description | group_image
User table
_id | user_name | user_image
Group participants table (this table determines which users are part of which groups)
_id | user_id_foreign | group_id_foreign
Activityobject table
_id | target_id | user_id_foreign | target_type | time | type
Here is the sql statement im trying to get to work:
Cursor c = database
.rawQuery(
"SELECT groups.*, activityobject.time, activityobject.type, user.user_name, user.user_image "
+ "FROM activityobject "
+ "JOIN user "
+ "ON user._id = activityobject.user_id "
+ "JOIN groups"
+ "ON groups._id = activityobject.target_id "
+ "WHERE groups._id IN(SELECT group_id_foreign FROM group_participants WHERE user_id_foreign = ?)"
+ "AND activityobject.target_type = 0 "
+ "GROUP BY groups._id "
+ "ORDER BY activityobject._id",
new String[] { String.valueOf(userId) });
The result i get right now are all the groups that has an activityobject, but not the ones without.
I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell.
Thanks in advance!
EDIT:
An image of the tables im refering to
Heres my insert statements with data in the tables
//Activity object table
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (1, 2, 1, 1, 'Alex gruppe', 0, '07-05-13', 1)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (2, 2, 1, 2, 'Jeremys gruppe', 0, '08-06-13', 2)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (3, 2, 1, 2, 'Jeremys gruppe', 1, '09-07-13',3)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (4, 2, 1, 2, 'Jeremys gruppe', 0, '10-08-13', 4)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (5, 2, 1, 1, 'Alex gruppe', 0, '11-09-13', 5)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (6, 2, 1, 1, 'Alex gruppe', 0, '12-10-13', 6)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (7, 2, 1, 1, 'Alex gruppe', 1, '13-11-13', 7)");
db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
+ KEY_ACTIVITYOBJECT_ID + ", "
+ KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
+ KEY_ACTIVITYOBJECT_USER_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_ID + ", "
+ KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
+ KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
+ KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
+ ") VALUES (8, 2, 1, 1, 'Alex gruppe', 0, '14-12-13', 8)");
// Group table
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (1,'alex gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (2,'jeremy gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (3,'ole gruppe', 'mega awesome gruppe', null);");
db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
+ KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
+ KEY_GROUP_IMAGE
+ ") VALUES (4,'egon gruppe', 'mega awesome gruppe', null);");
// User table
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (1, 'Alex', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (2, 'Peter', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (3, 'Jeremy', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (4, 'Søren', null, 0)");
db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
+ KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
+ KEY_USER_STATUS + ") VALUES (5, 'Ole', null, 0)");
// Group participants table
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(1, 1, 1)");
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(2, 1, 4)");
db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
+ KEY_GROUP_PARTICIPANTS_ID + ", "
+ KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
+ KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(3, 2, 2)");
"I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell."
You have to use a LEFT JOIN then.
select g.*, a.time, a.type, u.user_name, u.user_image
from groups g
join group_participants gp on gp.group_id_foreign = g._id
left join activityobject a on g._id = a.target_id and a.target_type = 0
left join user u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;
If you just want to select the last activity object per group (if any), you can add that to the join condition, too:
select g.*, a.*
from groups g
join group_participants gp on gp.group_id_foreign = g._id
left join activityobject a
on g._id = a.target_id and
a.target_type = 0 and
a._id in (select max(_id) from activityobject group by target_id)
left join users u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;
Just keep in mind that you have to add all filtering criteria for activityobjects to the join condition of the left join, not to the where clause of your main query. Otherwise, you will surpress rows from the result where no matching activityobject can be found, instead of filling up the columns with nulls.