SQLiteLog:没有这样的表

时间:2016-08-09 17:31:56

标签: android sqlite tablename

关于我的应用程序:保存培训性能 - 一个包含许多表的数据库(一个表是锻炼日志)。日期是表格的名称(例如d2016_08_09)。当用户按下"保存按钮"时,app会设置表的名称。

public static final String DB_NAME ="db_workout";//name of base
public static  String DB_TABLE ="dateOfWorkout";//name of table
public static final int DB_VER =1;

public static void setDbTable(String dbTable) {
    DB_TABLE = dbTable;
}

public Base(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, String db_table_name) {
    super(context, DB_NAME, null, DB_VER);
    setDbTable(db_table_name);
    System.out.println(DB_TABLE);
}


@Override


public void onCreate(SQLiteDatabase db){
    db.execSQL(
            "create table IF NOT EXISTS "+ DB_TABLE +"("
            + "nr integer primary key autoincrement,"
            + "name text,"
            + "s1 integer,"
            + "s2 integer,"
            + "s3 integer,"
            + "s4 integer,"
            + "s5 integer,"
            + "weight integer);"
            +"");

}

public void addWorkoutPlan(String i, int q, int w, int e, int r, int t, int wt) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues wartosci = new ContentValues();

    wartosci.put("name", i);
    wartosci.put("s1", q);
    wartosci.put("s2", w);
    wartosci.put("s3", e);
    wartosci.put("s4", r);
    wartosci.put("s5",t);
    wartosci.put("weight",wt);



    db.insertOrThrow(DB_TABLE, null, wartosci);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //uzupelnic

}

和我使用的类添加日期基础

sNameTable = "d"+sNameTable;
    db= new Base(this,null,null,0,sNameTable);

    bAddDate = (Button) findViewById(R.id.bAddDate);



    bAddDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            setValues(); // setting values 





                for (int i = 0; i < 1; i++) {

                    db.addWorkoutPlan(
                            tName[0][i].toString(),//nazwa cwiczenia
                            Integer.parseInt(tSeries[0][i].getText().toString()),//1. seria
                            Integer.parseInt(tSeries[1][i].getText().toString()),
                            Integer.parseInt(tSeries[2][i].getText().toString()),
                            Integer.parseInt(tSeries[3][i].getText().toString()),
                            Integer.parseInt(tSeries[4][i].getText().toString()),
                            Integer.parseInt(tWeight[0][i].getText().toString())

                    );
                }

                Context context = getApplicationContext();
                CharSequence text = "Trening dodany";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();


        }
    });
}

当我按下按钮添加日期,应用程序崩溃和AndroidStudio获取此日志:

  

23190-23190 / com.example.konra_000.workoutplan E / SQLiteLog:(1)没有这样的表:d2016_08_09

     

23190-23190 / com.example.konra_000.workoutplan   E / AndroidRuntime:致命异常:主要                                                                                      处理:com.example.konra_000.workoutplan,PID:23190                                                                                      android.database.sqlite.SQLiteException:没有这样的表:d2016_08_09   (代码1):,编译时:INSERT INTO   d2016_08_09(名称,s3,权重,s2,s1,s5,s4)值(?,?,?,?,?,?,?)

1 个答案:

答案 0 :(得分:0)

当您对SQLite数据库进行一些更改时,有时无法看到更改,因为您创建数据库的逻辑未经过优化。

因此,您的设备上可能存在较旧版本的数据库,只需卸载并重新安装该应用即可。