-1

Hi can anyone please help to overcome this problem..

THis is my DatabaseHelper.java code

private static final String TABLE_SEATS = "Seat";
private static final String KEY_IDseats = "id";
private static final String KEY_CREATED_ATTs = "created_at";
private static final String KEY_Seatstrainno = "TrainNo";
private static final String KEY_Seatstype = "TrainType";
private static final String KEY_SeatsCls = "AvailC";
private static final String KEY_SEATSFARE="Fare" ;
private static final String KEY_TOTALSEATS="Total_Seats";


private static final String CREATE_TABLE_seats = "CREATE TABLE " + TABLE_SEATS
        + " (" + KEY_IDseats + " integer primary key autoincrement, "
        + KEY_Seatstrainno + " string not null,"
        + KEY_Seatstype + " string not null,"
        + KEY_SeatsCls + " string not null,"
        + KEY_SEATSFARE + " string not null,"
        + KEY_TOTALSEATS + " string not null"
        + ");";

@Override
    public void onCreate(SQLiteDatabase _db) {

        _db.execSQL(CREATE_TABLE_seats);
        Log.d("Train seats table ", "created");


    }


 public void insertTrainseats(DatabaseHelper dob, String trainno, String   trainttype, String traincls,String fare,String seats) {

SQLiteDatabase SQ = dob.getWritableDatabase();
ContentValues initialVa = new ContentValues();
initialVa.put(KEY_Seatstrainno, trainno);
initialVa.put(KEY_Seatstype, trainttype);
initialVa.put(KEY_SeatsCls,traincls);
initialVa.put(KEY_SEATSFARE, fare);
initialVa.put(KEY_TOTALSEATS, seats);

long k=SQ.insert(TABLE_SEATS, null, initialVa);
}

and my logcat shows this error

07-19 08:28:06.444 2522-2522/com.example.myapplication E/SQLiteLog﹕ (1) no such table: Seat

1 Answers1

0

It looks like the Seat table is not being created. You need to make sure onCreate() is getting called before you try to add data.

You probably need an onUpgrade() method to make sure onCreate is being called properly if you have made changes to your database:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "YOUR_DATABASE_NAME;
    // Increment this when you make changes
    private static final int DATABASE_VERSION = 2;

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

    // This will delete your old database, but that's probably ok since it's not yet working.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_SEATS);
        onCreate(database);
    }


    // plus the code you have above
    ...
}
kg.
  • 561
  • 6
  • 14
  • i add a log.d() statement in the beginning of oncreate and checked weather it is calling but it is not displayed in the logcat that means oncreate is not called no? what can i do now...[i am new to android] – Sajana S Jul 19 '15 at 06:53
  • You probably need to add an onUpgrade method. I've added some more details on that above. – kg. Jul 19 '15 at 15:45