i want to authenticate user id and password from sqlite database. The user table is saved in sqlite db, i want to compare the edittext value with the value saved in the sqlite db, when i initialize and open the database i get error that the method open() is undefined for the Db_getUserDetail (name of my database helper class).
code of main activity
@Override
public void onClick(View v) {
ffCode = ed_login.getText().toString();
password = ed_password.getText().toString();
// check if any of edit text is empty
if(ffCode.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your FF code", Toast.LENGTH_LONG).show();
return;
}
else if (password.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your password", Toast.LENGTH_LONG).show();
return;
}
Log.e("opening database", "yes");
Db_getUserDetail myDb= new Db_getUserDetail(MainActivity.this);
myDb.open();
//this is the method to query
String storedffcode=Db_getUserDetail.getCodeAndPassword(ffCode);
myDb.close();
if(ffCode.equals(storedffcode))
{
Toast.makeText(getApplicationContext(), "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
code for getting values from database
public static String getCodeAndPassword(String ffCode)
{
Log.e("retrieving ff code", "yes");
SQLiteDatabase db = null;
Cursor cursor = db.query("user_detail", null, " ff_code=?" , new String[]{ffCode}, null, null, null);
if(cursor.getCount()<1)
{
cursor.close();
return "Not Exist";
}
Log.e("found ff code", "yes");
cursor.moveToFirst();
String ffcode= cursor.getString(cursor.getColumnIndex("ff_code"));
return ffcode;
}
log cat:
java.lang.NullPointerException at com.example.db_client.Db_getUserDetail.getCodeAndPassword(Db_getUserDetail.java:135) at com.example.db_client.UserDetailActivity$1.onClick(UserDetailActivity.java:75) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136)enter code here
code of db helper class
public class Db_getUserDetail extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "CRM";
public static final String CONTACTS_TABLE_NAME = "user_detail";
public static final String CONTACTS_COLUMN_USER_ID = "user_id";
public static final String CONTACTS_COLUMN_USER_PWD = "user_pwd";
public static final String CONTACTS_COLUMN_FF_CODE= "ff_code";
public static final String CONTACTS_COLUMN_FF_NAME = "ff_name";
public static final String CONTACTS_COLUMN_TERR_CODE = "terr_code";
public static final String CONTACTS_COLUMN_DG_CODE= "dg_code";
public static final String CONTACTS_COLUMN_DIST_CODE = "dist_code";
public static final String CONTACTS_COLUMN_FF_MOB = "ff_mob";
public static final String CONTACTS_COLUMN_IMEI_NO = "imeino";
//constructor for database class DBHelper....
public Db_getUserDetail(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
String ff_code;
String ff_name;
String terr_code;
String dg_code;
String dist_code;
String ff_mob;
String imeino;
//override onCreate method to create Database table...
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub.
Log.e("DB created", "yes");
db.execSQL("create table user_detail" + "(user_id text, user_pwd text,ff_code text,ff_name text,terr_code text,dg_code text, dist_code text,ff_mob text,imeino text) ");
}
//Upgrade new Table...
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS user_detail");
onCreate(db);
}
//to insert new records in table...
public boolean insertUserData (String user_id, String user_pwd, String ff_code ,String ff_name,String terr_code,String dg_code, String dist_code,
String ff_mob, String imeino)
{
Log.e("DB insert call", "yes");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
Log.e("content values", "yes");
contentValues.put("user_id", user_id);
contentValues.put("user_pwd", user_pwd);
contentValues.put("ff_code", ff_code);
contentValues.put("ff_name",ff_name);
contentValues.put("terr_code", terr_code);
contentValues.put("dg_code", dg_code);
contentValues.put("dist_code", dist_code);
contentValues.put("ff_mob", ff_mob);
contentValues.put("imeino", imeino);
db.insert("user_detail", null, contentValues);
return true;
}
//get data from table...
public Cursor getData(String id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from user_detail", null );
return res;
}
//get no of rows in table...
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
//to delete data from table at particular id....
public Integer deleteContact (String id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("user_detail","id = '"+id+"'", null);
}
//to get all data from database...
public ArrayList getAllCotacts()
{
ArrayList array_list = new ArrayList();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from user_detail", null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_USER_ID)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_USER_PWD)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_FF_CODE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_FF_NAME)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_TERR_CODE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DG_CODE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_DIST_CODE)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_FF_MOB)));
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_IMEI_NO)));
res.moveToNext();
}
return array_list;
}
public static String getCodeAndPassword(String ffCode)
{
Log.e("retrieving ff code", "yes");
SQLiteDatabase db = null;
//SQLiteDatabase db = getWritableDatabse();
Cursor cursor = db.query("user_detail", null, " ff_code=?" , new String[]{ffCode}, null, null, null);
if(cursor.getCount()<1)
{
cursor.close();
return "Not Exist";
}
Log.e("found ff code", "yes");
cursor.moveToFirst();
String ffcode= cursor.getString(cursor.getColumnIndex("ff_code"));
return ffcode;
}
Aucun commentaire:
Enregistrer un commentaire