我收到以下错误:
08-04 20:31:10.788 30724-30724/com.lifttrackplus.jason.lifttrackplus
E/SQLiteLog﹕ (1) no such table: exercise_data_table
08-04 20:31:10.799 30724-30724/com.lifttrackplus.jason.lifttrackplus E/SQLiteDatabase﹕ Error inserting date=2015-08-04 name=bench press repCount=1 setCount=1 weight=1
android.database.sqlite.SQLiteException: no such table: exercise_data_table (code 1): , while compiling: INSERT INTO exercise_data_table(date,name,repCount,setCount,weight) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1527)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1399)
at com.lifttrackplus.jason.lifttrackplus.ExerciseDataDBHelper.addRow(ExerciseDataDBHelper.java:56)
at com.lifttrackplus.jason.lifttrackplus.AddExerciseInstanceActivity.onClick(AddExerciseInstanceActivity.java:79)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19919)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5499)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)
以下是.java文件
DBContract.java
public class DBContract implements BaseColumns {
public static final String TABLE_NAME_DATA = "exercise_data_table";
public static final String TABLE_NAME_LIST = "exercise_name_table";
public static final String COLUMN_NAME_LIST_NAME = "name";
public static final String COLUMN_NAME_DATA_NAME = "name";
public static final String COLUMN_NAME_DATA_WEIGHT = "weight";
public static final String COLUMN_NAME_DATA_SET = "setCount";
public static final String COLUMN_NAME_DATA_REP = "repCount";
public static final String COLUMN_NAME_DATA_DATE = "date";
}
ExerciseDataDBHelper.java
public class ExerciseDataDBHelper extends SQLiteOpenHelper {
private static final String TAG = "ListDBHelper";
public static final String DATABASE_NAME = "exercise_data.db";
public static final int DATABASE_VERSION = 1;
private static final String NUM_TYPE = " INTEGER";
private static final String STRING_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + DBContract.TABLE_NAME_DATA;
private static final String CREATE_TABLE = "CREATE TABLE " + DBContract.TABLE_NAME_DATA + "(" +
DBContract._ID + " INTEGER PRIMARY KEY, " +
DBContract.COLUMN_NAME_DATA_NAME + STRING_TYPE + COMMA_SEP +
DBContract.COLUMN_NAME_DATA_WEIGHT + NUM_TYPE + COMMA_SEP +
DBContract.COLUMN_NAME_DATA_SET + NUM_TYPE + COMMA_SEP +
DBContract.COLUMN_NAME_DATA_REP + NUM_TYPE + COMMA_SEP +
DBContract.COLUMN_NAME_DATA_DATE + STRING_TYPE + ")";
public ExerciseDataDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Table created");
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DELETE_TABLE);
db.execSQL(CREATE_TABLE);
}
public void addRow(SQLiteDatabase db, Exercise exercise) {
ContentValues contentValues = new ContentValues();
contentValues.put(DBContract.COLUMN_NAME_DATA_NAME, exercise.getNAME());
contentValues.put(DBContract.COLUMN_NAME_DATA_WEIGHT, exercise.getWEIGHT());
contentValues.put(DBContract.COLUMN_NAME_DATA_SET, exercise.getSET());
contentValues.put(DBContract.COLUMN_NAME_DATA_REP, exercise.getREP());
contentValues.put(DBContract.COLUMN_NAME_DATA_DATE, exercise.getDATE());
db.insert(DBContract.TABLE_NAME_DATA, null, contentValues);
}
public Exercise exerciseByName(SQLiteDatabase db, String name) {
Exercise exercise = new Exercise();
Cursor cursor = db.rawQuery("SELECT * FROM " + DBContract.TABLE_NAME_DATA + " WHERE " + DBContract.COLUMN_NAME_DATA_NAME + "=" + "'" + name + "'", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
exercise.setNAME(cursor.getString(cursor.getColumnIndex(DBContract.COLUMN_NAME_DATA_NAME)));
exercise.setWEIGHT(cursor.getInt(cursor.getColumnIndex(DBContract.COLUMN_NAME_DATA_WEIGHT)));
exercise.setSET(cursor.getInt(cursor.getColumnIndex(DBContract.COLUMN_NAME_DATA_SET)));
exercise.setREP(cursor.getInt(cursor.getColumnIndex(DBContract.COLUMN_NAME_DATA_REP)));
exercise.setDATE(cursor.getString(cursor.getColumnIndex(DBContract.COLUMN_NAME_DATA_DATE)));
cursor.moveToNext();
}
cursor.close();
return exercise;
}
}
AddExerciseInstanceActivity.java
public class AddExerciseInstanceActivity extends Activity implements View.OnClickListener {
private Button addInstanceButton;
private Button toGraphButton;
private EditText weight;
private EditText set;
private EditText rep;
private ExerciseDataDBHelper dataDBHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_exercise_instance);
addInstanceButton = (Button) findViewById(R.id.add_exercise_instance_btn);
toGraphButton = (Button) findViewById(R.id.to_graph_btn);
weight = (EditText) findViewById(R.id.weight);
set = (EditText) findViewById(R.id.set);
rep = (EditText) findViewById(R.id.rep);
dataDBHelper = new ExerciseDataDBHelper(AddExerciseInstanceActivity.this);
db = dataDBHelper.getWritableDatabase();
addInstanceButton.setOnClickListener(this);
toGraphButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(AddExerciseInstanceActivity.this, ExerciseGraphActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_exercise_instance, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onClick(View v) {
Exercise exercise = new Exercise();
exercise.setNAME(getIntent().getExtras().getString("NAME"));
exercise.setWEIGHT(Integer.parseInt(weight.getText().toString()));
exercise.setSET(Integer.parseInt(set.getText().toString()));
exercise.setREP(Integer.parseInt(rep.getText().toString()));
exercise.setDATE(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
dataDBHelper.addRow(db, exercise);
dataDBHelper.close();
db.close();
}
}
我已经尝试卸载该应用并重新启动Android Studio,但我仍然遇到同样的错误。它是CREATE_TABLE String
,因为这似乎是标准问题,但我看不出我的陈述有什么问题。我知道这可能是一个愚蠢的小错误。我今天早上开始这个项目,我觉得它需要一双新鲜的眼睛。谢谢。
答案 0 :(得分:0)
我头顶的两件事:
onUpgrade
,并在您的onUpgrade
中删除并重新创建表格。如果您这样做,您(以及使用您的应用程序的任何人)将丢失表格中的所有数据,但是从现在开始您无论如何都不能使用它。因此,一旦您投入生产,建议在if
中使用级联onUpgrade
语句,以便在需要更改内容时对数据库进行非破坏性更新,例如添加列或表答案 1 :(得分:0)
好的,这段代码没有错。我将代码复制并粘贴到另一个类中并对其进行了编辑,但是我忘了在新的帮助程序类中更改DATABASE_NAME ...我知道这样的东西很蠢。