我的数据库出现问题:“递归调用了getDatabase”

时间:2019-06-02 12:38:16

标签: android android-sqlite

我正在用android studio创建一个sqlite数据库。我的应用程序在主程序中调用我的方法 Insertprofile(); 时关闭。

  • 您能帮我发现问题吗?
  • 现在如何操作,以查看我们的表格(Android设备监视器已在Android Studio 3.1中弃用,现在已从Android Studio 3.2中删除)。

我的数据库管理器:

public class DatabaseManager extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "Userx.db";
    private static final int DATABASE_VERSION = 1;
SQLiteDatabase db;
    public DatabaseManager( Context context ) {
        super( context, DATABASE_NAME, null, DATABASE_VERSION );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String strSql = "create table User ("
                + "    id integer primary key autoincrement,"
                + "    mission integer not null,"
                + "    day integer not null"
                + ")";
        db.execSQL( strSql );
        Log.i( "DATABASE", "onCreate invoked" );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insertProfil( int mission, int day ) {
        //name = name.replace( "'", "''" );
        String strSql = "INSERT INTO User (mission, day) VALUES ("
                + mission + ", " + day + ")";
        this.getWritableDatabase().execSQL( strSql );
        Log.i( "DATABASE", "insertScore invoked" );
    }

    public Profil readData() {
        Profil profil = null;
        //REQUEST
        String strSql = "SELECT * from User";
        //CREATE CURSOR WITH REQUEST
        Cursor cursor = this.getReadableDatabase().rawQuery(strSql, null);
        //PUT IN TOP
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            //RECEPT DATA AND CREATE VALUES THIS
            Integer id = cursor.getInt(0);
            Integer mission = cursor.getInt(1);
            Integer day = cursor.getInt(2);
            profil = new Profil(id, mission, day);
            //POSSIBLE CREATE LIST WITH WHILE
        }
        cursor.close();
        return profil;
    }
    public void modifyData(int mission){
        SQLiteDatabase db = this.getWritableDatabase();
        String strSql = "UPDATE User SET mission = " + mission + " WHERE id = " + "1";
        db.execSQL( strSql );
    }
    public void deleteData(){

        String strSql = "DELETE FROM User" ;
        this.getWritableDatabase().execSQL( strSql );
    }
}

我的主要联系人:

public class MainActivity extends AppCompatActivity {

private TextView profil_text;
private DatabaseManager databaseManager;
Profil profil;

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

    profil_text = (TextView) findViewById( R.id.profil_text);
    Log.d("**************1", "onCreate: ");
    databaseManager = new DatabaseManager(this);
    Log.d("**************2", "onCreate: ");
    databaseManager.insertProfil(2,3);
    Log.d("**************3", "onCreate: ");
    databaseManager.modifyData(80);
    Log.d("**************4", "onCreate: ");
    profil = databaseManager.readData();
    Log.d("**************5", "onCreate: ");
    profil_text.setText(Integer.toString(profil.getMission()));
    databaseManager.close();
}

}

我的类配置文件只是具有getter和setter和构造函数的简单类。

我的错误日志:

2019-06-02 14:45:41.142 19652-19652/com.example.pc.sqlite3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pc.sqlite3, PID: 19652
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pc.sqlite3/com.example.pc.sqlite3.MainActivity}: java.lang.IllegalStateException: getDatabase called recursively
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.IllegalStateException: getDatabase called recursively
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at com.example.pc.sqlite3.DatabaseManager.deleteData(DatabaseManager.java:75)
    at com.example.pc.sqlite3.DatabaseManager.onUpgrade(DatabaseManager.java:31)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at com.example.pc.sqlite3.DatabaseManager.insertProfil(DatabaseManager.java:44)
    at com.example.pc.sqlite3.MainActivity.onCreate(MainActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

PS:我是法国人,我的英语不好

2 个答案:

答案 0 :(得分:1)

尝试修改以下方法,使用ContentValue对象可以在SQLite数据库中插入数据

   //add below line inside your DatabaseManager class method in onUpgrade()
    db.execSQL("DROP TABLE IF EXISTS User");


   // the below method will return boolean method
   public boolean insertProfil( int mission, int day ) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("mission", mission);
    contentValues.put("day", day);
    long result = db.insert("User", null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

 // call above method from your activity
  boolean status=databaseManager.insertProfil( 2,6);
  if(status){
   Log.d("insert_","success");
 }else{
   Log.d("insert_","fail");
 }

答案 1 :(得分:0)

尝试将您的insertProfil()方法更改为...

public void insertProfil(int mission, int day, SQLiteDatabase db)

...然后在onCreate(...)中只需将db参数传递给它并更改此行...

this.getWritableDatabase().execSQL( strSql );

然后您的代码应如下所示……

public void insertProfil(int mission, int day, SQLiteDatabase db) {
    //name = name.replace( "'", "''" );
    String strSql = "INSERT INTO User (mission, day) VALUES ("
            + mission + ", " + day + ")";
    db.execSQL( strSql );
    Log.i( "DATABASE", "insertScore invoked" );
}

您现有代码中的问题是,正在将onCreate(...)传递给打开/可写数据库的引用,但是它随后调用insertProfil(...),试图获取对数据库的另一个可写引用。 / p>