我正在创建一个日志应用程序,用户在其中输入用户名和密码。 如果数据库中存在用户名和密码,则用户将通过意图转到新页面,否则将引发错误。
主页
package com.loginappdemo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainPage extends Activity {
EditText un, pw;
Button login, register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DataBaseHandler db = new DataBaseHandler(this);
un = (EditText) findViewById(R.id.username);
pw = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a=un.getText().toString();
String b=pw.getText().toString();
String data=db.selectData(a, b);
}
});
register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainPage.this, NewLogin.class);
startActivity(i);
}
});
}
}
DATABASEHANDLER CLASS
package com.loginappdemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHandler extends SQLiteOpenHelper {
private static final String username = "username";
private static final String password = "password";
private static final String id = "id";
private static final String db_name = "LOGINDEMO";
private static final String table_name = "LOGINDETAILS";
private static final int db_version = 1;
private static final String create_table = " CREATE TABLE " + table_name
+ "(" + id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + username
+ " VARCHAR(200)," + password + " VARCHAR(200) " + " ) ";
public DataBaseHandler(Context context) {
super(context, db_name, null, db_version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(create_table);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public long insertData(String name,String password1){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(username, name);
cv.put(password, password1);
long id=db.insert(table_name, null, cv);
return id;
}
public String selectData(String name,String password1){
SQLiteDatabase db=getWritableDatabase();
String[] col={ username, password};
String[] args={ name, password1};
StringBuffer b=new StringBuffer();
Cursor cursor=db.query(table_name, col, username + " =? AND " +password+ " =? ", args, null, null, null);
String s1= cursor.getString(1);
String s2=cursor.getString(2);
b.append(s1 + " " + s2 + "\n" );
return b.toString();
}
}
答案 0 :(得分:1)
你可以这样做,
public int selectData(String name,String password1)
{
SQLiteDatabase db=getWritableDatabase();
String[] col={ username, password};
String[] args={ name, password1};
StringBuffer b=new StringBuffer();
Cursor cursor=db.query(table_name, col, username + " =? AND " +password+ " =? ", args, null, null, null);
cursor.moveToFirst();
return cursor.getCount();
}
在onClick()
方法
if ( db.selectData(a, b) > 0 )
{
// Launch second Activity
}
else
{
// Display Toast Msg "Invalid Username/Password";
}
答案 1 :(得分:1)
尝试这种方式,
boolean res = dbh.GetData(s1, s2);
if(res == true) {
Intent in=new Intent(LoginActivity.this,nextActivity.class);
startActivity(in);
}
else {
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
dbh
是您的DataBaseHandler
类对象,然后是
public boolean GetData(String uname, String pass)
{
SQLiteDatabase dbc = this.getReadableDatabase();
Cursor c1 = dbc.query(
TABLE_MIS2,
null,
DatabaseHandler.KEY_USERID + "=? AND " + DatabaseHandler.KEY_PWD + "=?",
new String[] { uname, pass },
null,
null,
null);
return (c1.getCount() >= 1);
}
用变量替换变量名,然后尝试。