I have table which contains data along with created on date. I want to select data from table according to two given date ranges, or of one particular month.
I am not sure how to do that, as created on column is of type text and is stored as dd-MM-yyyy HH:mm:ss
I am using rawQuery() to fetch data.
You can do something like this:
mDb.query(MY_TABLE, null, DATE_COL + " BETWEEN ? AND ?", new String[] {
minDate + " 00:00:00", maxDate + " 23:59:59" }, null, null, null, null);
minDate and maxDate, which are date strings, make up your range. This query will get all rows from MY_TABLE which fall between this range.
Here is some usefull
//Get Trips Between dates
public List<ModelGps> gelAllTripsBetweenGivenDates(String dateOne, String dateTwo) {
List<ModelGps> gpses = new ArrayList<>();
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String columNames[] = {DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME, DBHelper.COLUMN_LATITUTE, DBHelper.COLUMN_LONGITUDE, DBHelper.COLUMN_ALTITUDE, DBHelper.COLUMN_DATE, DBHelper.COLUMN_TYPE, DBHelper.COLUMN_TRAVEL, DBHelper.COLUMN_SPEED};
String whereClause = DBHelper.COLUMN_TYPE + " = ? AND " + DBHelper.COLUMN_DATE + " BETWEEN " + dateOne + " AND " + dateTwo;
String[] whereArgs = {"Trip"};
Cursor cursor = database.query(DBHelper.TABLE_NAME_GPS, columNames, whereClause, whereArgs, null, null, DBHelper.COLUMN_NAME + " ASC");
while (cursor.moveToNext()) {
ModelGps gps = new ModelGps();
gps.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
gps.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)));
gps.setLatitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LATITUTE)));
gps.setLongitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LONGITUDE)));
gps.setAltitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_ALTITUDE)));
gps.setDate(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DATE)));
gps.setType(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TYPE)));
gps.setTravel(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TRAVEL)));
gps.setSpeed(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_SPEED)));
gpses.add(gps);
}
database.close();
cursor.close();
return gpses;
}
Related
I'm trying to learn how to use database in Android and so far I have learned how to fetch all rows of data in the table, but I'm not sure how I'm going to use a SQL query to search for a value that is equal to the string value I pass to the method below?
I wonder if someone could add some simple code how to query the database with SQL in my code? Preciate the help! Thanks!
Something like this: String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH = filePath";
// Read from database
public String readContacts(String filePath){
String[] columns = new String[]{TABLE_ID, TABLE_IMAGE_PATH, TABLE_CONTACT_NAME, TABLE_CONTACT_NUMBER, TABLE_CONTACT_ADDRESS};
Cursor c = db.query(DB_TABLE, columns, null, null, null, null, null);
int id = c.getColumnIndex(TABLE_ID);
int imagePath = c.getColumnIndex(TABLE_IMAGE_PATH);
int name = c.getColumnIndex(TABLE_CONTACT_NAME);
int number = c.getColumnIndex(TABLE_CONTACT_NUMBER);
int address = c.getColumnIndex(TABLE_CONTACT_ADDRESS);
String result = "";
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(id) + " " + c.getString(imagePath) + " " + c.getString(name) + " " + c.getString(number) + " " + c.getString(address);
}
return result;
}
You can search Query
its for exact string
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '"+filePath+"';
if value contains your string
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '%"+filePath+"%';
if value needs from last
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '%"+filePath+"';
if value needs from first
String query = "Select * from DB_TABLE where TABLE_IMAGE_PATH like '"+filePath+"%';
put these
SQLiteDatabase dbR = this.getReadableDatabase();
Cursor cur = dbR.rawQuery("query", null);
return cur
using this cursor you can get value.
you can do that easily by modifying your query code to:
String[] columns = new String[]{TABLE_ID, TABLE_IMAGE_PATH, TABLE_CONTACT_NAME, TABLE_CONTACT_NUMBER, TABLE_CONTACT_ADDRESS};
Cursor cursor = db.query(ANSWERS_TABLE,
columns,
columns[1] + " = ?", new String[]{filePath}, null, null, null);
Take a look here : SQLiteDataBase Query
I am currently trying to compare dates for a final year project. I want to be able to compare dates (from datepicker and calendarview) to see if they match and then only display the information for that given date.
I have tried:
String dateSelected = (dayOfMonth + "/" + (month + 1) + "/" + year);
Toast.makeText(getApplicationContext(), "TODAYS DATE" + dateSelected, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), Calms.KEY_FLARE_DATE, Toast.LENGTH_SHORT).show();
db = openOrCreateDatabase(Calms.DATABASE_NAME, MODE_PRIVATE, null);
Cursor c = null;
String dbDate = c.getString(c.getColumnIndex(Calms.KEY_FLARE_DATE));
Toast.makeText(getApplication(), dbDate, Toast.LENGTH_SHORT).show();
I am at a real loss here.
Any help would be greatly appreciated.
Thanks
You need to read some tutorial to find out how to query databases.
The following would give you all columns in all records of some table:
Cursor c = db.query("SomeTable", null,
null, null, null, null, null);
To get only the records with some specific date, add a where clause:
Cursor c = db.query("SomeTable", null,
KEY_FLARE_DATE + " = ?",
new String[] { dateSelected },
null, null, null);
I have a database of users
every user have a first name , last name and age
I want to order my data base using last name, than if two user have the same last name i want to organize the data base using first name and finally age
What do i need to add to this line?
Cursor cursor = mDatabase.query(DATABASE_ATTENDEES_TABLE, null, Attendee.DATABASE_KEY_USER_ID + " = ?", new String[] { Long.toString(user_id) }, null, null, Attendee.DATABASE_KEY_LASTNAME);
Thanks i get it:
Cursor cursor = mDatabase.query(
DATABASE_ATTENDEES_TABLE,
null,
Attendee.DATABASE_KEY_USER_ID + " = ?",
new String[] { Long.toString(userId) },
null,
null,
Attendee.DATABASE_KEY_LASTNAME+ " ASC," +
Attendee.DATABASE_KEY_FIRSTNAME + " ASC," +
Attendee.DATABASE_KEY_AGE
);
All the examples I see for doing a query show only using one key. Is it possible to use two keys. I have tried the code below and it does not work. I am trying to find the row where date and time match the date and time in the table
public String getPrimaryID(int date, int time )
{
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ "time=" + time, null, null, null, null);
int id=c.getColumnIndex(KEY_ROWID);
String result=c.getString(id);
return result;
}
I think you need to AND your query:
Cursor c= db.query(DB_TABLE, new String[]{KEY_ROWID}, "date="+date+ " AND time=" + time, null, null, null, null);
try this :
Cursor c = db.query(TABLENAME, null, "date = ? AND time = ?", new String[] { date, time }, null, null, null, null);
or :
String query ="SELECT * FROM " + TABLENAME + " " + "WHERE "
"date = ? " +
"AND time = ? ";
Cursor c = db.rawQuery(query, new String[] {date, time});
Explanation : variables date and time will replace the ? characters on the query
I have a SQlite database with some columns and with a content of an EditText I would like to select some rows from the database.
In main activity:
case R.id.betx:
String dates2 = sqletx.getText().toString();
GlobalVars.settables(dates2);
Intent intentstar2t = new Intent(dbhelp.this, CustomList.class);
startActivity(intentstar2t);
break;
This is my main activity, the 'dates2' String is the content of the edittext, and I save it for the setTables method.
My code in the hornot class where:
public Cursor getTable() {
String deda = GlobalVars.gettables();
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS,
KEY_CALORIE, KEY_MULTI, KEY_DATE};
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda ,
null, null, null, null);
}
Here I create a Cursor for the rows that fulfill the requirements in the last row. The String deda will be the 'dates2' we set before and I would like to get the rows where the KEY_DATE is equivalent to dates2.
The listview activity where I list the suitable rows.
if (todoItems.size() > 0) {
todoItems.clear();
}
if (list.size() > 0) {
list.clear();
}
Cursor c = info.getTable();
c = info.getTable();
if (c.moveToFirst())
{
do{
todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)+ " " + c.getString(3)+ " " + c.getString(4));
quanitems.add(c.getString(4));
}
while (c.moveToNext());
}
if (todoItems.size() > 0)
{
for (int i=0; i<todoItems.size(); i++)
{
each=new EachRow();
each.text=(String) todoItems.get(i);
list.add(each);
each2=new EachRow();
each2.text=(String) quanitems.get(i);
list2.add(each2);
}
listView.setAdapter(new MyAdapter(this, 0, list));
}
I use a custom listview, in every row there are some items, but the point is that I create a cursor (c) for the rows where the KEY_DATE is equivalent to dates2.
My problem is I never define integer variable here, but if I create an entry where the date is for example "107" or "6519684" it is working, but if it is "tableOne" or any text it crushes at the row:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=" + deda , null, null, null, null);
and
Cursor c = info.getTable();
(as you can see both problems are connected to the getTable method.
I use only Strings so practically the numbers ("107") are strings as well.
Any idea?
Thanks in advance!
use:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "=?" ,
new String[] { deda }, null, null, null);
Edit(explaination):
in query method you have String selection and String[] selectionArgs parameters, so in selection you can use ? for parameters in selectionArgs. Now underlying query builder will build query with this paramater and will apply proper escape chars.
I think that your query should be like this:
return ourDatabase.query(DATABASE_TABLE, columns, KEY_DATE + "= ?" ,
new String[] {deda}, null, null, null);