SQLite connection in ANDROID

2019-09-24 18:00发布

问题:

1) How To Retrieve Data from Database ... Can I Use Buttons to show my Database or it should be A ListView ??

2) where to put the class of Cursor in my Android App. ??

回答1:

You can refer to this link to know all about SQLite database.



回答2:

I'm posting simple way of creating database(using SQLiteDatabase object), inserting values to it and retrieving data from it using Cursor:

Database Creation:

SQLiteDatabase db; 
db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);

Table creation:

db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar);");

Inserting values in table:

db.execSQL("INSERT INTO LIST VALUES('খবর');"); 

After completion of work close the database as:

db.close(); 

Retrieving data using cursor:

db = SQLiteDatabase.openDatabase("path to your database", null,0); 
String q = "SELECT * FROM LIST where( wlist like '"+mComposing.toString()+"%')"+"ORDER BY wlist"; //your wanted query
Cursor c = db.rawQuery(q, null);
c.moveToFirst();
do {
//do whatever you like
} while (c.moveToNext()); 

Close cursor & database:

c.close();
db.close();

That's it, you can use these anywhere you want in your code segment.