我正在做一个存储用户数据的数据库,用户输入用户名密码年龄性别身高电子邮件和体重,其他列的值保留为 0.0。当我调试应用程序时,我收到以下错误 E/SQLiteLog:(1)表用户表没有名为 Monday_Calories 的列错误来自用户表,尚未调用产品表。
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
//DATABASE VERSION
private static int DATABASE_VERSION = 1;
//DATABASE NAME
private static final String DATABASE_NAME = "AppDatabase";
//TABLE NAMES
private static final String TABLE_USERS = "Users_Table";
private static final String TABLE_PRODUCTS = "Products_Table";
//COMMON COLUMN NAMES
private static final String KEY_USER_ID = "User_ID";
private static final String KEY_PRODUCT_ID = "Product_ID";
//USER TABLE
private static final String KEY_USERNAME = "Username";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_EMAIL = "Email";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
private static final String KEY_CURRENT_WEIGHT = "Current_Weight";
private static final String KEY_START_WEIGHT = "Start_Weight";
private static final String KEY_WEIGHT_CHANGE = "Weight_Change";
private static final String KEY_BMI = "BMI";
private static final String KEY_BMR = "BMR";
private static final String KEY_MON_CAL = "Monday_Calories";
private static final String KEY_TUES_CAL = "Tuesday_Calories";
private static final String KEY_WED_CAL = "Wednesday_Calories";
private static final String KEY_THUR_CAL = "Thursday_Calories";
private static final String KEY_FRI_CAL = "Friday_Calories";
private static final String KEY_SAT_CAL = "Saturday_Calories";
private static final String KEY_SUN_CAL = "Sunday_Calories";
//PRODUCT TABLE
private static final String KEY_ITEMNAME = "Item_name";
private static final String KEY_ITEMCALORIES = "Item_Calories";
//
private static final String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "( "
+ KEY_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_USERNAME + " TEXT, "
+ KEY_PASSWORD + " TEXT, "
+ KEY_AGE + " INTEGER, "
+ KEY_EMAIL + " TEXT, "
+ KEY_GENDER + " TEXT, "
+ KEY_HEIGHT + " DOUBLE, "
+ KEY_START_WEIGHT + " DOUBLE, "
+ KEY_CURRENT_WEIGHT + " DOUBLE, "
+ KEY_WEIGHT_CHANGE + " DOUBLE, "
+ KEY_BMI + " DOUBLE, "
+ KEY_BMR + " DOUBLE, "
+ KEY_MON_CAL + "DOUBLE, "
+ KEY_TUES_CAL + " DOUBLE, "
+ KEY_WED_CAL + " DOUBLE, "
+ KEY_THUR_CAL + " DOUBLE, "
+ KEY_FRI_CAL + " DOUBLE, "
+ KEY_SAT_CAL + " DOUBLE, "
+ KEY_SUN_CAL + " DOUBLE ); ";
private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "( " + KEY_PRODUCT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_ITEMNAME + " TEXT, "
+ KEY_ITEMCALORIES + " DOUBLE );";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_PRODUCT_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
DATABASE_VERSION = 2;
db.execSQL("DROP TABLE IF IT EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF IT EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//CRUD OPERATIONS
public Users getUser(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS,
new String[]{ KEY_USER_ID, KEY_USERNAME, KEY_PASSWORD, KEY_AGE,
KEY_EMAIL, KEY_GENDER, KEY_HEIGHT, KEY_START_WEIGHT,
KEY_CURRENT_WEIGHT, KEY_WEIGHT_CHANGE, KEY_BMI, KEY_BMR},
KEY_USER_ID + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor!= null)
cursor.moveToFirst();
Users users = new Users(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
cursor.getString(2), cursor.getInt(3), cursor.getString(4), cursor.getString(5),
cursor.getDouble(6), cursor.getDouble(7), cursor.getDouble(8), cursor.getDouble(9),
cursor.getDouble(10), cursor.getDouble(11), cursor.getDouble(12), cursor.getDouble(13),
cursor.getDouble(14), cursor.getDouble(15), cursor.getDouble(16), cursor.getDouble(17), cursor.getDouble(18) );
return users;
}
public void addUser(Users users){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, users.get_username());
values.put(KEY_PASSWORD, users.get_password());
values.put(KEY_AGE, users.get_age());
values.put(KEY_EMAIL, users.get_email());
values.put(KEY_GENDER, users.get_gender());
values.put(KEY_HEIGHT, users.get_height());
values.put(KEY_START_WEIGHT, users.get_startWeight());
values.put(KEY_CURRENT_WEIGHT, users.get_currentWeight());
values.put(KEY_WEIGHT_CHANGE, users.get_weightChange());
values.put(KEY_BMI, users.get_BMI());
values.put(KEY_BMR, users.get_BMR());
values.put(KEY_MON_CAL, users.get_monCal());
values.put(KEY_TUES_CAL, users.get_tuesCal());
values.put(KEY_WED_CAL, users.get_wedCal());
values.put(KEY_THUR_CAL, users.get_thurCal());
values.put(KEY_FRI_CAL, users.get_friCal());
values.put(KEY_SAT_CAL, users.get_satCal());
values.put(KEY_SUN_CAL, users.get_sunCal());
db.insert(TABLE_USERS, null, values);
db.close();
}
public int getUserCount(){
String countQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}
public int updateUser(Users users){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, users.get_username());
values.put(KEY_PASSWORD, users.get_password());
values.put(KEY_AGE, users.get_age());
values.put(KEY_EMAIL, users.get_email());
values.put(KEY_GENDER, users.get_gender());
values.put(KEY_HEIGHT, users.get_height());
values.put(KEY_START_WEIGHT, users.get_startWeight());
values.put(KEY_CURRENT_WEIGHT, users.get_currentWeight());
values.put(KEY_WEIGHT_CHANGE, users.get_weightChange());
values.put(KEY_BMI, users.get_BMI());
values.put(KEY_BMR, users.get_BMR());
values.put(KEY_MON_CAL, users.get_monCal());
values.put(KEY_TUES_CAL, users.get_tuesCal());
values.put(KEY_WED_CAL, users.get_wedCal());
values.put(KEY_THUR_CAL, users.get_thurCal());
values.put(KEY_FRI_CAL, users.get_friCal());
values.put(KEY_SAT_CAL, users.get_satCal());
values.put(KEY_SUN_CAL, users.get_sunCal());
return db.update(TABLE_USERS, values, KEY_USER_ID + " = ?",
new String[]{String.valueOf(users.get_id())});
}
public List<Users> getallUsers(){
List<Users> usersList = new ArrayList<Users>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
Users users = new Users();
users.set_id(Integer.parseInt(cursor.getString(0)));
users.set_username(cursor.getString(1));
users.set_password(cursor.getString(2));
users.set_age(cursor.getInt(3));
users.set_email(cursor.getString(4));
users.set_gender(cursor.getString(5));
users.set_height(cursor.getDouble(6));
users.set_startWeight(cursor.getDouble(7));
users.set_currentWeight(cursor.getDouble(8));
users.set_weightChange();
users.set_BMI(cursor.getDouble(10));
users.set_BMR(cursor.getDouble(11));
users.set_monCal(cursor.getDouble(12));
users.set_tuesCal(cursor.getDouble(13));
users.set_wedCal(cursor.getDouble(14));
users.set_thurCal(cursor.getDouble(15));
users.set_friCal(cursor.getDouble(16));
users.set_satCal(cursor.getDouble(17));
users.set_sunCal(cursor.getDouble(18));
usersList.add(users);
}while (cursor.moveToNext());
}
return usersList;
}
}