Create Mydb class for Manage database
package actiitylifecycle.vsoftcoders.net.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Mydb {
private static final String TAG = "STOREDETAILS";
private static final String DATABASE_NAME = "emp";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "tbl_details";
private static final String TBL_CATEGORY = "create table tbl_category("+"id integer primary key
autoincrement , " + "category TEXT);";
private static final String TBL_DETAILS = "create tbl_details(" + "id integer primary key
autoincrement , " + "first_name TEXT,"+ "last_name TEXT," + "mobile TEXT," + "email TEXT);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public Mydb(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override public void onCreate(SQLiteDatabase db) {
db.execSQL(TBL_CATEGORY);
db.execSQL(TBL_DETAILS);
}
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS tbl_category");
onCreate(db);
db.execSQL("DROP TABLE IF EXISTS tbl_details");
onCreate(db);
}
}
public boolean upgradeTableData(String DATABASE_TABLE) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Delete from TableName
// DELETE FROM SQLITE_SEQUENCE WHERE name='TableName';
db.execSQL(TBL_CATEGORY);
return db.delete(DATABASE_TABLE, null, null) > 0;
}
// ---opens the database---
public Mydb openAsReadOnly() throws SQLException {
db = DBHelper.getReadableDatabase();
return this;
}
// ---opens the database---
public Mydb open() throws SQLException {
if (db != null) {
if (db.isOpen()) {
DBHelper.close();
}
}
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database--- public void close() {
if (db.isOpen()) {
DBHelper.close();
}
}
// ---insert a title into the database---
public long insert(String DATABASE_TABLE, ContentValues initialValues) {
return db.insert(DATABASE_TABLE, null, initialValues);
}
// ---deletes a particular title--- public boolean delete(String DATABASE_TABLE, String KEY_ROWID,
long rowNumericId, String rowStringId, Boolean flag) {
if (flag) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowNumericId,
null) > 0;
} else {
return db.delete(DATABASE_TABLE, KEY_ROWID + "='" + rowStringId
+ "'", null) > 0;
}
}
public void delete(String DATABASE_TABLE, String id) {
db.execSQL("delete from "+DATABASE_TABLE+" where id='"+id+"'");
}
// ---deletes multiple title---
public boolean deleteMultiple(String DATABASE_TABLE, String KEY_ROWID,
String rowStringId, Boolean flag) {
if (flag) {
return db.delete(DATABASE_TABLE, KEY_ROWID + " IN(" + rowStringId
+ ")", null) > 0;
} else {
return db.delete(DATABASE_TABLE, KEY_ROWID + " NOT IN(" + rowStringId + ")",
null) > 0;
}
}
// ---deletes a all the title---
public boolean deleteAll(String DATABASE_TABLE) {
return db.delete(DATABASE_TABLE, null, null) > 0;
}
// ---retrieves all the titles--- public Cursor getAll(String select) {
return db.rawQuery(select, null);
}
// ---retrieves a particular title---
public Cursor getSingle(String select) throws SQLException {
Cursor mCursor = null;
try {
mCursor = db.rawQuery(select, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
} catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace();
}
return mCursor;
}
public void updateValue(String DATABASE_TABLE, ContentValues val,int id)
{
db.update(DATABASE_TABLE, val, "id="+id, null);
}
}
Now you can perform insert,update delete
Mydb db = new Mydb(this);
db.open();
ContentValues namelist = new ContentValues();
namelist.put("category", "Test by vijay");
db.insert("tbl_category", namelist);
Cursor crs = db.getAll("select * from tbl_category");
//"select * from tbl_category where id = '"+ "" + "'"if (crs.getCount() > 0) {
if (crs.moveToFirst()) {
do {
String data = crs.getString(crs.getColumnIndex("category"));
// do what ever you want here Log.e("category", "category****" + data);
int id = crs.getInt(crs.getColumnIndex("id"));
Log.e("category", "category****" + id);
} while (crs.moveToNext());
}
String id = "17";
//for delete detail db.delete("tbl_category", id);
//for update details namelist.put("category", "Test updated by vijay");
db.updateValue("tbl_category", namelist, 18);
crs.close();
} else {
db.insert("tbl_category", namelist);
}
db.close();
No comments:
Post a Comment