Lowercase in SQLite for Android - android

I'm now getting problem about how to convert capitalize word to lowercase in SQLite for Android.
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor mCursor =
mDb.query(true, FUNCTIONS_TABLE, new String[] {
FUNS_WORD, FUNS_BODY}, FUNS_WORD + "='" + searchword + "'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
I want to conver like LOWER(FUNS_WORD) + "='" + searchword + "'" unfortunately, getting syntax error. What I want is I want to convert all of data in FUNS_WORD to lowercase type.

You'll have to use rawQuery
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor mCursor =
mDb.rawQuery("SELECT FUNS_WORD, FUNS_BODY FROM FUNCTIONS_TABLE WHERE LOWER(FUNS_WORD) = '" + searchword + "'", null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

You can use the LIKE keyword in place of =. The LIKE keyword is case insensitive and can be used in your case without % sign.

There is a SQL injection vulnerably in the query '" + searchword + "'. This could allow an attacker to compromise the database.
Cursors should be wrapped in try/finally blocks to ensure they are always closed upon completion.
public Cursor fetchFunctions(String searchword) throws SQLException {
Cursor cursor = null;
try {
cursor = mDb.rawQuery("SELECT FUNS_WORD, FUNS_BODY FROM FUNCTIONS_TABLE WHERE LOWER(FUNS_WORD) = ?", new String[] { searchword });
if (cursor != null) {
cursor.moveToFirst();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return cursor;
}

Related

retrieve specific values from SQLite database

I am a beginner in Android. I created a database with unique id, name, email etc. I want to retrieve the specific data from the database using the unique id. I don't know what to do. I read the previous answers but didn't get any idea from that.
At first you should read SQL Database
Below code is DEMO
public String getSpecificResult(int getID) // you should pass getID from your Class
{
SQLiteDatabase db = this.getWritableDatabase();
String get_Status = "0";
String selectQuery = "SELECT your_coloum_ FROM "+ TABLE_NAME + " WHERE " + YOUR_UNIQUE_KEY + " = '"+ getID +"'";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null && c.moveToFirst()) {
get_Status = c.getString(0); // Return 1 selected result
}
db.close();
return get_Status;
}
Try this..
pass ur unique_id to get the contents.. alter names according to your table.
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_NUMBER}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

Display multiple rows in a textview Android

Hello guys I want to display records. However, I don't want to display them in listview but in textview. I have in my to do the new line feed \n to do the trick, but in my program, it just shows the first record.
This is what I've tried so far:
MainActivity.class
Bundle extras = getIntent().getExtras();
if (extras != null) {
dog_name = extras.getString("dog_name");
cursor = dbHelper.fetchbBreedByName(dog_name);
strID = cursor.getString(0);
strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
strDiet = cursor.getString(cursor.getColumnIndexOrThrow("diet"));
strShelter = cursor.getString(cursor.getColumnIndexOrThrow("shelter"));
strHygiene = cursor.getString(cursor.getColumnIndexOrThrow("hygiene"));
strMedication = cursor.getString(cursor.getColumnIndexOrThrow("medication"));
strBreed = cursor.getString(cursor.getColumnIndexOrThrow("breed"));
Log.d("Animal ID", "Animal ID is " + strID + " and breed is " + strBreed);
Log.d("Desc", "Desc " + strDesc);
Description.setText(strDesc);
Diet.setText(strDiet);
Shelter.setText(strShelter);
Hygene.setText(strHygiene);
Medication.setText(strMedication); }
DBHelper.class
public Cursor fetchbBreedByName(CharSequence inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
null, null, null, null, null);
}
else {
String qry = "SELECT _id, description, diet, shelter, hygiene, medication, " +
"breed FROM tblAnimalInfo WHERE breed LIKE '%" + inputText + "%';";
mCursor = myDataBase.rawQuery(qry, null);
//mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
// KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
// KEY_BREED + " like '%" + inputText + "%'", null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
I don't know what's wrong. Please help me figure out what's missing in my code. Thanks in advance.
It seems that you are only loading the first row given by your Cursor. You have to use cursor.moveToNext(); to be able to get the rest of your data. Take a look to the example below:
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
You are missing cursor.moveToNext() function to move to the next record, as mentioned earlier. Besides I would like to mention a few other issues in the coding style. You should not call moveToFirst() in the query function itself, use it where you are actually traversing through the records. Also make sure you close the cursor appropriately. So your code should look like:
cursor = dbHelper.fetchbBreedByName(dog_name);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
....
< access the record >
...
} while (cursor.moveToNext());
}
cursor.close(); // very important
}
And remove these lines from fetchbBreedByName()
if (mCursor != null) {
mCursor.moveToFirst();
}

SQLite Cursor return NULL if no record found in Android

I have problem today about retrieving data from SQLite. Here is my coding
public Cursor fetchDictionary(String searchword) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_WORD, KEY_DEF}, KEY_WORD + "='" + searchword + "'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Above coding if no record found, Android prompt dialogbox and force to close. Is there any way to return "no record found" error message if no record found?
You can also do the following to check if there is data at the current column index cursor.isNull(columnIndex);
Do a try catch, and replace the e.printStackTrace() with your messages.
You have 'throws SQLException' in your code... catch the exception and determine if the exception is due to no-record-found.
try{
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_WORD, KEY_DEF}, KEY_WORD + "='" + searchword + "'", null,
null, null, null, null);
if (mCursor != null && mCursor.getCount()>0) {
mCursor.moveToFirst();
// rest goes here
}else{
Toast.makeText(context,"No records found ",Toast.LENGTH_LONG).show();
// show error msg here
}
}catch(Exception e){
e.printStackTrace();
}

sqlite selection error

I got this code in one of my methods. It should check if the string is already in the database and if it is the db entry gets updated. If it is not there I catch the exception and just create the entry.
String s="hallo";
try {
Cursor c1 = dba.fetchSubject(s);
dba.updateSubject(c1.getLong(c1.getColumnIndex(DbAdapter.KEY_ROWID)), s, t, r);
} catch (SQLException e){
dba.createSubject(s, t, r);
}
The problem is fetchSubject allway throws an Exception.
I use a similar method to get a db entry by a rowId instead of a String, which is working:
public Cursor fetchSubject(long rowId) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_ROWID
+ "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The method called on top:
public Cursor fetchSubject(String subject) throws SQLException {
Cursor mCursor = database.query(true, DATABASE_TABLE2, new String[] {
KEY_ROWID, KEY_SUBJECT, KEY_TEACHER, KEY_ROOM}, KEY_SUBJECT
+ "=" + subject, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The error:
sqlite returned: error code = 1, msg = no such column: hallo
I don't know where my error is. Isn't it possible to use a string at the selection parameter?
Try adding single quotes around hallo
String s = "'hallo'";
Essentially what you're doing is telling your query: select blah blah blah from TABLE where subject = hallo. Since hallo isn't surrounded by quotes, it is trying to find a variable / column with that name. When we surround it with quotes, we are saying that we are looking for the string within these quotes, rather than a variable.
Think of it like normal programming..
String s = hallo;
is completely different than
String s = "hallo";

STRING in sqlite database query

When i try to search in row POINTA (text data type in SQLite) and I compare it to a String the program stops. This is the code:
public Cursor getpoints(String start,String end) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_PRIM,
NAME,
POINTA,
POINTA_LANG,
POINTA_LAT,
POINTB,
POINTB_LANG,
POINTB_LAT
},
POINTA +"=" +start,//here is the problem
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
...
Same problem as this, you need single quotes around your start String, i.e.
POINTA + "='" + start + "'",

Categories

Resources