这是在Android中使用Sqlite数据库的正确方法吗?

时间:2016-01-16 06:43:41

标签: java android sqlite android-studio android-sqlite

我是Android的初学者。现在我正在创建一个与Sqlite数据库集成的注册表单。但是当我点击注册按钮时它会抛出错误。我的代码出了什么问题?如何在Android中调试我的应用程序?我正在使用Gennymotion Emulator。

这是我的数据库助手类DatabaseHelper.java:

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_PASSWORD = "password";
    SQLiteDatabase db;
    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null auto_increment,"+
            "name text not null , password text not null);";

    public DatabaseHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        db.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);
    }

    public  void insertContact(Contact c)
    {
        db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME,c.getName());
        values.put(COLUMN_PASSWORD,c.getPassword());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public  String getUsers()
    {
        String users = "";
        db = this.getReadableDatabase();
        String query = "SELECT name,password FROM "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query,null);
        if(cursor.moveToFirst())
        {
            do{
                users = users + cursor.getString(0)+":"+cursor.getString(1)+" ";
            }while(cursor.moveToNext());
        }
        return users;
    }
}

这是我的注册活动:

public class RegisterActivity extends Activity {

    DatabaseHelper helper = new DatabaseHelper(this);
    Button registerBtn;

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

        registerBtn = (Button)findViewById(R.id.btn_register);
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerBtnClick(v);
            }
        });
    }

    public void registerBtnClick(View v)
    {
        String name = ((EditText)findViewById(R.id.tf_name_register)).getText().toString();
        String password = ((EditText)findViewById(R.id.tf_password_register)).getText().toString();
        String confirm_password = ((EditText)findViewById(R.id.tf_confirm_password_register)).getText().toString();

        if(name.isEmpty())
        {
            Toast.makeText(getBaseContext(),"Name should not be empty",Toast.LENGTH_LONG).show();
        }
        else if(password.isEmpty())
        {
            Toast.makeText(getBaseContext(),"Password is required",Toast.LENGTH_LONG).show();
        }
        else if(!password.equals(confirm_password))
        {
            Toast.makeText(getBaseContext(),"Password does not match",Toast.LENGTH_LONG).show();
        }
        else{
            //save to database
             Contact c = new Contact();
            c.setName(name);
            c.setPassword(password);
            helper.insertContact(c);

        }
    }
}

当我点击注册按钮时,它会抛出错误。我怎样才能发现错误?

1 个答案:

答案 0 :(得分:1)

下面:

DatabaseHelper helper = new DatabaseHelper(this);

此行导致问题,因为在创建它之前访问活动上下文。

在任何其他内容中使用thisRegisterActivity.this来获取有效的上下文。

在类级别声明helper并在onCreate方法中将其初始化为:

 DatabaseHelper helper ;//>> declare here
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        // initialize `helper` here
        helper = new DatabaseHelper(this);
       ....
}