每当我试图创建这个数据库时,它都会给出一个表ex_bottom不存在的错误。那有什么不对? 这是我的代码 -
DB_excercise_bottom类 - >
package db;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DB_Handler_exercise_bottom extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "exercise_items";
private static final int DATABASE_VERSION = 1;
//table name
private static final String TABLE_EXERCISE_BOTTOM = "exercise_bottom";
//column name
private static final String KEY_EX_ID = "ex_id";
private static final String KEY_EX_NAME = "ex_name";
private static final String KEY_EX_NICKNAME = "ex_nickname";
private static final String KEY_NO_VARIATION = "ex_variation";
public DB_Handler_exercise_bottom(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE_BOTTOM="CREATE TABLE IF NOT EXISTS " + TABLE_EXERCISE_BOTTOM+ "(" + KEY_EX_ID + " INTEGER," + KEY_EX_NAME + " TEXT," +KEY_EX_NICKNAME + " TEXT," + KEY_NO_VARIATION + " INTEGER" +")";
db.execSQL(CREATE_TABLE_BOTTOM);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public int getRecordCount() {
String countQuery = "SELECT * FROM " + TABLE_EXERCISE_BOTTOM;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
public void addData(Table_exercise_bottom bottom){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_EX_ID, bottom.getEx_id());
values.put(KEY_EX_NAME, bottom.getEx_name());
values.put(KEY_EX_NICKNAME, bottom.getEx_nickname());
values.put(KEY_NO_VARIATION, bottom.getEx_variation());
db.insert(TABLE_EXERCISE_BOTTOM, null, values);
db.close();
}
public ArrayList<Table_exercise_bottom> getAllData() {
ArrayList<Table_exercise_bottom> ex_bottom_list = new ArrayList<Table_exercise_bottom>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EXERCISE_BOTTOM;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Table_exercise_bottom ex_bottom=new Table_exercise_bottom();
ex_bottom.setEx_id(cursor.getInt(0));
ex_bottom.setEx_name(cursor.getString(1));
ex_bottom.setEx_nickname(cursor.getString(2));
ex_bottom.setEx_variation(cursor.getInt(3));
// Adding data to list
ex_bottom_list.add(ex_bottom);
} while (cursor.moveToNext());// return data list
}
return ex_bottom_list;
}
}
table_excercise_bottom class---->
package db;
public class Table_exercise_bottom {
int ex_id,ex_variation;
String ex_name,ex_nickname;
public Table_exercise_bottom() {
}
public int getEx_id() {
return ex_id;
}
public void setEx_id(int ex_id) {
this.ex_id = ex_id;
}
public int getEx_variation() {
return ex_variation;
}
public void setEx_variation(int ex_variation) {
this.ex_variation = ex_variation;
}
public String getEx_name() {
return ex_name;
}
public void setEx_name(String ex_name) {
this.ex_name = ex_name;
}
public String getEx_nickname() {
return ex_nickname;
}
public void setEx_nickname(String ex_nickname) {
this.ex_nickname = ex_nickname;
}
public Table_exercise_bottom(int ex_id, String ex_name,
String ex_nickname,int ex_variation) {
this.ex_id = ex_id;
this.ex_variation = ex_variation;
this.ex_name = ex_name;
this.ex_nickname = ex_nickname;
}
}
启动活动 - &gt;(这里我试图将数据添加到表格中)
package com.example.fitnessapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import constants.Const_String_Food;
import db.DB_Handler_exercise_bottom;
import db.DB_Handler_food;
import db.DB_Handler_exercise_top;
import db.Table_exercise_bottom;
import db.Table_exercise_top;
import db.Table_items;
public class Splash_Activity extends Activity {
public static final int Splash_Time=3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent=new Intent(Splash_Activity.this,Main_Screen.class);
DB_Handler_food db1 = new DB_Handler_food(Splash_Activity.this);
Const_String_Food con=new Const_String_Food();
if(db1.getRecordCount()==0){
//adding data in database food
db1.addData(new Table_items("Eggs",con.egg_benefits , con.egg_positives,"eggs" ));
db1.addData(new Table_items("Brown Rice",con.brown_rice_benefits,con.brown_rice_positives,"brown_rice"));
db1.addData(new Table_items("Black Beans",con.black_beans_benefits,con.black_beans_positives,"black_beans"));
db1.addData(new Table_items("Oats",con.oats_benefits,con.oats_positives,"oats"));
db1.addData(new Table_items("Avocados",con.avocados_benefits,con.avocados_positives,"avocado"));
db1.addData(new Table_items("Salmon",con.salmon_benefits,con.salmon_positives,"salmon"));
db1.addData(new Table_items("Blueberries",con.blueberries_benefits,con.blueberries_positives,"black_berry"));
db1.addData(new Table_items("Broccoli",con.broccoli_benefits,con.broccoli_positives,"brocolli"));
db1.addData(new Table_items("Pears",con.pears_benefits,con.pears_positives,"pears"));
}
DB_Handler_exercise_top db2=new DB_Handler_exercise_top(Splash_Activity.this);
if(db2.getRecordCount()==0){
//adding data in database exercise_top
db2.addData(new Table_exercise_top(1,"Presses","",0));
db2.addData(new Table_exercise_top(2,"Triceps","",2));
db2.addData(new Table_exercise_top(3,"Bicepts","",2));
db2.addData(new Table_exercise_top(4,"Bridge","",0));
db2.addData(new Table_exercise_top(5,"Plank","",0));
db2.addData(new Table_exercise_top(6,"Shoulders","",0));
db2.addData(new Table_exercise_top(7,"Chest","",2));
db2.addData(new Table_exercise_top(8,"Back","",0));
}
DB_Handler_exercise_bottom db3=new DB_Handler_exercise_bottom(Splash_Activity.this);
if(db3.getRecordCount()==0){
//adding data in database exercise_bottom
db3.addData(new Table_exercise_bottom(1,"Squats","",3));
db3.addData(new Table_exercise_bottom(2,"leg lifts","",3));
db3.addData(new Table_exercise_bottom(3,"lunges","",3));
db3.addData(new Table_exercise_bottom(4,"Inner thighs","",0));
db3.addData(new Table_exercise_bottom(5,"Roll Ups","",2));
db3.addData(new Table_exercise_bottom(6,"Side Situps","",0));
db3.addData(new Table_exercise_bottom(7,"bicycle","",3));
}
DB_exercise_cardio db4=new DB_exercise_cardio(Splash_Activity.this);
if(db4.getRecordCount()==0){
//adding data in database exercise_cardio
db4.addData(new Table_exercise_cardio(1,"Walk","",0));
db4.addData(new Table_exercise_cardio(2,"Taps","",0));
db4.addData(new Table_exercise_cardio(3,"Step","",0));
db4.addData(new Table_exercise_cardio(4,"Knee Lifts","",0));
db4.addData(new Table_exercise_cardio(5,"Boxing","",0));
db4.addData(new Table_exercise_cardio(6,"Dance","",0));
}
startActivity(intent);
finish();
}
},Splash_Time);
}
}
logs---->
07-22 13:08:32.826: E/AndroidRuntime(20241): FATAL EXCEPTION: main
07-22 13:08:32.826: E/AndroidRuntime(20241): Process: com.example.fitnessapp, PID: 20241
07-22 13:08:32.826: E/AndroidRuntime(20241): android.database.sqlite.SQLiteException: no such table: exercise_bottom (code 1): , while compiling: SELECT * FROM exercise_bottom
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
07-22 13:08:32.826: E/AndroidRuntime(20241): at db.DB_Handler_exercise_bottom.getRecordCount(DB_Handler_exercise_bottom.java:42)
07-22 13:08:32.826: E/AndroidRuntime(20241): at com.example.fitnessapp.Splash_Activity$1.run(Splash_Activity.java:60)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.os.Handler.handleCallback(Handler.java:733)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.os.Handler.dispatchMessage(Handler.java:95)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.os.Looper.loop(Looper.java:136)
07-22 13:08:32.826: E/AndroidRuntime(20241): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-22 13:08:32.826: E/AndroidRuntime(20241): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 13:08:32.826: E/AndroidRuntime(20241): at java.lang.reflect.Method.invoke(Method.java:515)
07-22 13:08:32.826: E/AndroidRuntime(20241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-22 13:08:32.826: E/AndroidRuntime(20241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-22 13:08:32.826: E/AndroidRuntime(20241): at dalvik.system.NativeStart.main(Native Method)