coderslay
coderslay

Reputation: 14370

Sqlite3 query not running properly on Android

I have a query like this

SELECT value1 FROM mytable where (value1 > value2) AND category = 1 ;

This is not executing properly in Android. Although if i manually go to

/data/data/packagename/databases/mydatabase.db

Here is the Code

try {
SQLiteDatabase sqdb = _context.openOrCreateDatabase(_DB_NAME, 0,
                null);
String sql = null;
sql = "SELECT value1 FROM mytable where (value1 > value2) AND category = " + category
                +"; ";
Cursor c = sqdb.rawQuery(sql, null);
Log.i("sql = ", sql);

if (c.moveToFirst()) {
while (!c.isAfterLast()) {

categoryValue = c.getString(0);
c.moveToNext();
}
}
Log.i("categoryValue", categoryValue);
sqdb.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}

And then execute it. Then it works perfectly fine.

What is happeneing?

Upvotes: 0

Views: 70

Answers (1)

waqaslam
waqaslam

Reputation: 68187

i believe first you need to close cursor and then the database connection

//the order should be
c.close();
sqdb.close();

Upvotes: 1

Related Questions