mercredi 30 décembre 2015

android sqlite insert query

Here is my code, I don't know what is wrong with getWritableDatabase().insert(TABLE_ROOM, null, contentValues); and Cursor wrapped = getReadableDatabase().rawQuery("select * from room", null); this is RoomDatabaseHelper.java

public class RoomDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "rooms.sqlite";
private static final int VERSION = 1;

private static final String TABLE_ROOM = "room";
private static final String ROOM_NUMBER = "room_number";
private static final String ROOM_PRICE = "room_price";
private static final String GUEST_NAME = "guest_name";
private static final String ID_CARD = "id_card";
private static final String CHECK_IN_DATE = "check_in_date";
private static final String CHECK_OUT_DATE = "check_out_date";

public RoomDatabaseHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table room (room_number integer primary key autoincrement, " +
            "room_price integer, guest_name varchar(100), id_card varchar(100), " +
            "check_in_date varchar(100), check_out_date varchar(100))");

    for (int i = 1; i < 50; i++) {
        Room room = new Room(100 + i, 270);
        insertRoom(room);
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void insertRoom(Room room) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(ROOM_NUMBER, room.getNumber());
    contentValues.put(ROOM_PRICE, room.getPrice());
    contentValues.put(GUEST_NAME, room.getGuestName());
    contentValues.put(ID_CARD, room.getIdCard());
    contentValues.put(CHECK_IN_DATE, room.getCheckInDate());
    contentValues.put(CHECK_OUT_DATE, room.getCheckOutDate());
    getWritableDatabase().insert(TABLE_ROOM, null, contentValues);
}

public RoomCursor queryRoom(int number) {
    Cursor wrapped = getReadableDatabase().query(TABLE_ROOM, null,
            ROOM_NUMBER + "=?",
            new String[]{String.valueOf(number)},
            null, null, null, "1");
    return new RoomCursor(wrapped);
}

public RoomCursor queryRooms() {
    Cursor wrapped = getReadableDatabase().rawQuery("select * from room", null);
    return new RoomCursor(wrapped);
}

public static class RoomCursor extends CursorWrapper {
    public RoomCursor(Cursor cursor) {
        super(cursor);
    }

    public Room getRoom() {
        if (isBeforeFirst() || isAfterLast()) {
            return null;
        }
        Room room = new Room(-1, -1);
        int roomNumber = getInt(getColumnIndex(ROOM_NUMBER));
        room.setNumber(roomNumber);
        int roomPrice = getInt(getColumnIndex(ROOM_PRICE));
        room.setPrice(roomPrice);
        String guestName = getString(getColumnIndex(GUEST_NAME));
        room.setGuestName(guestName);
        String idCard = getString(getColumnIndex(ID_CARD));
        room.setIdCard(idCard);
        String checkInDate = getString(getColumnIndex(CHECK_IN_DATE));
        room.setCheckInDate(checkInDate);
        String checkOutDate = getString(getColumnIndex(CHECK_OUT_DATE));
        room.setCheckOutDate(checkOutDate);
        return room;
    }
}

} this is Room.java

public class Room {
private int mNumber;
private int mPrice;
private String mGuestName;
private String mIdCard;
private String mCheckInDate;
private String mCheckOutDate;

public Room(int number, int price) {
    mNumber = number;
    mPrice = price;
    mGuestName = " ";
    mIdCard = " ";
    mCheckInDate = " ";
    mCheckOutDate = " ";
}

public int getNumber() {
    return mNumber;
}

public void setNumber(int number) {
    mNumber = number;
}

public int getPrice() {
    return mPrice;
}

public void setPrice(int price) {
    mPrice = price;
}

public String getGuestName() {
    return mGuestName;
}

public void setGuestName(String guestName) {
    mGuestName = guestName;
}

public String getIdCard() {
    return mIdCard;
}

public void setIdCard(String idCard) {
    mIdCard = idCard;
}

public String getCheckInDate() {
    return mCheckInDate;
}

public void setCheckInDate(String checkInDate) {
    mCheckInDate = checkInDate;
}

public String getCheckOutDate() {
    return mCheckOutDate;
}

public void setCheckOutDate(String checkOutDate) {
    mCheckOutDate = checkOutDate;
}

}

Aucun commentaire:

Enregistrer un commentaire