Is this way of fetching data from sqlite correct? - android

I am using this query to fetch data from sqlite but only one condition is working but not two at same time.
I am also getting some notification that no such column but it exist.
String lquery = "SELECT SUM(totalcalorie) AS LTotal FROM fooditem
WHERE name = ('" + fname + "') AND foodtype = 'Lunch'";

you might be having null values on those places which you are checking while calling..
go through your code again queries seems write.

change totalcalorie to INTEGER type in the database schema
comment says that you are using TEXT

Related

How to fix empty string while fetching it from Database?

I am fetching a string from Database using the column id. When I enter a query in SQLite DB Browser it returns what is need but the same query returns nothing when coded through Java.
My Data Base contains a table named drugs which has 3 columns i.e. drug_id, drug_name and drug_overview. Using drug_id i am fetching drug_overview. I have tried the query in db browser which returns me the correct string from drug_overview but the same query returns nothing when coded through java.
SQLite DB Browser query:
SELECT * FROM drugs Where drug_id = 50;
JAVA CODE:
String query105 = "SELECT * FROM drugs Where drug_id = " + drug_id;
Log.e("TESTDB1","Drugs table query: " + query105);
Cursor c105 = db.rawQuery(query105,null);
if (c105 != null){
while (c105.moveToNext()){
String overview = c105.getString(c105.getColumnIndexOrThrow("drug_overview"));
Log.e("TESTDB1","Overview: " + overview);
}
c105.close();
}
Expected result is Overview: Acyclovir is an antiviral drug. It slows the growth and spread of the herpes virus in the body. It will not cure herpes, but it can lessen the symptoms of the infection.Acyclovir is used to treat infections caused by herpes viruses, such as genital herpes, cold sores, shingles, and chicken pox, as well as varicella (chickenpox), and cytomegalovirus.Acyclovir may also be used for purposes not listed in this medication guide.
But the actual result is Overview:
empty
. When i change the id in my query it gives the correct result from a different drug.
I am afraid that your problem may be with the actual data itself as Mike said in comment, I think your database in the files is old and you haven't copied the latest to folder. Try to re-install and delete old database
Your query returns 0 or 1 lines so I think you should use c105.moveToFirst() instead of c105.moveToNext(). moveToNext is supposed to be used for a list, not for a single entry. Do something like:
if (c105.moveToFirst()){
String overview = c105.getString(c105.getColumnIndex("drug_overview"));
// do something with the result
}
c105.close();

value of FeedEntry.COLUMN_NAME_UPDATED while creating a sql database

I am creating a sql database in my app and I am following the documentation on the official developer guide of android, at the webpage
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow.
I don't understand what is the meaning of the FeedEntry.COLUMN_NAME_UPDATED value.
What should is value be? What does it mean actually?
It's the name of the update column in the feed table ;)
// How you want the results sorted in the resulting Cursor
String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC";

Select date range from sqlite

I really need your help.
I have problem getting values from database. I have two editTexts and one Button. When I insert dates and press Button my table opens without data. How can I get for my database to read those editText values?
My code:
String val1=minDate.getText().toString();
String val2=maxDate.getText().toString();
Cursor c=database.rawQuery("SELECT * from TABLE WHERE DATE BETWEEN '"+ val1+"' AND '"+ val2+"' ", null);
Please refer to documentation:
http://developer.android.com/guide/topics/data/data-storage.html#db
To avoid SQL injection, never add parameter to query by concatenating values, To create a query please refer to: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String,%20java.lang.String%29
Security tip:
http://developer.android.com/training/articles/security-tips.html#ContentProviders
How date are stored in your table ? Which format ?
When you add parameter to your query format must be the same.
If you want to use BETWEEN key word, date must be stored in this format: year-month-date to be sorted by SQLite.
Update 1
You're right change date format to yyyy-mm-dd
You parameters in your query must have same format, of course.
Put query in variable and log it before, as this:
String query = "SELECT * from TABLE WHERE DATE BETWEEN '"+ val1+"' AND '"+ val2+"' ";
Log.d("MyQuery", query);
Cursor c=database.rawQuery(query, null);
Search in logcat TAG "MyQuery" and post value of query
Now replace your code with below and let me know what you got in Logs
Cursor cursor=database.rawQuery("SELECT * from TABLE WHERE DATE >='"+val1+ "' AND <= '"+val2+ "'", null);
if (cursor.moveToFirst()) {
do {
Log.i("cursor_Data",cursor.getString(cursor.getColumnIndex("Date")));
} while (cursor.moveToNext());
}
If above not work for you then please post your TABLE Structure?

Sqlite update DATETIME field not working

I am trying to update a DATETIME column in a Sqlite table using the following query.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.rawQuery(query, null);
I have tested this on both device and emulator but its not working, its not updating the value in the column.
PS: I tried the same query in Sqlite browser and its working there.
Please help me figure out a solution.
Thanks.
Instead of using rawQuery(), use execSQL() if the execution doesn't return any data.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.execSQL(query);
Though just a tip, it is better to use UNIX time if you really want to store and process date in the future. It will be more flexible and standardized.

android database through SQLIteDatabase

I am trying to learn the SQL Database stuff for SQLite using the android. I have seen a couple examples of the Queries....
I have a two part question about sqlite queries in android.
Part 1
Say I want to delete something. and I use the following Query.
db.delete(MY_DB_TABLE, "CustomerName = ?", new String[] { customerName });
what would happen if the Customer name had a bad character in it.
For example. If I use the following Query
db.execSQL("delete from " + MY_DB_TABLE +
" where customername = '" + customerName + "';");
and say for this example the name of my customer was "Arby's".
That query would blow up because the ' is a special character and the query would not be formatted correctly.
Part 2
does this format allow me to specify as many paramaters as I want.
Example:
db.delete(MYTABLE, "val1 = ? and val2 != ?", new String[] { "test", "test2" } );
Please refer to my post here:
Storing Lists to A Database, and Retrieving Them All Together : Android
and short answer to your question, yes.
Each '?' means that an argument will be expected, so for each '?' you WILL have an exact number of arguments to pass in unless you want an exception :) !

Categories

Resources