我想从sqlite数据库验证用户ID和密码。用户表保存在sqlite db中,我想将edittext值与sqlite db中保存的值进行比较,当我初始化并打开数据库时,我得到的错误是方法open()未定义为Db_getUserDetail(我的名字)数据库助手类)。
主要活动代码
@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();
}
}
});
从数据库中获取值的代码
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;
}
记录猫: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;
}
答案 0 :(得分:1)
您无需打开数据库getWritableDatabse()
方法将打开它。
所以删除这一行
myDb.open();
另一个是NullPointerException
并更改此
SQLiteDatabase db = null;
到
SQLiteDatabase db = getWritableDatabse();
将getCodeAndPassword()
更改为实例方法而不是static
方法
更改此
public static String getCodeAndPassword(String ffCode)
到
public String getCodeAndPassword(String ffCode)
并从MainActivity调用
String storedffcode=myDb.getCodeAndPassword(ffCode);
改为
String storedffcode=Db_getUserDetail.getCodeAndPassword(ffCode);