How to check if data already saved in Android database - android

I am trying to check if the text the user inputs is already in my database but unsure how this should be done.
So I can fetch the data from my database by calling the following in my database helper class:
public Cursor fetchDamagedComponentsForLocation(long damagedComponentId) {
Cursor mCursor =
rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
COMPONENT_ID, LOCATION_LINK, RUN_LINK, AREA_LINK, INSPECTION_LINK, LOCATION_REF, RACKING_SYSTEM, COMPONENT, POSITION, RISK, ACTION_REQUIRED, NOTES_GENERAL, MANUFACTURER, TEXT1, TEXT2, TEXT3, TEXT4, NOTES_SPEC},
LOCATION_LINK + "=" + damagedComponentId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
For each of the database entries I need to check if two user inputs match two of the database fields (in this case COMPONENT and POSITION) which I get from my Spinners when the user clicks on the save button:
String component = ((Cursor)componentSpinner.getSelectedItem()).getString(2).toString();
String position = ((Cursor)positionSpinner.getSelectedItem()).getString(1).toString();
This is basically to make sure there is not already that component saved.
I'm sure this is quite simple, but I can't work out how to do it..
edit - following Sams advice, I have added the following class to my databased helper class:
public boolean checkIfComponentAlreadySaved(long locationId, String component, String position) {
Cursor mCursor = rmDb.query(DAMAGED_COMPONENTS_TABLE, new String[] {"1"},
LOCATION_LINK + " = " + locationId + " AND " + COMPONENT + " = " + component + " AND " + POSITION + " = " + position, null, null, null, null, null);
boolean result = mCursor.moveToFirst();
mCursor.close();
return result;
}
However, I am getting the following error:
android.database.sqlite.SQLiteException: near "pin": syntax error: , while compiling: SELECT 1 FROM damaged_components_table WHERE location_link = 3 AND component = Locking pin AND position = Not Applicable
I'm guessing this is because I am comparing Strings, but any suggestions?

moveToFirst() returns true if one or more rows exist, false if the Cursor is empty. So create a new method like this:
public boolean isComponentDamagedForLocation(long damagedComponentId) {
Cursor mCursor =
rmDb.query(DAMAGED_COMPONENTS_TABLE, new String[] {"1"},
LOCATION_LINK + "=" + damagedComponentId, null,
null, null, null, null);
result = mCursor.moveToFirst();
mCursor.close();
return result;
}
The exact columns don't matter so you can pass anything that you want, the WHERE clause is the heart of this query. Use this Java method, to test if the data exists. If isComponentDamagedForLocation) is false, then insert the data.
Addition
Since you are using Strings in your query you should use the replacement character (?) to prevent the error you are seeing now and protect yourself from SQL injection attacks:
public boolean checkIfComponentAlreadySaved(long locationId, String component, String position) {
Cursor mCursor = rmDb.query(DAMAGED_COMPONENTS_TABLE, new String[] {"1"},
LOCATION_LINK + " = " + locationId + " AND " + COMPONENT + " = ? AND " + POSITION + " = ?",
new String[] {component, position}, null, null, null, null);
// etc

I'm confused. You're retrieving data from the database in a Cursor, but you're also getting Cursor IDs from the Spinner? What's the source of the spinner data? getSelectedItem is a method of AdapterView, which assumes that each entry in the View is bound to a row in the backing data. getSelectedItem returns an "id" value in the backing data of the currently selected bound View.
Step back and think about the problem logically. What data is in the database? What data is the user entering? How do you make a record of what the user selected, and look for that selection in data you have in the database? Don't try to do fancy shortcuts, do it step-by-step. How would you search the database if the user had to type in the data?

Related

Sql search statement always returning empty even when there is data in database in android

I am using the following to fetch a string from my table. The cursor is always returning empty even when I have data in database. Is the query wrong?
public void find(String myNumber){
String[] thecolumns = new String[]{
ID,
FLAG};
cursor = sqlDb.query(MY_TABLE,
thecolumns, NUMBER + "='"
+ myNumber+ "'", null, null, null, null);
if (cursorToFetchAssets != null ) {
cursorToFetchAssets.moveToFirst();{
try{
//code to fetch
}catch{
//return when there are no rows found.
}
}
EDIT: NUMBER is of type string "...+ NUMBER + " TEXT,.. " and myNumber is also a string
FIXED: Issue was on the server side of my code. Not over here..
try this:
cursor = sqlDb.query(
MY_TABLE,
thecolumns,
NUMBER + "=?",
new String[]{String.valueOf(number)},
null, null, null);

Querying sqlite in android

I'm creating a database for my game, everything is working until I want to query one item.
I have been trying few different methods and I can't make it work. I just simply don't see the error in my code.
The code is as follows:
public Item getItem(String icon) {
String[] columns = {KEY_ID, KEY_TYPE, KEY_ICON, KEY_LEVEL, KEY_ARMOR, KEY_DAMAGE, KEY_BUY, KEY_SELL};
Cursor cursor = db.query(DB_TABLE, columns, KEY_ICON + "=" + icon,
null, null, null, null);
Item item=null;
if(cursor != null && cursor.moveToFirst()) {
item= new Item(cursor.getString(TYPE_COLUMN),
cursor.getString(ICON_COLUMN),
cursor.getString(LEVEL_COLUMN),
cursor.getString(ARMOR_COLUMN),
cursor.getString(DAMAGE_COLUMN),
cursor.getString(BUY_COLUMN),
cursor.getString(SELL_COLUMN)
);
}
return item;
}
The error I'm getting is
No such column: fast_boots (code 1): while compiling: SELECT id, type,
icon, level, armor, damage, buy, sell from items where icon=fast_boots
When trying to find .getItem("fast_boots"); I do see the fast_boots in my sql database
To make the query work, maybe you should try this :
KEY_ICON + "= '" + icon + "' "
As 'icon' is a string value. Since you're not specifying it, it is probably trying to understand it as being a column in the projection
This is the wrong way of implementing such functionality, though. Do not perform database queries on the getItem() method itself (main thread), it deserves to run in background, so it won't affect the main thread.
Please read about AsyncTask.
Try this
Cursor cursor = db.query(DB_TABLE, columns, KEY_ICON + "='" + icon +"'",
null, null, null, null);
I added '

Getting completion suggestions from a database

I am trying to retrieve first 5 words from database and set those as text of button for that here is my code
String [] deal;
String text,s1,s2,s3,s4,s5;
text=edt.getText().toString();
deal=db.getAllItemFilter(text);
s1=deal[0];
s2=deal[0];
s3=deal[0];
s4=deal[0];
s5=deal[0];
sug1.setText(s1);
sug2.setText(s2);
sug3.setText(s3);
sug4.setText(s4);
sug5.setText(s5);
for database query i am using this code but it is not working
public String[] getAllItemFilter(String text)
{
String [] columns= new String[]{word};
Cursor cursor = this.ourdatabase.query(database_table, columns, " word like 'text%' ", null, null, null, null);
String [] deal = new String[cursor.getCount()];
int iword = cursor.getColumnIndex(word);
int i=0;
for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
{
deal[i]=cursor.getString(iword);
i++;
if(i==5)
break;
}
return deal;
}
can anybody help me to get first 5 suggestions from database .Thanks in advance
you should first set a "LIMIT=5" at your SQL query end rather than a loop limit, it will save resources. Then you can replace word% by %word% to also match word beginning.
Your query is incorrect. As you have it set up, it is looking for "text*" every time (you forgot to format the query so the variable is used as a variable).
Change your cursor to what I show below if you want words beginnning with the supplied text:
Cursor cursor = this.ourdatabase.query(database_table, columns, " word like '" + text + "%' ", null, null, null, null);
If you just want words containing the supplied text, change it to:
Cursor cursor = this.ourdatabase.query(database_table, columns, " word like '%" + text + "%' ", null, null, null, null);

Android: sqlite returning information from all rows containing a similar value in one row

I am trying to have the user select a value, then search my database for that value, and return information from all rows containing that value.
I tried the following, but I am not sure how to return the results correctly to the other activity to be viewed by the user.
Cursor c = ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME + " = '" + favoriteWorkoutName + "'", null);
I also tried this, where the int row is specifying the KEY_ROW I want to grab data from when querying. For example, the user enters the name of a favorite workout, it then searches the database for all rows containing that name, and then returns KEY_ROWS 1, 2, and 3 (which correspond to Exercise, Reps, etc). However, this only returns one row value from one row.
int row = 1;
Cursor c = ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME + " = '" + favoriteWorkoutName + "'", null);
do{c.moveToNext();
String data = c.getString(row);
return data;
}while (row <= 3);
Any suggestions?
**EDIT:
I had already had something in my activity like that. Here is what I have there. However, I am getting an error, and it asks me to either "Change type of listCursor to Cursor", which it already is...or to "Change return type of 'GetFavoriteData' to Cursor", which is also already is.
Also, when I do get that to not have an error, I'm not sure how to use that returned data and insert it into my TextView. It will not allow me to setText(listCursor).
I ideally need to get the information from each returned row as a separate String so that I can display them the way I need to in TextViews.
In my Activity:
String favoriteWorkoutName = cfdName.getText().toString();
ExerciseDatabase choosefavorite = new ExerciseDatabase(WorkMeOutActivity.this);
try {
choosefavorite.open();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cursor listCursor = choosefavorite.GetFavoriteData(favoriteWorkoutName);
choosefavorite.close();
in my DBHelper class:
public Cursor GetFavoriteData(String favoriteWorkoutName){
return ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME + " = '" + favoriteWorkoutName + "'", null);
}
I think you misunderstood the getString() method. Have a look:
public abstract String getString (int columnIndex)
Since: API Level 1
Returns the value of the requested column as a String.
The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
Parameters
columnIndex the zero-based index of the target column.
Returns
the value of that column as a String.
So, if you pass 1,2 or 3 inside with c.getString(row) it only gives you value of only one column(with index 1,2 or 3) from a specific row.
You can use c.getString(c.getColumnIndex(column_name)) if you are not sure about index of a column and want to get it from it's name.
Now I think you want to retrieve value of some columns(say first 3 column value of a row) from first few matching rows(like 3 rows) and use them if so use:
Cursor c = ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME + " = '" + favoriteWorkoutName + "'", null);
String fcv="";
String scv="";
String tcv="";
if(c.getCount() == 0)
{
//no data found
}
else {
int i=0;
c.moveToFirst();
do {
fcv = fcv + c.getString(0)); //or use fcv = fcv + c.getString(c.getColumnIndex(name of first column)));
scv = fcv + c.getString(1));
tcv = fcv + c.getString(2));
i++;
} while (c.moveToNext() && i<3);
c.close();
Use the value of the strings as you like according to your need :)
Your question is not clear...
If you are trying to search all your different columns for that value your query should look more like this:
Cursor c = ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME +
" LIKE '" + favoriteWorkoutName + "' OR " + COLUMN2 + " LIKE '" + favoriteWorkoutName +
"' OR " + COLUMN3 + " LIKE '" + favoriteWorkoutName + "'", null);
That will search columns KEY_FAVNAME, COLUMN2 and COLUMN3 and return any rows that contain favoriteWorkoutName's contents.
If you want only those from the specific column, what you first posted is correct.
EDIT
After re-reading, I'm getting a different idea of what your issue is.
You should call for the cursor in the activity in which you are going to use it. The normal method is to define a DB helper class that creates the db and contains all functions related to it (adding, deleting, updating info, getting cursors, etc). Then you instantiate that class and use it's methods to work with the db.
An example would be (used in the activity where you want to access the data):
mDbHelper = new WorkoutDB(getActivity()); // instantiate your dbhelper class
mDbHelper.open(); // use the dbhelper open method to open the db
Cursor listCursor = mDbHelper.fetchFavoriteWorkout(favoriteWorkoutName); // use the fetchFavoriteWorkout method to get your cursor
Your dbhelper class would contain a method (among many others) something like this:
public Cursor fetchFavoriteWorkout(String favoriteWorkoutName){
return ourDatabase.rawQuery("SELECT * FROM FavoriteTable WHERE " + KEY_FAVNAME + " = '" + favoriteWorkoutName + "'", null);
}

Android: Deleting specific row in database

Sorry if this seems obvious. I'm trying to write a method to delete a row from a String showId. What would be the best way, and can Cursors only be used for Selects or also for Deletes and Updates?
These are the two methods I'm at so far:
public int deleteShowById1(String showId){
Cursor cursor = db.rawQuery("DELETE FROM tblShows WHERE showId = '" + showId+"'", null);
if (cursor.moveToFirst()) {
return 1;
} else
return -1;
}
public int deleteShowById2(String showId) {
String table_name = "tblShows";
String where = "showId='"+showId+"'";
return db.delete(table_name, where, null);
}
As we know from mysql query, it is same here in android.
String query = "DELETE FROM " +TABLE_NAME+ " WHERE " + COLUM_NAME+ " = " + "'"+VALUE +"'" ;
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(query);
db.close();
VALUE may or may not have single quotation depending on datatype.
I tend to use the second method (db.delete), as I think using rawQuery is frowned upon.
If you do a select, then loop through the cursor to do updates or deletes, that would make sense, but to pass a cursor to do the delete or update doesn't make sense to me, as the program won't know how to parse the cursor results to get the correct fields.

Categories

Resources