John Walker
John Walker

Reputation: 1121

How come can't read the data from SqlLiteDatabase in Android

SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.abc2/databases/DB_BusData", null, 0);
    Cursor c = db.rawQuery("SELECT * FROM Tbl_Driver", null);
    String username = c.getString(0).trim();
    String password = c.getString(1).trim();
    //Log.d(username, "try");
    db.close();

this is connecting to my DB_BusData in my assets folder,
anything wrong with my code?

p/s: I don't like to use DatabaseHelper.java or what, that is too complicated code. i just want a simply database connection and bind it to my Spinner

Upvotes: 0

Views: 49

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78525

You need to move the cursor to a position before you can read from it:

SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.abc2/databases/DB_BusData", null, 0);
Cursor c = db.rawQuery("SELECT * FROM Tbl_Driver", null);
if(c.moveToFirst()) {
    String username = c.getString(0).trim();
    String password = c.getString(1).trim();
    //Log.d(username, "try");
}
db.close();

P.S. This database is not in your assets folder. I'm assuming that your file resides on the local device at the path you specified and that it opens successfully

Upvotes: 2

Related Questions