Friday, August 1, 2014

DBAdapter

DBAdapter

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
public static final String DBNAME = "ramkidb";
public static final String DBTABLE = "usrmstp";
public static final int DBVERSION = 1;

public static final String ROWID ="id";
public static final String NAME1 = "name1";
public static final String PASSWORD = "password";
//public static final String CONTENT = "Content";
private static final String TAG = "DBAdapter1";


private static final String DBCREATE = "create table " + DBNAME +
" (" +ROWID + "integer primary key autoincrement, " + NAME1 + "text not null"
+ PASSWORD + "text not null);";

/*"create table if not exist usrmstp (id integer primary key autoincrement,"
+ "name1 VARCHAR not null, password VARCHAR);";
*/

    private SQLiteHelper DBHelper;

private SQLiteDatabase db;

private Context context;

public class SQLiteHelper extends SQLiteOpenHelper
{
public SQLiteHelper(Context context, String name,CursorFactory factory, int version)
{
super(context,DBNAME,null,DBVERSION);
}


@Override
public void onCreate(SQLiteDatabase db1)
{
try{
db1.execSQL(DBCREATE);
} catch (SQLException e){
e.printStackTrace();
}
}


@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 "+ DBTABLE);
onCreate(db);
}


}

public DBAdapter(Context ctx)
{
context = ctx;
DBHelper = new SQLiteHelper(context, DBNAME, null, DBVERSION);

}



public DBAdapter openToRead() throws SQLException
{
// DBHelper = new SQLiteHelper(context);
DBHelper = new SQLiteHelper(context, DBNAME, null, DBVERSION);
db = DBHelper.getReadableDatabase();
return this;
}

public DBAdapter openToWrite() throws android.database.SQLException {
// DBHelper = new SQLiteHelper(context);
DBHelper = new SQLiteHelper(context, DBNAME, null, DBVERSION);
   db = DBHelper.getWritableDatabase();
return this;
}

//close the database
public void close()
{
DBHelper.close();
}



public long insertRecord(String name1, String password)
{
ContentValues intialValues = new ContentValues();
intialValues.put(NAME1, name1);
intialValues.put(PASSWORD, password);
return db.insert(DBTABLE, null, intialValues);
}


public String getSingleEntry(String name1)
{
Cursor cursor = db.query("RAMKIDB",null, " NAME1=?", new String[] {name1}, null, null, null);

if(cursor.getCount() < 1)
{
cursor.close();
return "NOT EXIST";
}

cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;

}


public boolean deleteRecord(long rowId)
{
return db.delete(DBTABLE, ROWID + "=" + rowId, null) > 0;
}

public int deleteAll() {
return db.delete(DBTABLE, null, null);
}


public Cursor getAllRecords()
{
return db.query(DBTABLE, new String[] {ROWID,NAME1,PASSWORD},null,null,null, null, null);
}

public Cursor getRecord(long rowid) throws SQLException
{
Cursor mCursor = db.query(true,DBTABLE,new String[] {ROWID,NAME1,PASSWORD}, ROWID

+ "=" , null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}



public boolean updateRecord(long rowId, String name1, String password)
{
ContentValues args = new ContentValues();
args.put(NAME1, name1);
args.put(PASSWORD, password);
return db.update(DBTABLE, args, ROWID + "=" + rowId, null) > 0;
}


public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
//db = DBHelper.getWritableDatabase();
return this;
}




}

No comments:

Post a Comment