登录验证问题

时间:2014-05-15 06:58:38

标签: android database sqlite

您好我正在尝试使用我已存储在SQLite数据库中的数据(我成功注册的地方)登录我的应用程序。我一直在努力编写我的登录页面。我想要的就是能够使用我的数据库中的匹配数据进行登录。

如果有人可以帮助我编写代码片段,那将是很棒的,因为我是这些Android开发领域的初学者。我也不是一个非常聪明的编程学生。谢谢!

这是我的登录类:

 public class Login extends Activity{
    DBAdapter db = new DBAdapter(this);
    Button btnLogin;
    EditText NRIC,Password;
    Editable d1,d2 = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }

    //Event handler from Login page to Homepage
        public void buttonOnClick (View v) {
            btnLogin = (Button)  findViewById(R.id.btnLogin);
            btnLogin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    d1 = NRIC.getText();
                    d2 = Password.getText();
                    try{

                        Cursor c = db.getRecord("SELECT * FROM assignments", null);
                        if(c!= null){
                            if (c.moveToFirst()) {
                                do {
                                    //whole data of column is fetched by getColumnIndex()
                                    String nric =c.getString(c.getColumnIndex("custNRIC"));
                                    String password =c.getString(c.getColumnIndex("custPassword"));
                                    System.out.println(nric);
                                    System.out.println(password);
                                }while(c.moveToNext());}
                            //count the total number of entries
                            Integer a =  c.getCount();
                            System.out.println(a);
                            //db1.close();
                            //if you close the database then illegal exception will be occurred...
                        }} catch(Exception e){
                        System.out.println(e);
                        startActivity(new Intent(getApplicationContext(), Homepage.class));
                    }
                }
            });

            }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的DBAdapter:

    public class DBAdapter {

    public static final String KEY_ROWID = "custId";
    public static final String KEY_NAME = "custName";
    public static final String KEY_PHONE = "custPhone";
    public static final String KEY_GENDER = "custGender";
    public static final String KEY_DOB = "custDOB";
    public static final String KEY_ADDRESS = "custAddress";
    public static final String KEY_NRIC = "custNRIC";
    public static final String KEY_EMAIL = "custEmail";
    public static final String KEY_PASSWORD = "custPassword";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "powerfood";
    private static final String DATABASE_TABLE = "assignments";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE =
            "create table if not exists assignments (id integer primary key autoincrement, "
                    + "custName VARCHAR not null, custPhone VARCHAR, custGender VARCHAR, custDOB VARCHAR, custAddress VARCHAR, custNRIC VARCHAR, custEmail VARCHAR, custPassword VARCHAR );";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS customer");
            onCreate(db);
        }
    }

    //---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }

    //---insert a record into the database---
    public long insertRecord(String custName, String custPhone, String custGender, String custDOB, String custAddress, String custNRIC, String custEmail, String custPassword)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, custName);
        initialValues.put(KEY_PHONE, custPhone);
        initialValues.put(KEY_GENDER, custGender);
        initialValues.put(KEY_DOB, custDOB);
        initialValues.put(KEY_ADDRESS, custAddress);
        initialValues.put(KEY_NRIC, custNRIC);
        initialValues.put(KEY_EMAIL, custEmail);
        initialValues.put(KEY_PASSWORD, custPassword);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
//---retrieves a particular record---
    public Cursor getRecord(long rowId) throws SQLException
    {
        Cursor mCursor =
                db.query(DATABASE_TABLE, new String[] {KEY_NRIC, KEY_PASSWORD},
                        KEY_NRIC + "=?",
                        new String[] { String.valueOf(rowId)}, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

这是我的Logcat显示的内容。

Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
C:\Users\L33545\Downloads\Powerfood2014\app\src\main\java\com\powerfood2014\app\Login.java
error: method getRecord in class DBAdapter cannot be applied to given types;
required: long
found: String,<null>
reason: actual and formal argument lists differ in length

0 个答案:

没有答案