嗨,我有问题。

public void open(){  //il database su cui agiamo è leggibile/scrivibile
        mDb=mDbHelper.getWritableDatabase();

}


public void close(){ //chiudiamo il database su cui agiamo
        mDb.close();
}


//i seguenti 2 metodi servono per la lettura/scrittura del db. aggiungete e modificate a discrezione
// consiglio:si potrebbe creare una classe Prodotto, i quali oggetti verrebbero passati come parametri dei seguenti metodi, rispettivamente ritornati. Lacio a voi il divertimento



public void insertListView(String header,String footer,String activity){ //metodo per inserire i dati
        ContentValues cv=new ContentValues();
        cv.put(ListViewMetaData.LIST_VIEW_HEADER_KEY, header);
        cv.put(ListViewMetaData.LIST_VIEW_FOOTER_KEY, footer);
        cv.put(ListViewMetaData.LIST_VIEW_ACTIVITY_KEY, activity);
        mDb.insert(ListViewMetaData.LIST_VIEW_TABLE, null, cv);
}


public boolean delete(String row) {

    return mDb.delete(ListViewMetaData.LIST_VIEW_TABLE, ListViewMetaData.ID + "=" + row, null) > 0;

}



public Cursor fetchListView(){ //metodo per fare la query di tutti i dati
        return mDb.query(ListViewMetaData.LIST_VIEW_TABLE, null,null,null,null,null,null);
}


public Cursor fetchListViewByActivity(String activity){
        String[] act_query={activity};
        return mDb.query(ListViewMetaData.LIST_VIEW_TABLE,null,ListViewMetaData.LIST_VIEW_ACTIVITY_KEY+"=?",act_query,null,null,null);
}


static class ListViewMetaData {  // i metadati della tabella, accessibili ovunque
        static final String LIST_VIEW_TABLE = "list View";
        static final String ID = "_id";
        static final String LIST_VIEW_HEADER_KEY = "header";
        static final String LIST_VIEW_FOOTER_KEY = "footer";
        static final String LIST_VIEW_ACTIVITY_KEY = "activity";
}


private static final String LIST_VIEW_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  //codice sql di creazione della tabella
                + ListViewMetaData.LIST_VIEW_TABLE + " ("
                + ListViewMetaData.ID+ " integer primary key autoincrement, "
                + ListViewMetaData.LIST_VIEW_HEADER_KEY + " text, "
                + ListViewMetaData.LIST_VIEW_FOOTER_KEY + " text, "
                + ListViewMetaData.LIST_VIEW_ACTIVITY_KEY + " text);";


private class DbHelper extends SQLiteOpenHelper { //classe che ci aiuta nella creazione del db

        public DbHelper(Context context, String name, CursorFactory factory,int version) {
                super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) { //solo quando il db viene creato, creiamo la tabella
            System.out.println("merda");
                _db.execSQL(LIST_VIEW_TABLE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
                //qui mettiamo eventuali modifiche al db, se nella nostra nuova versione della app, il db cambia numero di versione

        }

}


这是我的代码:

final ListView lV_new_miss = (ListView) findViewById(R.id.lV_new_miss);

    //Apriamo il db creato tramite MissioniDb a cui passo il contesto della classe attuale ovvero Lista_Missioni
    listdb=new ListViewDb(getApplicationContext());
    System.out.println("prima del open");
    //apriamo il db
    listdb.open();
    System.out.println("WTF");
    //Chiamo la Query che in questo caso è un all
    Cursor curs_list_view=listdb.fetchListViewByActivity("1");

    //Comincio a fare il management del cursore
    startManagingCursor(curs_list_view);

    //Semplice cursor adapter
    adapter_list_view=new SimpleCursorAdapter(
                    this, //Contesto
                    R.layout.row_new_miss, //il layout di ogni riga/prodotto
                    curs_list_view, //Il cursore
                    new String[]{ListViewDb.ListViewMetaData.LIST_VIEW_HEADER_KEY,ListViewDb.ListViewMetaData.LIST_VIEW_FOOTER_KEY},//questi colonne
                    new int[]{R.id.tV_header_miss,R.id.tV_footer_miss});//in queste views


    lV_new_miss.setAdapter(adapter_list_view);

    listdb.close();


这是我的日志猫:

    04-02 15:03:22.624: I/Database(390): sqlite returned: error code = 1, msg = near "View": syntax error
04-02 15:03:22.624: E/Database(390): Failure 1 (near "View": syntax error) on 0x267938 when preparing 'CREATE TABLE IF NOT EXISTS list View (_id integer primary key autoincrement, header text, footer text, activity text);'.
04-02 15:03:22.634: D/AndroidRuntime(390): Shutting down VM
04-02 15:03:22.634: W/dalvikvm(390): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-02 15:03:22.644: E/AndroidRuntime(390): FATAL EXCEPTION: main
04-02 15:03:22.644: E/AndroidRuntime(390): java.lang.RuntimeException: Unable to start activity ComponentInfo{spese.gestore.androidiani/spese.gestore.androidiani.Nuova_Missione}: android.database.sqlite.SQLiteException: near "View": syntax error: CREATE TABLE IF NOT EXISTS list View (_id integer primary key autoincrement, header text, footer text, activity text);
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.os.Looper.loop(Looper.java:123)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-02 15:03:22.644: E/AndroidRuntime(390):  at java.lang.reflect.Method.invokeNative(Native Method)
04-02 15:03:22.644: E/AndroidRuntime(390):  at java.lang.reflect.Method.invoke(Method.java:521)
04-02 15:03:22.644: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-02 15:03:22.644: E/AndroidRuntime(390):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-02 15:03:22.644: E/AndroidRuntime(390):  at dalvik.system.NativeStart.main(Native Method)
04-02 15:03:22.644: E/AndroidRuntime(390): Caused by: android.database.sqlite.SQLiteException: near "View": syntax error: CREATE TABLE IF NOT EXISTS list View (_id integer primary key autoincrement, header text, footer text, activity text);
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
04-02 15:03:22.644: E/AndroidRuntime(390):  at spese.gestore.androidiani.ListViewDb$DbHelper.onCreate(ListViewDb.java:137)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
04-02 15:03:22.644: E/AndroidRuntime(390):  at spese.gestore.androidiani.ListViewDb.open(ListViewDb.java:39)
04-02 15:03:22.644: E/AndroidRuntime(390):  at spese.gestore.androidiani.Nuova_Missione.onCreate(Nuova_Missione.java:55)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-02 15:03:22.644: E/AndroidRuntime(390):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-02 15:03:22.644: E/AndroidRuntime(390):  ... 11 more

最佳答案

尝试改变

static final String LIST_VIEW_TABLE = "list View";




static final String LIST_VIEW_TABLE = "list_View";

10-08 19:04