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 :) !
Related
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();
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";
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
I've done quite a bit of research on what I thought would be an easy question but I cannot find what I am looking for. I am simply trying to return a record as a match with only a search term matching part of the text in the record.
For example, if the user searches for "ed" I would like any record that contains "ed" to be returned. So if there was a record with the name "Edward" in the name column it would be returned as a match. If there was a record with "finished" in the description, it would also return that record as a match.
I have looked into full text search but not sure if this is something that I would need to do or if it would even do what I need.
As always, I'm not looking for an answer per say, I'm just looking for a direction.
Never used SQLite before, but does it have the "LIKE" operator?
In MySQL, you can do something like:
SELECT name FROM list WHERE name LIKE '%ed%';
Here is some code that will do what you want if you are querying a content provider. If you are querying a sqlite database object directly, the parameters are similar but different.
String search = "ed";
// surround it with the SQL wildcard '%'
String q = "%" + search + "%"
Cursor c = getContentResolver().query(uri,
new String[] { column1, column2 },
"column_name like ?",
new String[] { q },
null);
The snippet will search the content provider at uri for the string ed in column_name.
I have a database with five tables in an Android application. I have been surfing around looking for a way to put conditions in the query (WHERE, &&, OR).
My queries are the form:
public Cursor getAlternative(int questionid) {
Cursor cursor = mDb.query(DBTABLE_ALTERNATIVE, new String[] { KEY_ALT }, KEY_QID + "=" + questionid, null, null, null, null, null);
return cursor;
}
But I find that many people write their queries with regular SQL, for ex:
Cursor c = myDB.query("SELECT FirstName,Age" +
" FROM " + MY_DATABASE_TABLE
+ " WHERE Age > 10 LIMIT 7;",
null);
What is the most efficient way? To me it seems easier form regular SQL statements, but after reading the tutorials on the Android Dev site I started forming the queries like above.
Question 2: if I use the first way, how can I use two conditions? Say I have two parameters, questionid and categoryid, how do I put the next KEY_CID + "=" + categoryid in there?
I have tried with && and AND but none seem to work. Thanks.
What is the most efficient way?
It depends... but generally speaking the second way will be faster. Why? because it won't need to build the query string using the parameters that the query method takes.
However, I'd rather use the first way since it's less error prone.
Question 2. If I use the first way, how can I use two conditions?
It should work this way:
KEY_QID + "=" + questionid + " AND " +KEY_CATID + "=" + categoryid
I am not sure if the simple implications about String and StringBuffers (StringBuilder would even better) hold, as the SQL engine also needs to parse that query string again.
The db.query() way may have the advantage that some parts of the query can be stored in a pre-parsed way (think "PreparedStatement"). Especially if the parameters are not put in the string, but as placeholders
E.g. where KEY_QID=? AND KEY_CATID=?
Here the basic query "stays constant" and the system can optimize.