-1

我创建了一个数据库表,其中有、id和作为列。我的 xml 有 3 个元素,用户名、密码编辑文本和登录按钮。DatabaseActivity.java 中的方法进行验证。usernamepasswordemailvalidateLogin

错误

方法 getWritableDatabase() 未定义类型 DbHelper 行 ----- SQLiteDatabase db = mydb.getWritableDatabase();----

这是我的 DatabaseActivity.java 文件

    package com.login.recscores;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    @SuppressLint("Registered")
    public class DatabaseActivity extends Activity implements OnClickListener {

Button mLogin;
Button mNewUser;
Button mShowAll;
EditText mUsername;
EditText mPassword;
DbHelper mydb = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mNewUser = (Button)findViewById(R.id.sign_in_button);
    mNewUser.setOnClickListener(this);



}

public void onClick(View v) {

    switch(v.getId()){

    case R.id.sign_in_button:
        mUsername = (EditText)findViewById(R.id.email);
        mPassword = (EditText)findViewById(R.id.password);

        String uname = mUsername.getText().toString();
        String pass = mPassword.getText().toString();

        if(uname.equals("") || uname == null){
            Toast.makeText(getApplicationContext(), "email Empty", Toast.LENGTH_SHORT).show();
        }else if(pass.equals("") || pass == null){
            Toast.makeText(getApplicationContext(), "Password Empty", Toast.LENGTH_SHORT).show();
        }else{
            boolean validLogin = validateLogin(uname, pass, DatabaseActivity.this);
            if(validLogin){
                System.out.println("In Valid");
                Intent i = new Intent(DatabaseActivity.this, UserHome.class);
                startActivity(i);
                finish();
            }
        }
        break;


    }
}

    //  @SuppressWarnings("deprecation")

public boolean validateLogin(String uname, String pass, Context context) {

    mydb = new DbHelper(this);
            SQLiteDatabase db = mydb.getWritableDatabase();
    //SELECT
    String[] columns = {"_id"};

    //WHERE clause
    String selection = "username=? AND password=?";

    //WHERE clause arguments
    String[] selectionArgs = {uname,pass};

    Cursor cursor = null;
    try{
    //SELECT _id FROM login WHERE username=uname AND password=pass
    cursor = db.query(DbHelper.RECSCORES_TABLE_NAME, columns, selection, selectionArgs, null, null, null);

    //  startManagingCursor(cursor);

    }catch(Exception e){
        e.printStackTrace();
    }
    int numberOfRows = cursor.getCount();

    if(numberOfRows <= 0){

        Toast.makeText(getApplicationContext(), "Wha Failed..\nTry Again", Toast.LENGTH_SHORT).show();
        return false;
    }


    return true;
}

public void onDestroy(){
    super.onDestroy();
    mydb.close();
}
}

DbHelper.java 是

    package com.login.recscores;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;



    public class DbHelper {

private static final String DATABASE_NAME = "recscores.db";
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DbHelper";
private static final int DATABASE_VERSION = 1;
public static final String RECSCORES_TABLE_NAME = "users";
private static final String RECSCORES_TABLE_CREATE =
                "CREATE TABLE " + RECSCORES_TABLE_NAME + "(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
                "username VARCHAR NOT NULL, password VARCHAR NOT NULL, email VARCHAR NOT NULL);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DbHelper(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)
    {
    db.execSQL(RECSCORES_TABLE_CREATE);
     }

    @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 titles");
    onCreate(db);
    }
    }

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

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

    //---insert a title into the database---
    public long insertTitle(String Id, String username, String password, String email)
    {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ROWID, Id);
    initialValues.put(KEY_USERNAME, username);
    initialValues.put(KEY_PASSWORD, password);
    initialValues.put(KEY_EMAIL, email);
    return db.insert(RECSCORES_TABLE_NAME, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId)
    {
    return db.delete(RECSCORES_TABLE_NAME, KEY_ROWID +
    "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles()
    {
    return db.query(RECSCORES_TABLE_NAME, new String[] {
    KEY_ROWID,
    KEY_USERNAME,
    KEY_PASSWORD,
    KEY_EMAIL},
    null,null,null,null,null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException
    {
    Cursor mCursor =
    db.query(true, RECSCORES_TABLE_NAME, new String[] {
    KEY_ROWID,
    KEY_USERNAME,
    KEY_PASSWORD,
    KEY_EMAIL
    },
    KEY_ROWID + "=" + rowId,
    null,null,null,null,null);
    if (mCursor != null) {
    mCursor.moveToFirst();
    }
    return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String isbn,
    String title, String publisher)
    {
    ContentValues args = new ContentValues();
    args.put(KEY_USERNAME, isbn);
    args.put(KEY_PASSWORD, title);
    args.put(KEY_EMAIL, publisher);
    return db.update(RECSCORES_TABLE_NAME, args,
    KEY_ROWID + "=" + rowId, null) > 0;
    }






    }
4

1 回答 1

0

getWritableDatabase是一种方法DatabaseHelper,不是DbHelper。例如:

在您的 DbHelper.java 中,您使用 getgetWritableDatabase如下:

db = DBHelper.getWritableDatabase();

您在哪里声明:

private DatabaseHelper DBHelper;

所以你正在调用getWritableDatabase一个实例DatabaseHelper

但是在问题线上你有这个:

mydb = new DbHelper(this);
        SQLiteDatabase db = mydb.getWritableDatabase();

这是试图调用您尚未声明getWritableDatabase的 实例。DbHelper

请注意,您在DBHelper声明中调用的类中使用变量DbHelper是极端混乱的,并且可能是造成误解的原因。

于 2013-01-15T10:45:40.290 回答