I am trying and searching for a while now without success to remove specific rows in my db table.
I'd like to do this as a result in comparison between DATE record value and nextday value.
I thought this would do the work
String sql = "DELETE FROM Planned_expenses_table WHERE DATE <= date('now','+1 day')";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql);
db.close();
But it did not.
Any suggestion? I am currently storing column DATE values with the pattern "YYYY-MM-DD" which is the same returned by date().
Logcat doesn't show any exception.
So what is wrong with this piece of code? Thank you in advance.
EDIT : i've just noticed that if i change <= in > the rows are deleted, but this is very strange since saved dates are older
I managed to achieve the hoped result.
Apparently i was saving the dates in the form YYYY-M-D instead of the one written in the question. Thats why the order check <= was misbehaving.
Why not calculate the date then pass in the actual value.
var tomorrow = DateTime.Now + TimeSpan.FromDays(1);
string sql = $"DELETE FROM Planned_expenses_table WHERE DATE <= '{tomorrow.ToString("yyyy/MM/dd")}')";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql);
db.close();
Related
I have a query with a condition but it is returning an empty result even though I have the data that matches these conditions. I am a beginner, so I'm not sure if this query is correct.
public Cursor raw() {
SQLiteDatabase db = this.getReadableDatabase();
String select = "SELECT * FROM table_person WHERE data >= " + SelecionarPeriodo.dataInicial + " AND " + "data <=" + SelecionarPeriodo.dataFinal;
Cursor res = db.rawQuery( select, new String[]{});
Log.i("String", select);
return res;
}
If I remove this condition and use a query as follows
SELECT * FROM table_person
I have the results and the columns corresponding to the dates I'm trying to get.
The way you have written your query you are actually comparing the column data with 0, because a value like 14/12/2020 is considered as an expression containing divisions between the numbers 14, 12 and 2020 which has 0 as result.
If you want to compare dates in SQLite you must use a comparable format like YYYY-MM-DD, which is the only valid text date format for SQLite.
So if you stored the dates in the column data in any other format you'd better change it to YYYY-MM-DD.
You must also use the same format for the parameters SelecionarPeriodo.dataInicial and SelecionarPeriodo.dataFinal which will be compared to data.
Then use the recommended and safe way to pass the parameters in the 2nd argument of rawQuery():
public Cursor raw() {
SQLiteDatabase db = this.getReadableDatabase();
String select = "SELECT * FROM table_person WHERE data >= ? AND data <= ?" + ;
Cursor res = db.rawQuery(select, new String[]{SelecionarPeriodo.dataInicial, SelecionarPeriodo.dataFinal});
Log.i("String", select);
return res;
}
I am having trouble sort date database at android studio..
i have try at SQlite : SELECT * FROM tableName WHERE date BETWEEN "2017-07-01" AND "2017-07-31";
it's work at SQLite, i have a problem at android studio, i have try make the selectionArgs= {BETWEEN "2017-07-01" AND "2017-07-31"}
but it didnt work.
What is the input for that SQLite statement at android studio and i use cursor loader..
String selection = ?
String [] selectionArgs= ?
and also how to sort by ASC?
I believe you are looking for
String selection = "date BETWEEN ? AND ?";
String [] selectionArgs = new String[] { "2017-07-01", "2017-07-31" };
And you should be able to order the result using the version of SQLiteDatabase::query that takes an orderBy parameter.
Let the database sort the results.
Try this:
SELECT * FROM tableName WHERE date BETWEEN "2017-07-01" AND "2017-07-31"
order by date;
I have a unix date in long format
Long tmpLong = (Long) local.getValue();
Here is the value of tmpLong
1485710457166
Now I want to store this variable inside of my SQLite DB, after some research I found out that you cant store Longs in SQLite so I tried this:
Long tmpLong = (Long) local.getValue();
Integer tmpInt = tmpLong.intValue();
But this gives me:
-348227250
In SQLite my datatype is INTEGER
I need help storing this unix number, any help is appreciated
Update 1
If I try store it as a long, so
localDB.add(image_id, unixTimestamp, PATH);
And then in my add method (inside SQLiteHelper class). I print the value for testing :
public void add(String image_id, Long date, String path) {
SQLiteDatabase db = this.getWritableDatabase();
Log.d("SQL", "date " + date);
It prints 1
D/SQL: date 1
Well, that is what will happen when you truncate a 64 bit Long into a 32 bits integer...
Dont do that but try to store it like it is (as a long) because SQLite ints can handle that values, take a look into the Datatypes In SQLite
Edit:
Your code below:
public void add(String image_id, Long date, String path) {
SQLiteDatabase db = this.getWritableDatabase();
Log.d("SQL", "date " + date);
}
if that code is so as it is posted then you are passing as parameter 1 for the date value... the method this.getWritableDatabase(); is not going to modify/change the value of the date parameter...
It's been long fight between mysql and sql lite date issue. I have a complex query which returns multiple data.
I read server Json objects and store them in sql lite android was fine.
So i did a query which is like below one
"SELECT * FROM " + MAIN_TABLE + " LEFT OUTER JOIN " + SERVICE_INFO_TABLE + " ON ServiceInfo.main_id = MAIN_TABLE._ID"
+ " LEFT OUTER JOIN " + M_USER_TABLE + " ON M._ID = ServiceInfo.m_id"
+ " LEFT OUTER JOIN " + U_TABLE + " ON MAIN_TABLE.u_id = U_TABLE._ID"
The problem starts when i read the MAIN_TABLE which contains creation_time and upation_time, The SERVICE_INFO_TABLE also contains creation_time, updation_time.
However the table contains different date and time respectively. The cursor returns same date and time for all entity.
It seems the cursor method got confused and return same data and time read from ServiceInfo table.
I dig deeper into debugging mode while SQL lite read the query, i saw strange things happening over there. Below is the reading example
Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
Log.d("dummy", "dummy stopper");
VParcelable vParcelable = new VParcelable();
vParcelable.setId(cursor.getInt(cursor.getColumnIndex(VEntry._ID)));
vParcelable.setModel(cursor.getString(cursor.getColumnIndex(VEntry.MODEL)));
vParcelable.setYearMdl(cursor.getInt(cursor.getColumnIndex(VEntry.YEAR_MODEL)));
vParcelable.setCreationTime(cursor.getString(cursor.getColumnIndex(VEntry.CRE_TIME)));
vParcelable.setUpdationTime(cursor.getString(cursor.getColumnIndex(VEntry.UPA_TIME)));
So the ServiceInfo table also contains the same creation_time and updation_time.
vParcelable.setCreationTime(cursor.getString(cursor.getColumnIndex(VEntry.CRE_TIME)));
mColumns from cursor debugging mode shows "updation_time" and "creation_time" for VTable and same as for ServiceInfo table.
Is that problem of sql lite couldn't differentiate table column names?
That really sucks.
Help please.
Thanks
Your date string '15-04-2015' is in a DD-MM-YYYY format but it appears it is being treated as MM-DD-YYYY at some point. You aren't showing ANY code in your question, so I can't tell you exactly how you need to solve this. In general you need to specify to the database the date format you are using, or switch to storing it as a string or integer in the database. You might look at some of the answers to this question: Best way to work with dates in Android SQLite
String x=jTextField1.getText();
After connecting to the database the query is:
String query="INSERT INTO student(A) VALUES('"+a+"') where date=' " +x+ " ';";
stmt.executeUpdate(query);
*a is a string which has a letter P assigned to it.
The error i am getting is "....check your mysql syntax....corresponding to the date='"+x'"; "
I want to compare the date entered in the textfield to the date in the mysql 'date' column and if it is correct,the 'a' value (which is P) should be written in column A in the same row of the date entered...
Please help...
Thank you...
I see a space after/before the single quote.
Furthermore date is also an SQL keyword, so better not use that as field name. You could write
`date`
Addition
Sorry, I realized that I erred (date cannot be a field queried as we are inserting a new record).
Either you mean:
String query = "INSERT INTO student(A) VALUES('P') WHERE CURRENT_DATE() = '2012-05-09'";
Or date is a field, and you just want to set another field:
String query = "UPDATE student SET A = 'P' WHERE `date` = '2012-05-09'";
Inserting new records into same table
This is not allowed to do immediately, so one has to use a temporary table.
CREATE TEMPORARY TABLE tmp (A VARCHAR(1));
INSERT INTO tmp (A)
SELECT 'P' FROM student WHERE dt = '...';
INSERT INTO student(A)
SELECT A FROM tmp;
DROP TABLE tmp;