android数据库无法正确填充和检索

时间:2013-08-03 14:28:35

标签: android sql database

我现在正在努力为数据库添加一些内置值以供以后检索,并按如下方式执行。但它报告错误,从id = 102开始,在init_food_DB()执行fill_food_DB()的过程中无法写入数据库,并且可能在Inflate_All_Food_Data过程中检索时进一步导致错误。

问题:

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

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

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

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

MainActivity:

@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

1 个答案:

答案 0 :(得分:0)

我猜两件事之一。也许您有权限问题,而且根本没有权利在数据库中创建表(或者错误地使用数据库)。

或者,该表可能已经存在。如果是这样,你可以这样做:

 create table if not exists . . .

而不是:

create table . . .

要初始化类,您可能希望删除表中的任何数据。创建后,您可以:

delete from . . .;

或在创作之前:

drop table if exists . . .;

实际上,放弃桌面是一种更好的方法。如果您决定更改表结构,那么您就知道它将被实现。