如何从数据库中获取数据条件输入sqlite android中的edittext?

时间:2014-11-28 08:18:22

标签: android sqlite

我有两个edittexts和一个按钮。按下按钮时,如果表中存在两个edittext的字符串值,则通知:您登录成功! 这是我的代码,但没有工作。帮帮我!谢谢

final String sql_selectLogin = 
    "select * from sinhvien where username= ? and password=?";

btOK.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Cursor cs = data.rawQuery(sql_selectLogin, new String[] {username, password});
        // TODO Auto-generated method stub
        if (cs.getCount() == 1)
            Toast.makeText(
                MainActivity.this,
                "You just have succesfull logined by username= " + 
                edit_username.getText().toString(),
                Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(
                MainActivity.this, 
                "You fail login",
                Toast.LENGTH_SHORT).show();
    }
});

编辑

这是我的完整代码:

public class MainActivity extends ActionBarActivity {
    EditText edit_username, edit_password;
    Button btOK;
    SQLiteDatabase data;
    String dataname = "login.db";
    String tablename = "sinhvien";
    String colnameID = "id";
    String colnameUser = "username";
    String colnamePassword = "password";
    String sql_createTable = "create table sinhvien(id integer primary key autoincrement,"
            + "username nvarchar(50) not null, password nvarchar(50) not null);";
    String sql_insertTable = "insert into sinhvien(username,password) values('hiunicy','hiunicy123');";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit_username = (EditText) findViewById(R.id.editUsername);
        edit_password = (EditText) findViewById(R.id.editPassword);
        btOK = (Button) findViewById(R.id.btOk);
        final String username = edit_username.getText().toString();
        final String password = edit_password.getText().toString();

        final SQLiteDatabase data = openOrCreateDatabase("sinhvien.db",
                MODE_PRIVATE, null);

         data.execSQL(sql_createTable);
         ContentValues values = new ContentValues();
         values.put(colnameUser, "hiunicy1");
         values.put(colnamePassword, "hiunicy1233");
         data.insert("sinhvien", null, values);
        final String sql_selectLogin = "select * from sinhvien where username=? and password=?";

        btOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Cursor cs = data.rawQuery(sql_selectLogin, new String[] {
                        username, password });
                // TODO Auto-generated method stub
                if (cs.getCount() > 0)
                    Toast.makeText(
                            MainActivity.this,
                            "You just have succesfull logined by username= "
                                    + edit_username.getText().toString(),
                            Toast.LENGTH_SHORT).show();    
                else    
                    Toast.makeText(MainActivity.this, "You fail login",
                            Toast.LENGTH_SHORT).show();    
            }
        });    
    }    
}

2 个答案:

答案 0 :(得分:0)

Cursor mCursor = db.rawQuery("select * from  sinhvien 
             where   username =?  and password=?" , new String[] { username,password});

    if (mCursor != null) {
        if (mCursor.getCount() > 0) {
             Toast.makeText(
                    MainActivity.this,
                    "You just have succesfull logined by username= "
                            + edit_username.getText().toString(),
                    Toast.LENGTH_SHORT).show();}


   else{
            Toast.makeText(MainActivity.this, "You fail login",
                    Toast.LENGTH_SHORT).show();}}

这样做。

答案 1 :(得分:-1)

你可以做这样的事情

Cursor findEntry = db.query("sinhvien", columns, "username=? and password=?", new String[] { username, password}, null, null, null);