我正在做一个项目,我有两个表,一个存储应用程序的用户,另一个存储用户输入的产品。用户表工作正常但是当我查看我的logcat时,我发现没有产品进入产品表。数据库处理程序中的addproduct方法没有错误,所以我想知道我做错了什么?是数据库处理程序中的问题还是我添加产品的方式?任何帮助将非常感激? 数据库处理程序代码:
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 com.example.john.fitnessapp.Products;
import com.example.john.fitnessapp.Users;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String TAG = "DBHelper";
//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_REQ_CAL = "Required_Calories";
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_REQ_CAL + " 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) {
Log.w(TAG,
"Upgrading the database from version " + oldVersion + " to " + 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_REQ_CAL, KEY_MON_CAL, KEY_TUES_CAL, KEY_WED_CAL,
KEY_THUR_CAL, KEY_FRI_CAL, KEY_SAT_CAL, KEY_SUN_CAL},
KEY_USER_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null && cursor.getCount() > 0)
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), cursor.getDouble(19));
cursor.close();
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_REQ_CAL, users.get_reqCal());
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_REQ_CAL, users.get_reqCal());
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_reqCal(cursor.getDouble(12));
users.set_monCal(cursor.getDouble(13));
users.set_tuesCal(cursor.getDouble(14));
users.set_wedCal(cursor.getDouble(15));
users.set_thurCal(cursor.getDouble(16));
users.set_friCal(cursor.getDouble(17));
users.set_satCal(cursor.getDouble(18));
users.set_sunCal(cursor.getDouble(19));
usersList.add(users);
} while (cursor.moveToNext());
}
return usersList;
}
public List<Products> getallProducts(){
ArrayList<Products> productList = new ArrayList<Products>();
String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do {
Products products = new Products();
products.set_id(cursor.getInt(0));
products.set_itemName(cursor.getString(1));
products.set_itemCalorie(cursor.getDouble(2));
}while (cursor.moveToNext());
}
cursor.close();
return productList;
}
public void addProduct(Products products){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ITEMNAME, products.get_itemName());
values.put(KEY_ITEMCALORIES, products.get_itemCalorie());
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
public int getProductCount(){
String countQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}
答案 0 :(得分:0)
我希望产品列表插入到表格中。但是您错过了将产品添加到列表中
public List<Products> getallProducts() {
ArrayList<Products> productList = new ArrayList<Products>();
String selectQuery = "SELECT * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Products products = new Products();
products.set_id(cursor.getInt(0));
products.set_itemName(cursor.getString(1));
products.set_itemCalorie(cursor.getDouble(2));
productList.add(产品);
} while (cursor.moveToNext());
}
cursor.close();
return productList;
}