0

If i wanted to search for a string that is in the sql database below using java, how would i do so ? i honestly dont know where to start is their any material online that covers this?... thank you. I though i could have some sort of strstr() function or something that gathers the string in the table.

package f.s.l;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class HotOrNot {
public static final String KEY_ROWID ="_id";
public static final String KEY_NAME ="persons_name";
public static final String KEY_HOTNESS ="persons_hotness";

private static final String DATABASE_NAME ="HotOrNotdb";
private static final String DATABASE_TABLE ="peopleTable";
private static final int DATABASE_VERSION =1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_NAME + " TEXT NOT NULL, " +
        KEY_HOTNESS + " TEXT NOT NULL);"


        );

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);

}

}
public HotOrNot(Context c){
ourContext =c;
}
public HotOrNot open() throws SQLException{ 
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){

 ourHelper.close();
 }
 public long createEntry(String name, String hotness) {
 // TODO Auto-generated method stub
 ContentValues cv = new ContentValues();
 cv.put(KEY_NAME, name);
 cv.put(KEY_HOTNESS, hotness);
 return ourDatabase.insert(DATABASE_TABLE, null, cv);

 }
 public String getData() {
 // TODO Auto-generated method stub
String [] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {


result = result + c.getString(iRow) + " "+ c.getString(iName) + " " +                c.getString(iHotness) + "\n";
}



return result;
}
}
2
  • There's a whole bunch of Android tutorials basically everywhere, including in the Android docs. Are you trying to do a full-text search, over how much data, how big are the strings you're searching within, are you matching the contents of an entire column, etc. all make a difference. Commented Apr 29, 2012 at 23:47
  • im trying to do a full text search of the table that was created. Their are set of three variables that contain the strings and i want so search the strings stored in them . Commented Apr 29, 2012 at 23:52

4 Answers 4

5

In the query method, you need to declare the WHERE clause. For example:

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "some_col like " + "'%Somevalue%'", null, null, null, null);
Sign up to request clarification or add additional context in comments.

Comments

2

LIKE is a good SQL keyword for you to look up...

Comments

1

try below code for search :-

public Cursor getROUTINENAME(String search_str) {
        String query = "SELECT * FROM TBNAME where COL_NAME LIKE '%"+search_str+"%'";

        Cursor mCursor = db.rawQuery(query, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

call above function with string .

Comments

0

String query = " SELECT * FROM " + DB_NAME + " WHERE " + COLUMN_dars + " LIKE " + "'"+select+"'";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);

1 Comment

Just a heads up, this came up on the low quality post review queue. Please edit your post to include an explanation of what is occurring for future folks that stumble across the question. Cheers ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.