0

这个问题很有名,但每个人都说去看看这个记事本教程

这会有所帮助,但是当您被卡住时,它并没有太大帮助。

所以我有我在 Android 上创建的这个数据库,我想在列表视图中显示它,我做了教程,但我仍然不明白为什么我不能让它在我的项目上工作,所以如果有人谁能帮忙,请展示自己!


这是我的代码

public class MyFridge extends ListActivity {

private DBAdapter db;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notepad_list);
    db = new DBAdapter(this);
    db.open();
    fillData();   


}@Override
public boolean onCreateOptionsMenu (Menu menu)
{


    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected (MenuItem item)
{

    switch(item.getItemId())
    {
    case 0:

        Intent f = new Intent(MyFridge.this, Addform.class);
        startActivity(f);

            return true;

    case 1:

        Intent r = new Intent(MyFridge.this, MyRecipes.class);
        startActivity(r);


        return true;

    default:
        return super.onOptionsItemSelected(item);

        }
    }

private void CreateMenu(Menu menu) 
{
    MenuItem mnu1 = menu.add(0, 0, 0, "Add ingredient");
    {
        mnu1.setAlphabeticShortcut('a');//Change the alphabetic shortcut associated with this item.
        mnu1.setIcon(R.drawable.ic_launcher);

    }

    MenuItem mnu2 = menu.add(0, 1, 1, "Delete ingredient");
    {
        mnu2.setAlphabeticShortcut('b');//Change the alphabetic shortcut associated with this item.
        mnu2.setIcon(R.drawable.ic_launcher);
    }
    }

    private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = db.getAllCars();
        startManagingCursor(c);

        String[] from = new String[] { DBAdapter.KEY_TYPE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }

这是我的数据库:

    public class DBAdapter {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TYPE = "type";
    public static final String KEY_WEIGHT = "weight";
    public static final String KEY_EXP = "expiration day";

    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "MyDB";
    private static final String DATABASE_TABLE = "Fridge";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE =
        "create table Fridge (_id integer primary key autoincrement, "
        + "type text not null, weight integer not null, exp integer not null);";
    //CHECK (P_Id>0)

    private final Context context;    

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;  
        //It lets newly created objects understand what has been going on.
        //Typically you call it to get information regarding another part of your program
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);//calls the context in the parent class
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            try {
                db.execSQL(DATABASE_CREATE);    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) // this method is used if the OS or the version App is changed
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS cars");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a contact into the database---
    public long insertCar(String type, int weight, int exp) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_WEIGHT, weight);
        initialValues.put(KEY_EXP, exp);

        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular contact---
    public boolean deleteCar(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //---retrieves all the contacts---
    public Cursor getAllCars() 
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TYPE,
                KEY_WEIGHT, KEY_EXP}, null, null, null, null, null);
    }

    /*
     * retrieves a particular car
     * 
     */
    public Cursor getCar(String searchString) //throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_TYPE, KEY_WEIGHT,KEY_EXP},KEY_TYPE +"="+"'"+searchString+"'" +" OR "+ KEY_ROWID  + "=" + "'"+searchString +"'"+" OR "+ KEY_EXP + "=" + "'"+searchString +"'"+" OR " + KEY_WEIGHT + "=" + "'" + searchString +"'", null,
                null, null, null, null);

        return mCursor;
    }


    //---updates a contact---
    public boolean updateCar(long rowId, String type, int weight, int exp) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TYPE, type);
        args.put(KEY_WEIGHT, weight);
        args.put(KEY_EXP, exp);

        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

日志信息。

04-09 18:14:47.128: D/TextLayoutCache(23297): Using debug level: 0 - Debug Enabled: 0
04-09 18:14:47.173: D/CLIPBOARD(23297): Hide Clipboard dialog at Starting input: finished by someone else... !
04-09 18:14:49.113: I/SqliteDatabaseCpp(23297): sqlite returned: error code = 1, msg = no such table: Fridge, db=xxx
04-09 18:14:49.113: D/AndroidRuntime(23297): Shutting down VM
04-09 18:14:49.118: W/dalvikvm(23297): threadid=1: thread exiting with uncaught exception (group=0x40c281f8)
04-09 18:14:49.123: E/AndroidRuntime(23297): FATAL EXCEPTION: main
04-09 18:14:49.123: E/AndroidRuntime(23297): java.lang.RuntimeException: Unable to start activity ComponentInfo{wijayaratnam.sutharsun.mobilefridge/wijayaratnam.sutharsun.mobilefridge.MyFridge}: android.database.sqlite.SQLiteException: no such table: Fridge: , while compiling: SELECT _id, type, weight, expiration day FROM Fridge
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.os.Looper.loop(Looper.java:137)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread.main(ActivityThread.java:4507)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at dalvik.system.NativeStart.main(Native Method)
04-09 18:14:49.123: E/AndroidRuntime(23297): Caused by: android.database.sqlite.SQLiteException: no such table: Fridge: , while compiling: SELECT _id, type, weight, expiration day FROM Fridge
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1690)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1575)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1531)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1611)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at wijayaratnam.sutharsun.mobilefridge.DBAdapter.getAllCars(DBAdapter.java:101)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at wijayaratnam.sutharsun.mobilefridge.MyFridge.fillData(MyFridge.java:146)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at wijayaratnam.sutharsun.mobilefridge.MyFridge.onCreate(MyFridge.java:29)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.Activity.performCreate(Activity.java:4465)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
04-09 18:14:49.123: E/AndroidRuntime(23297):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
04-09 18:14:49.123: E/AndroidRuntime(23297):    ... 11 more
4

1 回答 1

0

create table如果您的错误是 sql“未找到列”,则将您的语句重写为:

private static final String DATABASE_CREATE =
    "create table " + DATABASE_TABLE + " ("
    + KEY_ROWID + " integer primary key autoincrement, "
    + KEY_TYPE + " text not null, "
    + KEY_WEIGHT + " integer not null, "
    + KEY_EXP + " integer not null);";

您会注意到您定义KEY_EXP = "expiration day"了但在您的创建语句中手动调用了该列exp;即未找到列。尝试尽可能多地使用您创建的那些变量来限制这些类型的拼写错误。祝你好运!

于 2012-04-09T17:14:34.800 回答