getDatabase被称为递归sqlite insert和getReadableDatabase()

时间:2016-02-22 08:35:04

标签: java android sqlite

我刚开始使用我的第一个应用程序而我正在尝试创建一个表设置,其中应包含一个条目PIN。之后我想运行方法getPIN()来获取此设置的值。

问题是,我总是得到错误“getDatabase”(递归调用)。正如我刚开始的那样,我不知道如何修复它。我见过很多人曾经遇到过同样的问题,但解决方案并不适用于我。 你能不能帮我理解我做错了什么。

联系人表格是我之前遵循的教程的一部分,这就是它仍然在那里的原因。

谢谢。

这是我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION =3;
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_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts (id integer primary key not null ," +
        "name text not null, email text not null, pass text not null);";
private static final String TABLE_SETTINGS = "create table settings (id integer primary key not null ," +
        "setting text not null, value integer not null);";





public DatabaseHelper(Context context){
    super(context, DATABASE_NAME, null,DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db){
    String ssetting = "PIN";
    db.execSQL(TABLE_CREATE);
    db.execSQL(TABLE_SETTINGS);

    db=this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "Select * from settings";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    values.put("id",count);
    values.put("setting", ssetting);
    values.put("value", 0);

    db.insert("settings", null, values);
    db.close();

    this.db=db;
}
@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 int insertContact(Contact c){
    db=this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "Select * from contacts";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    values.put(COLUMN_ID,count);
    values.put(COLUMN_NAME, c.getName());
    values.put(COLUMN_EMAIL, c.getEmail());
    values.put(COLUMN_PASS, c.getPass());

    db.insert(TABLE_NAME, null, values);
    db.close();
    return 0;
}
public String searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = "select name, pass from "+TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    String a,b;
    b="not found";
    if (cursor.moveToFirst()){
        do {
            a = cursor.getString(0);
            b = cursor.getString(1);

            if (a.equals(uname)) {
                b = cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());
    }
    db.close();
    return b;
}
public int getPIN()
{
    db = this.getReadableDatabase();
    int pin=1;
    String query = "select id, setting, value from settings where setting='PIN'";
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()){
        do {
            pin = cursor.getInt(2);
            break;
        }
        while(cursor.moveToNext());
    }
    db.close();
    return pin;
}

}

和MainActivity

public class MainActivity extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int pin = helper.getPIN();
    if (pin==0){
        Intent i = new Intent(MainActivity.this, Loginpin.class);
        startActivity(i);
    }
    if (pin==1) {
        Intent i = new Intent(MainActivity.this, Setuppin.class);
        startActivity(i);
    }
    //pin = helper.getPIN();
}
public void b_newOnClick(View v) {
    if (v.getId() == R.id.blogin)
    {
        EditText user = (EditText) findViewById(R.id.tfuser);
        String su = user.getText().toString();
        EditText pass = (EditText) findViewById(R.id.tf_pw);
        String spass = pass.getText().toString();

        String password = helper.searchPass(spass);
        if(spass.equals(password))
        {
            Intent i = new Intent(MainActivity.this, Welcome.class);
            i.putExtra("username", su);
            startActivity(i);
        }
        else{
        Toast.makeText(MainActivity.this, "Password incorrect", Toast.LENGTH_LONG).show();
    }
    }


}
public void onSignClick(View v) {
    if (v.getId() == R.id.bsign) {
        Intent i = new Intent(MainActivity.this, SignUp.class);
        startActivity(i);

    }
}

}

非常感谢你。

问候,S Kilimanscharo

1 个答案:

答案 0 :(得分:4)

请勿通过调用{getWritableDatabase()getReadableDatabase()之类的SQLiteOpenHelper生命周期方法致电onCreate()onUpgrade() {1}}。相反,请使用作为这些方法的参数的get...Database()对象。

具体而言,请从SQLiteDatabase

中删除此行
onCreate()