简体   繁体   中英

How to create multiple table?

Please help me, I'm newbie and I have problem. I try to create multiple table in this code but always error. this error say there is no table PENGINAPAN and i can't solve it.

this is DatabaseHelper class :

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

}
 }

please help me.

Use this code and modify it as per your needs. i have added all comments so that you can understand where i am adding a table, where i am adding a column and where i am adding entries.

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();
    }



}

Updated Answer: Picture class code is added

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;
    }

}

I cleaned up your code. If it doesn't work, uninstall your app and install it again. Be sure you create the database before you try to write/read any data.

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM