2014-02-18 104 views
0

請幫助我,我是新手,我有問題。我嘗試在這段代碼中創建多個表,但總是出錯。這個錯誤說沒有表PENGINAPAN,我無法解決它。如何創建多個表?

這是DatabaseHelper類:

public class DatabaseHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "dbwisata"; 
public static final String NAMA = "nama"; 
public static final String KEY_ID = "_id"; 


public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
} 

// method createTable untuk membuat table WISATA 
public void createTable(SQLiteDatabase db) { 
    db.execSQL("DROP TABLE IF EXISTS WISATA"); 
    db.execSQL("CREATE TABLE if not exists WISATA (_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + "nama TEXT);"); 
    db.execSQL("DROP TABLE IF EXISTS PENGINAPAN"); 
    db.execSQL("CREATE TABLE if not exists PENGINAPAN (_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + "nama TEXT);"); 
} 

// method generateData untuk mengisikan data ke table Wisata. 
public void generateData(SQLiteDatabase db) { 
    ContentValues cv = new ContentValues(); 
    cv.put(NAMA, "Ancol"); 
    db.insert("WISATA", NAMA, cv); 
    cv.put(NAMA, "Ragunan"); 
    db.insert("WISATA", NAMA, cv); 
    cv.put(NAMA, "Taman Mini"); 
    db.insert("PENGINAPAN", NAMA, cv); 

    cv.put(NAMA, "Melati"); 
    db.insert("PENGINAPAN", NAMA, cv); 
    cv.put(NAMA, "Villa"); 
    db.insert("PENGINAPAN", NAMA, cv); 
    cv.put(NAMA, "Bintang"); 
    db.insert("PENGINAPAN", NAMA, cv); 
} 

// method delAllAdata untuk menghapus data di table Wisata. 
public void delAllData(SQLiteDatabase db) { 
    db.delete("WISATA", null, null); 
    db.delete("WISATA", null, null); 
} 

public Cursor fetchAllWisata(SQLiteDatabase db) { 
    return db.query("WISATA", new String[] { KEY_ID, NAMA }, null, null, 
      null, null, null); 
} 

public Cursor fetchAllPenginapan(SQLiteDatabase db) { 
    return db.query("PENGINAPAN", new String[] { KEY_ID, NAMA }, null, null, 
      null, null, null); 
} 

@Override 
public void onCreate(SQLiteDatabase db) {  
    createTable(db); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

} 
} 

請幫助我。

+0

卸載並重新安裝您的應用程序,以便再次調用onCreate()。 – laalto

回答

0

使用此代碼並根據您的需要對其進行修改。我添加了所有評論,以便您可以瞭解我在哪裏添加表格,在哪裏添加列以及添加條目的位置。

public class DatabaseHelper extends SQLiteOpenHelper { 

    //changing master 
    // Logcat tag 
    //add comment 
    private static final String LOG = "DatabaseHelper"; 

    // Database Version 
    private static final int DATABASE_VERSION = 3; 

    // Database Name 
    private static final String DATABASE_NAME = "contactsManager"; 

    // Table Names 

    private static final String TABLE_VIDEO = "video"; 
    private static final String TABLE_PICTURE = "pictures"; 
    private static final String TABLE_MUSIC = "music"; 

    // Common column names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_FAV = "fav"; 


    // MUSIC Table - column names 

    private static final String KEY_MUSIC_PATH = "path"; 
    private static final String KEY_MUSIC_CAT_ID = "cat_id"; 
    private static final String KEY_MUSIC_NAME = "music_name"; 


    // PICTURE Table - column names 

    private static final String KEY_PICTURE_PATH = "path"; 
    private static final String KEY_PICTURE_CAT_ID = "cat_id"; 
    private static final String KEY_PICTURE_NAME = "picture_name"; 

    // VIDEO Table - column names 

    private static final String KEY_VIDEO_PATH = "path"; 
    private static final String KEY_VIDEO_CAT_ID = "cat_id"; 
    private static final String KEY_VIDEO_SUBCAT_ID = "subcat_id"; 
    private static final String KEY_VIDEO_NAME = "video_name"; 


    // Table Create Statements 


    // Video table create statement 
    private static final String CREATE_TABLE_VIDEO = "CREATE TABLE " 
      + TABLE_VIDEO + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + KEY_FAV + " TEXT," 
      + KEY_VIDEO_PATH + " TEXT," 
      + KEY_VIDEO_CAT_ID+ " INTEGER," 
      + KEY_VIDEO_SUBCAT_ID + " TEXT," 
      + KEY_VIDEO_NAME + " TEXT)"; 

    // PICTURE table create statement 
    private static final String CREATE_TABLE_PICTURE = "CREATE TABLE " 
      + TABLE_PICTURE + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + KEY_FAV + " TEXT," 
      + KEY_PICTURE_PATH + " TEXT," + KEY_PICTURE_CAT_ID+ " INTEGER," 
      + KEY_PICTURE_NAME + " TEXT)"; 

    // MUSIC table create statement 
    private static final String CREATE_TABLE_MUSIC = "CREATE TABLE " 
      + TABLE_MUSIC + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + KEY_FAV + " TEXT," 
      + KEY_MUSIC_PATH + " TEXT," + KEY_MUSIC_CAT_ID+ " INTEGER," 
      + KEY_MUSIC_NAME + " TEXT)"; 

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



    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     // creating required tables 

     db.execSQL(CREATE_TABLE_PICTURE); 
     db.execSQL(CREATE_TABLE_MUSIC); 
     db.execSQL(CREATE_TABLE_VIDEO); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     // on upgrade drop older tables 

     db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_PICTURE); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_MUSIC); 

     // create new tables 
     onCreate(db); 
    } 


    /* 
    * create videos 
    * */ 
/* public boolean addVideos(List<Video> video, int category_id){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     long id = 0; 
     for(int i = 0; i <video.size(); ++i){ 
      ContentValues values = new ContentValues(); 
      values.put(KEY_VIDEO_NAME, video.get(i).getName()); 
      values.put(KEY_VIDEO_PATH, video.get(i).getPath()); 
      values.put(KEY_VIDEO_CAT_ID, category_id); 
      // insert row 
      id = db.insert(TABLE_VIDEO, null, values); 
     } 

     if (id < 0) 
      return false; 
     return true; 
    }*/ 


    // add videos 
    public void addvideos(Video item, int catid, String subcat) { 
     SQLiteDatabase db = this.getWritableDatabase(); 


     db.execSQL("insert into video (fav, path, cat_id, subcat_id , video_name) VALUES (" + "'" + item.isFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + catid + "'," + "'" + subcat + "'," + "'" + item.getName() + "'" + ")"); 

    } 

    // add pictures 
    public void addpictures(Picture item, int catid) { 
     SQLiteDatabase db = this.getWritableDatabase(); 


     db.execSQL("insert into video (path, cat_id , video_name) VALUES (" + "'" + item.isFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + catid + "'," + "'" + item.getName() + "'" + ")"); 

    } 

    // add music 
    public void addmusic(Music item, int catid) { 
     SQLiteDatabase db = this.getWritableDatabase(); 


     db.execSQL("insert into video (path, cat_id , video_name) VALUES (" + "'" + item.isFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + catid + "'," + "'" + item.getName() + "'" + ")"); 

    } 

    /* 
    * getting all videos 
    * */ 
    public List<Video> getAllVideos() { 
     List<Video> lstVideo = new ArrayList<Video>(); 
     String selectQuery = "SELECT * FROM " + TABLE_VIDEO; 

     Log.e(LOG, selectQuery); 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (c.moveToFirst()) { 
      do { 
       Video video = new Video(); 
       video.setId(c.getInt((c.getColumnIndex(KEY_ID)))); 
       video.setName((c.getString(c.getColumnIndex(KEY_VIDEO_NAME)))); 
       video.setPath((c.getString(c.getColumnIndex(KEY_VIDEO_PATH)))); 
       video.setSubcategory(c.getString(c.getColumnIndex(KEY_VIDEO_SUBCAT_ID))); 
       // adding to video list 
       lstVideo.add(video); 
      } while (c.moveToNext()); 
     } 

     return lstVideo; 
    } 



    /* 
    * getting all videos by categories 
    * */ 
    public List<Video> getAllVideosByCategory(String category) { 
     List<Video> lstVideo = new ArrayList<Video>(); 

     String selectQuery = "SELECT * FROM " + TABLE_VIDEO + 
       " WHERE " + TABLE_VIDEO + "." + KEY_VIDEO_SUBCAT_ID + 
       " = " + category; 

     Log.e(LOG, selectQuery); 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (c.moveToFirst()) { 
      do { 
       Video video = new Video(); 
       video.setId(c.getInt((c.getColumnIndex(KEY_ID)))); 
       video.setName((c.getString(c.getColumnIndex(KEY_VIDEO_NAME)))); 
       video.setPath((c.getString(c.getColumnIndex(KEY_VIDEO_PATH)))); 
       video.setSubcategory(c.getString(c.getColumnIndex(KEY_VIDEO_SUBCAT_ID))); 
       // adding to video list 
       lstVideo.add(video); 
      } while (c.moveToNext()); 
     } 

     return lstVideo; 
    } 

    // getting all pictures 
    public List<Picture> getAllPictures() { 
     List<Picture> lstVideo = new ArrayList<Picture>(); 
     String selectQuery = "SELECT * FROM " + TABLE_PICTURE; 

     Log.e(LOG, selectQuery); 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (c.moveToFirst()) { 
      do { 
       Picture video = new Picture(); 
       video.setId(c.getInt((c.getColumnIndex(KEY_ID)))); 
       video.setName((c.getString(c.getColumnIndex(KEY_PICTURE_NAME)))); 
       video.setPath((c.getString(c.getColumnIndex(KEY_PICTURE_PATH)))); 
       // adding to video list 
       lstVideo.add(video); 
      } while (c.moveToNext()); 
     } 

     return lstVideo; 
    } 

    // getting all music 
    public List<Music> getAllMusic() { 
     List<Music> lstVideo = new ArrayList<Music>(); 
     String selectQuery = "SELECT * FROM " + TABLE_MUSIC; 

     Log.e(LOG, selectQuery); 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (c.moveToFirst()) { 
      do { 
       Music video = new Music(); 
       video.setId(c.getInt((c.getColumnIndex(KEY_ID)))); 
       video.setName((c.getString(c.getColumnIndex(KEY_VIDEO_NAME)))); 
       video.setPath((c.getString(c.getColumnIndex(KEY_VIDEO_PATH)))); 
       // adding to video list 
       lstVideo.add(video); 
      } while (c.moveToNext()); 
     } 

     return lstVideo; 
    } 

    // closing database 
    public void closeDB() { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     if (db != null && db.isOpen()) 
      db.close(); 
    } 



} 

更新答:

public class Picture { 

    public int id; 
    public String name; 
    public String path; 
    public int favourite; 



    public int getFavourite() { 
     return favourite; 
    } 

    public void setFavourite(int favourite) { 
     this.favourite = favourite; 
    } 
    public Picture(){ 
    } 

    public Picture(int id, String name, String path){ 
     this.id = id; 
     this.name = name; 
     this.path = path; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

} 
+0

你好Hassaan,我可以有你的照片班代碼。因爲我不明白。謝謝。 – Ryan

+0

圖片是模型類..我添加了代碼。請檢查 –

0

我清理你的代碼Picture類代碼被添加。如果不起作用,請卸載您的應用並重新安裝。確保在嘗試寫入/讀取任何數據之前創建數據庫。

public class DatabaseHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "dbwisata"; 
    private static final String TABLE_WISATA = "WISATA"; 
    private static final String TABLE_PENGINAPAN = "PENGINAPAN"; 
    public static final String NAMA = "nama"; 
    public static final String KEY_ID = "_id"; 


    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    // method createTable untuk membuat table WISATA 
    public void createTable(SQLiteDatabase db) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_WISATA); 
     db.execSQL("CREATE TABLE if not exists " + TABLE_WISATA + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + "nama TEXT);"); 
     db.execSQL("DROP TABLE IF EXISTS PENGINAPAN"); 
     db.execSQL("CREATE TABLE if not exists " + TABLE_PENGINAPAN + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + NAMA + " TEXT);"); 
    } 

    // method generateData untuk mengisikan data ke table Wisata. 
    public void generateData(SQLiteDatabase db) { 
     ContentValues cv = new ContentValues(); 
     cv.put(NAMA, "Ancol"); 
     db.insert(TABLE_WISATA, null, cv); 
     cv.put(NAMA, "Ragunan"); 
     db.insert(TABLE_WISATA, null, cv); 

     cv.put(NAMA, "Taman Mini"); 
     db.insert(TABLE_PENGINAPAN, null, cv); 
     cv.put(NAMA, "Melati"); 
     db.insert(TABLE_PENGINAPAN, null, cv); 
     cv.put(NAMA, "Villa"); 
     db.insert(TABLE_PENGINAPAN, null, cv); 
     cv.put(NAMA, "Bintang"); 
     db.insert(TABLE_PENGINAPAN, null, cv); 
    } 

    // method delAllAdata untuk menghapus data di table Wisata. 
    public void delAllData(SQLiteDatabase db) { 
     db.delete(TABLE_WISATA, null, null); 
     db.delete(TABLE_WISATA, null, null); 
    } 

    public Cursor fetchAllWisata(SQLiteDatabase db) { 
     return db.query(TABLE_WISATA, new String[] { KEY_ID, NAMA }, null, null, 
       null, null, null); 
    } 

    public Cursor fetchAllPenginapan(SQLiteDatabase db) { 
     return db.query(TABLE_PENGINAPAN, new String[] { KEY_ID, NAMA }, null, null, 
       null, null, null); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) {  
     createTable(db); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 
}