0

我现在正在努力将一些内置值放入数据库以供以后检索,并执行如下操作。但是在执行的过程中报错,从id=102开始数据库无法写入,在init_food_DB()执行的过程中fill_food_DB()可能会进一步导致检索出错Inflate_All_Food_Data

问题:

在这个应用程序中,我之前创建了另一个数据库(“first_database”)。第一个数据库可以正确执行、写入和检索。

基于成功,我因此完全从这个 first_database 复制,然后将 value.length 替换为 248,并仅替换内容。

为什么第一个数据库可以正常执行,但是这个食物数据库在id=102处停止填写?

作为参考,第一个数据库的 ContentValues 是 63。

主要活动:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.food_main); 

    init_food_DB();


    if(Food_dbHlp == null)
        Food_dbHlp = new Food_DataBaseHelper(this);
    food = Food_dbHlp.get_All_food_Data();  //LINE 221
    int i = food.size();        
    for (int j = 0; j < i; j++) 
    {
        Inflate_All_Food_Data(j);
    }   
}

private void init_food_DB()
{
    if(Food_dbHlp == null)
        Food_dbHlp = new Food_DataBaseHelper(this);
        Food_dbHlp.fill_food_DB();  
}

Food_DataBaseHelper

public class Food_DataBaseHelper extends SQLiteOpenHelper 
{
private static final String DATABASE_NAME = "food";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table_food";
private static final String TABLE_CREATE = 
                "CREATE TABLE " + TABLE_NAME + " ( " +
                " id TEXT NOT NULL, " +
                " food_group TEXT NOT NULL, " +
                " food_name TEXT, " +
                " food_qty TEXT, " +
                " food_cal TEXT, PRIMARY KEY (id)); ";
public static final String COL_id = "id";
public static final String COL_food_group = "food_group";
public static final String COL_food_name = "food_name";
public static final String COL_food_qty = "food_qty";
public static final String COL_food_cal = "food_cal";

public Food_DataBaseHelper(Context context) 
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) 
{
    db.execSQL(TABLE_CREATE);
}

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


public void fill_food_DB() 
{
    SQLiteDatabase db = getWritableDatabase();      
    ContentValues[] values = new ContentValues[248];
    for(int i=0; i<values.length; i++)
        values[i] = new ContentValues();

    values[0].put("id", "001");
    values[0].put("food_group", "Snacks");
    values[0].put("food_name","Chestnuts");
    values[0].put("food_qty","100g");
    values[0].put("food_cal","170");
    values[1].put("id", "002");
    values[1].put("food_group", "Snacks");
    values[1].put("food_name","Brazil apricots");
    values[1].put("food_qty","100g");
    values[1].put("food_cal","619");
            ...
            ...
    values[247].put("id", "248");
    values[247].put("food_group", "Milk product");
    values[247].put("food_name","Crispy chocolate ice cream sticks");
    values[247].put("food_qty","1 cup");
    values[247].put("food_cal","372");

    for(ContentValues row : values)
    {
        db.insert(TABLE_NAME, null, row);
    }   
    db.close();
}

public ArrayList<Food> get_All_food_Data()
{
    SQLiteDatabase db = getReadableDatabase();
    String[] columns = {COL_id, COL_food_group, COL_food_name, COL_food_qty, COL_food_cal};
    Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, COL_id); //LINE 1308
    ArrayList<Food> foods = new ArrayList<Food>();
    while(cursor.moveToNext()){
        String id = cursor.getString(0);
        String food_group = cursor.getString(1);
        String food_name = cursor.getString(2);
        String food_qty = cursor.getString(3);
        String food_cal = cursor.getString(4);          
        Food food = new Food(id, food_group, food_name, food_qty, food_cal);
        foods.add(food);            
    }
    cursor.close();
    db.close();
    return foods;       
}

但是当id达到102时报错

Logcat 报告如下:

08-03 22:03:20.570: E/SQLiteLog(8105): (1) no such table: table_food
08-03 22:03:20.615: E/SQLiteDatabase(8105): Error inserting id=102 food_name=Tomato food_group=Fruits & vegetables food_qty=100g food_cal=14
08-03 22:03:20.615: E/SQLiteDatabase(8105): android.database.sqlite.SQLiteException: no such table: table_food (code 1): , while compiling: INSERT INTO table_food(id,food_name,food_group,food_qty,food_cal) VALUES (?,?,?,?,?)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at com.abc.abc.Abc_Food_DataBaseHelper.fill_food_DB(Abc_Food_DataBaseHelper.java:1299)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at com.abc.abc.Food_MainActivity.init_food_DB(Food_MainActivity.java:262)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at com.abc.abc.Food_MainActivity.onCreate(Food_MainActivity.java:64)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.Activity.performCreate(Activity.java:5206)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.os.Looper.loop(Looper.java:137)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at android.app.ActivityThread.main(ActivityThread.java:4921)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at java.lang.reflect.Method.invoke(Method.java:511)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-03 22:03:20.615: E/SQLiteDatabase(8105):     at dalvik.system.NativeStart.main(Native Method)
08-03 22:03:20.615: E/SQLiteLog(8105): (1) no such table: table_food
08-03 22:03:20.615: E/SQLiteDatabase(8105): Error inserting id=103 food_name=Chinese water chestnut food_group=Fruits & vegetables food_qty=100g food_cal=68
 ...
 ...keep continue for every id from 102 to 248
 ...
08-03 22:03:21.245: E/SQLiteDatabase(8105): Error inserting id=248 food_name=Crispy chocolate ice cream sticks food_group=Milk product food_qty=1 cup food_cal=372
08-03 22:03:21.245: E/SQLiteDatabase(8105): android.database.sqlite.SQLiteException: no such table: table_food (code 1): , while compiling: INSERT INTO table_food(id,food_name,food_group,food_qty,food_cal) VALUES (?,?,?,?,?)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at com.abc.abc.Abc_Food_DataBaseHelper.fill_food_DB(Abc_Food_DataBaseHelper.java:1299)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at com.abc.abc.Food_MainActivity.init_food_DB(Food_MainActivity.java:262)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at com.abc.abc.Food_MainActivity.onCreate(Food_MainActivity.java:64)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.Activity.performCreate(Activity.java:5206)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.os.Looper.loop(Looper.java:137)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at android.app.ActivityThread.main(ActivityThread.java:4921)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at java.lang.reflect.Method.invoke(Method.java:511)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-03 22:03:21.245: E/SQLiteDatabase(8105):     at dalvik.system.NativeStart.main(Native Method)   
08-03 22:03:21.265: E/SQLiteLog(8105): (1) no such table: table_food
08-03 22:03:21.285: D/AndroidRuntime(8105): Shutting down VM
08-03 22:03:21.285: W/dalvikvm(8105): threadid=1: thread exiting with uncaught exception (group=0x41c572a0)
08-03 22:03:21.285: E/AndroidRuntime(8105): FATAL EXCEPTION: main
08-03 22:03:21.285: E/AndroidRuntime(8105): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.abc/com.abc.abc.Food_MainActivity}: android.database.sqlite.SQLiteException: no such table: table_food (code 1): , while compiling: SELECT id, food_group, food_name, food_qty, food_cal FROM table_food ORDER BY id
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.os.Looper.loop(Looper.java:137)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread.main(ActivityThread.java:4921)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at java.lang.reflect.Method.invokeNative(Native Method)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at java.lang.reflect.Method.invoke(Method.java:511)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at dalvik.system.NativeStart.main(Native Method)
08-03 22:03:21.285: E/AndroidRuntime(8105): Caused by: android.database.sqlite.SQLiteException: no such table: table_food (code 1): , while compiling: SELECT id, food_group, food_name, food_qty, food_cal FROM table_food ORDER BY id
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at com.abc.abc.Abc_Food_DataBaseHelper.get_All_food_Data(Abc_Food_DataBaseHelper.java:1308)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at com.abc.abc.Food_MainActivity.onCreate(Food_MainActivity.java:221)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.Activity.performCreate(Activity.java:5206)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-03 22:03:21.285: E/AndroidRuntime(8105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-03 22:03:21.285: E/AndroidRuntime(8105):     ... 11 more
4

1 回答 1

0

I would guess one of two things. Perhaps you have a permissions issue, and simply don't have the rights to create a table in the database (or are misnaming the database).

Or, the table might already exist. If so, you could do:

 create table if not exists . . .

rather than:

create table . . .

To initialize the class, you might then want to remove any data in the table. After creation you can do:

delete from . . .;

Or before the creation:

drop table if exists . . .;

Actually, dropping the table is a better approach. If you decide to change the table structure, then you know it will be implemented.

于 2013-08-03T14:41:27.083 回答