SqlLite DataBase Example
Step 1: Create a DBManager Class which extends SQLiteOpenHelper
public class DBManager extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Databasename.db";
private static final int DATABASE_VERSION = 1;
private static SyncManagerDBAdapter instance;
public Context context;
public static SyncManagerDBAdapter getSharedObject(Context context) {
if (instance == null) {
instance = new SyncManagerDBAdapter(context);
}
instance.context = context;
return instance;
}
public SyncManagerDBAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public SQLiteDatabase getWritableDatabase() {
SQLiteDatabase sqdb = super.getWritableDatabase();
sqdb.setLockingEnabled(true);
return sqdb;
}
public SQLiteDatabase getReadableDatabase() {
return super.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE demotable(demoId INTEGER PRIMARY KEY,demoName TEXT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Step 2: Insert Into the Table Using ContentValues.
public void StoreIntoTheTable(String demoid,String demoname)
{
SQLiteDatabase sqdb = databaseManager.getWritableDatabase();
sqdb.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put("demoId", demoid);
cv.put("demoname", demoname);
try {
sqdb.insertOrThrow("demotable", null, cv);
} catch (SQLiteConstraintException e) {
Log.e("Already Stored", "true");
}
sqdb.setTransactionSuccessful();
} catch (Exception e) {
Log.e("Already Stored", "true");
}
sqdb.endTransaction();
sqdb.close();
}
}
Step 3::
private void ReadfromDb(Context context) {
DBManager dbmanager = DBManager.getSharedObject(context);
SQLiteDatabase db = dbmanager.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from demotable", null);
db.beginTransaction();
// Toast.makeText(this, ""+count, Toast.LENGTH_LONG).show();
try {
int count = cursor.getCount();
if (!(count == 0)) {
cursor.moveToFirst();
for (int i = 0; i < count; i++) {
String Sno = cursor.getString(cursor
.getColumnIndexOrThrow("demoid"));
String Sname = cursor.getString(cursor
.getColumnIndexOrThrow("demoname"));
cursor.moveToNext();
}
} else {
cursor.deactivate();
cursor.close();
db.close();
}
} catch (Exception e) {
// TODO: handle exception
}
cursor.deactivate();
cursor.close();
db.close();
}
public class DBManager extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Databasename.db";
private static final int DATABASE_VERSION = 1;
private static SyncManagerDBAdapter instance;
public Context context;
public static SyncManagerDBAdapter getSharedObject(Context context) {
if (instance == null) {
instance = new SyncManagerDBAdapter(context);
}
instance.context = context;
return instance;
}
public SyncManagerDBAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public SQLiteDatabase getWritableDatabase() {
SQLiteDatabase sqdb = super.getWritableDatabase();
sqdb.setLockingEnabled(true);
return sqdb;
}
public SQLiteDatabase getReadableDatabase() {
return super.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE demotable(demoId INTEGER PRIMARY KEY,demoName TEXT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Step 2: Insert Into the Table Using ContentValues.
public void StoreIntoTheTable(String demoid,String demoname)
{
SQLiteDatabase sqdb = databaseManager.getWritableDatabase();
sqdb.beginTransaction();
try {
ContentValues cv = new ContentValues();
cv.put("demoId", demoid);
cv.put("demoname", demoname);
try {
sqdb.insertOrThrow("demotable", null, cv);
} catch (SQLiteConstraintException e) {
Log.e("Already Stored", "true");
}
sqdb.setTransactionSuccessful();
} catch (Exception e) {
Log.e("Already Stored", "true");
}
sqdb.endTransaction();
sqdb.close();
}
}
Step 3::
private void ReadfromDb(Context context) {
DBManager dbmanager = DBManager.getSharedObject(context);
SQLiteDatabase db = dbmanager.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from demotable", null);
db.beginTransaction();
// Toast.makeText(this, ""+count, Toast.LENGTH_LONG).show();
try {
int count = cursor.getCount();
if (!(count == 0)) {
cursor.moveToFirst();
for (int i = 0; i < count; i++) {
String Sno = cursor.getString(cursor
.getColumnIndexOrThrow("demoid"));
String Sname = cursor.getString(cursor
.getColumnIndexOrThrow("demoname"));
cursor.moveToNext();
}
} else {
cursor.deactivate();
cursor.close();
db.close();
}
} catch (Exception e) {
// TODO: handle exception
}
cursor.deactivate();
cursor.close();
db.close();
}
Comments
Post a Comment