Friday 20 April 2012

Create DataBase In Android (Part 2)

Step 1:- Now create Insert Method in database class for inserting data in database table

public long insert(String title,String type) {


this.insertStmt.bindString(1,title);
this.insertStmt.bindString(2,type);
return this.insertStmt.executeInsert();
}

Step 2:- Now Create Select Method


     public void selectAll() {
Cursor cursor = null;
    cursor = this.db.query(TABLE_NAME, new String[] { "title","type"},
"", null, null, null, "id desc");
 
if (cursor.moveToFirst()) {

do {
System.out.println(cursor.getString(0));
       System.out.println(cursor.getString(1));

} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}


Setp 3: - Method for delete and Update Table Data


public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}

public void deleteRow(long rowId) {
db.delete(TABLE_NAME, "id=" + rowId, null);
}

public void updateRow(long rowId, String text, String title, String date) {
ContentValues args = new ContentValues();
args.put("memodata", text);
args.put("title", title);
db.update(TABLE_NAME, args, "id=" + rowId, null);
}



No comments:

Post a Comment