我正在研究需要比较从蓝牙获取的字符串(用户ID和密码),将其存储在sqlite中,然后将其与我从editTexts获取的字符串(用户ID和密码)进行比较的项目。我可以从蓝牙读取输入并将其存储在sqlite中,但是我不知道如何将其与从editTexts获得的数据进行比较。你能帮我吗?
这是mydBHelper活动:
public class myDbAdapter {
myDbHelper myhelper;
public myDbAdapter(Context context)
{
myhelper = new myDbHelper(context);
}
public long insertData(String userid, String pass)
{
SQLiteDatabase dbb = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.UID, userid);
contentValues.put(myDbHelper.MyPASSWORD, pass);
long id = dbb.insert(myDbHelper.TABLE_NAME, null , contentValues);
return id;
}
static class myDbHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "myDatabase"; // Database Name
private static final String TABLE_NAME = "myTable"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID="_id"; // Column I (Primary Key)
private static final String MyPASSWORD= "Password"; // Column III
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+ MyPASSWORD+" VARCHAR(225));";
private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
private Context context;
public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context=context;
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context,"OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (Exception e) {
Message.message(context,""+e);
}
}
}
}
我得到的字符串已插入到FromServer活动中:
public class FromServer extends AppCompatActivity {
myDbAdapter helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_from_server);
//Userid_txt= (EditText) findViewById(R.id.editUserID);
//Pass_txt= (EditText) findViewById(R.id.editPass);
helper = new myDbAdapter(this);
}
//insert string from bluetooth
public void insertData() {
//----------------passed string from bluetooth
Intent intent = getIntent();
String message = intent.getExtras().getString("moved");
try {
JSONObject obj = new JSONObject(message);
String t1 = obj.getString("user_id");
String t2 = obj.getString("password");
if (t1.isEmpty() || t2.isEmpty()) {
Message.message(getApplicationContext(), "User ID and Password empty");
}
long id = helper.insertData(t1, t2);
if (id >= 0) {
Message.message(getApplicationContext(), "Insertion success");
} else {
Message.message(getApplicationContext(), "Insertion to database failed");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
然后,在“主要活动”中,我将创建userid.getText和password.getText并将它们与我来自sqlite的数据进行比较。我是sqlite的新手,请帮助我。
答案 0 :(得分:0)
如果存在匹配的数据,则myDbAdapter类中的以下方法将返回true,否则返回false。
public boolean compareData(String userid, String pass)
{
boolean rv = false;
SQLiteDatabase dbb = myhelper.getWritableDatabase();
String whereclause = myDbHelper.UID + "=? AND " + myDbHelper.MyPASSWORD + "=?";
String[] whereargs = new String[]{userid,pass};
Cursor csr = dbb.query(myDbHelper.TABLE_NAME,null,whereclause,whereargs,null,null,null);
if (csr.getCount() > 0) {
rv = true;
}
csr.close();
return rv;
}
您可以使用类似:-
的方法来调用它if(helper.compareData(Userid_txt.getText().toString(),Pass_txt.getText().toString()) {
.... handle data compares
} else {
.... handle data does not compare
}
您可能希望制作 Userid_txt
和 Pass_Txt
类变量,并且还希望有一种方法可以在数据被保存之后调用上述内容。输入(也许是一个按钮)。因此,假设有一个按钮,那么您可能会遇到类似:-
public class FromServer extends AppCompatActivity {
myDbAdapter helper;
EditText Userid_txt, Pass_txt;
Button doCompare;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_from_server);
Userid_txt= (EditText) findViewById(R.id.editUserID);
Pass_txt= (EditText) findViewById(R.id.editPass);
doCompare = (Button) findViewById(R.id.your_button);
helper = new myDbAdapter(this);
doCompare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helper.compareData(Userid_txt.getText().toString(),Pass_txt.getText().toString()) {
//.... handle data compared
} else {
//.... handle data doesn't compare
}
}
});
}
......