Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我无法将数据插入我的SQLITE数据库_Android_Sqlite - Fatal编程技术网

Android 我无法将数据插入我的SQLITE数据库

Android 我无法将数据插入我的SQLITE数据库,android,sqlite,Android,Sqlite,我正在Android Studio中创建一个SQLiteDatabase,用于加载和计算食物热量,在按下submit应用程序后出现以下错误: 2020-02-06 21:36:47.840 26379-26379/?E/SQLiteLog:1 table food\u tbl没有名为Carries的列 2020-02-06 21:36:47.840 26379-26379/? E/SQLiteDatabase:插入name=beer carries=567 recorddate=15810430

我正在Android Studio中创建一个SQLiteDatabase,用于加载和计算食物热量,在按下submit应用程序后出现以下错误:

2020-02-06 21:36:47.840 26379-26379/?E/SQLiteLog:1 table food\u tbl没有名为Carries的列 2020-02-06 21:36:47.840 26379-26379/? E/SQLiteDatabase:插入name=beer carries=567 recorddate=158104307840时出错 android.database.sqlite.SQLiteException:table food_tbl没有名为carries code 1:的列,编译时:INSERT INTO food_tblname,carries,recorddate value

****以下是我的DatabaseHandler代码:****

公共类DatabaseHandler扩展了SQLiteOpenHelper{

private final ArrayList<Food> foodList = new ArrayList<>();


public DatabaseHandler(Context context) {
    super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // create table
    String CREATE_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
            + Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.FOOD_NAME +
            " TEXT, " + Constants.FOOD_CALORIES_NAME + " INT, " + Constants.DATE_NAME + " LONG);";

    db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(" DROP TABLE IF EXISTS " + Constants.TABLE_NAME);


    // create a new one
    onCreate(db);

}

// Get total items saved
public int getTotalItems() {
    int totalItems = 0;

    String query = " SELECT * FROM " + Constants.TABLE_NAME;
    SQLiteDatabase dba = this.getReadableDatabase();
    Cursor cursor = dba.rawQuery(query, null);

    totalItems = cursor.getCount();

    cursor.close();

    return totalItems;
}

//get total calories consumed
public int totalCalories(){
    int cals = 0;

    SQLiteDatabase dba = this.getReadableDatabase();

    String query = "SELECT SUM( " + Constants.FOOD_CALORIES_NAME + " ) " +
            "FROM " + Constants.TABLE_NAME;

    Cursor cursor = dba.rawQuery(query, null);

    if (cursor.moveToFirst()){
        cals = cursor.getInt(0);
    }

    cursor.close();
    dba.close();


    return cals;
}

//delete food item
public void deleteFood(int id) {

    SQLiteDatabase dba = this.getWritableDatabase();
    dba.delete(Constants.TABLE_NAME, Constants.KEY_ID + " = ?",
            new String[]{ String.valueOf(id)});

    dba.close();
}

//add content to db - add food
public void addFood(Food food) {

    SQLiteDatabase dba = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constants.FOOD_NAME, food.getFoodName());
    values.put(Constants.FOOD_CALORIES_NAME, food.getCalories());
    values.put(Constants.DATE_NAME, System.currentTimeMillis());

    dba.insert(Constants.TABLE_NAME, null, values);

    Log.v("Added Food item", "Yesss!!");

    dba.close();
}

// Get all foods
public ArrayList<Food> getFoods() {

    foodList.clear();

    SQLiteDatabase dba = this.getReadableDatabase();

    Cursor cursor = dba.query(Constants.TABLE_NAME,
            new String[]{Constants.KEY_ID, Constants.FOOD_NAME, Constants.FOOD_CALORIES_NAME,
            Constants.DATE_NAME}, null, null, null, null, Constants.DATE_NAME + " DESC ");

    // loop through...
    if (cursor.moveToFirst()) {
        do {

            Food food = new Food();
            food.setFoodName(cursor.getString(cursor.getColumnIndex(Constants.FOOD_NAME)));
            food.setCalories(cursor.getInt(cursor.getColumnIndex(Constants.FOOD_CALORIES_NAME)));
            food.setFoodId(cursor.getInt(cursor.getColumnIndex(Constants.KEY_ID)));

            DateFormat dateFormat = DateFormat.getDateInstance();
            String date = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Constants.DATE_NAME))).getTime());

            food.setRecordDate(date);

            foodList.add(food);

        }while (cursor.moveToNext());
    }

    cursor.close();
    dba.close();

    return foodList;
}
}试试这个

String CREATE_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
            + Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.FOOD_NAME +
            " TEXT, " + Constants.FOOD_CALORIES_NAME + " INT, " + Constants.DATE_NAME + " LONG"+ ")";  

根据事故日志,我可以理解您需要在您的餐桌食品中添加卡路里列


请添加卡路里列以解决此问题。

我认为您的创建表查询是错误的,请尝试下面的代码

String CREATE_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
        + Constants.KEY_ID + " INTEGER PRIMARY KEY," + Constants.FOOD_NAME +
        " TEXT," + Constants.FOOD_CALORIES_NAME + " INTEGER," + Constants.DATE_NAME + " DATETIME);"
问题在于cursor.getInt0中的总卡路里 您没有从数据库中检索任何内容,因为您在第一个索引中有一个整数列,但从第0个索引中检索它。 解决方案:

cursor.getInt(1)

我解决了这个问题:我使用的android studio中的模拟器存在兼容性问题。我使用的是Nexus5API24,显然与sqlite3不兼容。我切换到Pixel3API26作为我的模拟器,它工作了。谢谢你的回复。这很有帮助。

onUpgrade没有被称为maybe。使用Room,它非常简单和干净。您可能有一个此代码的早期版本,但使用了不同的列名,但没有更改数据库版本。不,这不起作用。我想那是肯定的。那也不行