0

Im trying to use sqlite as my database for my app. At first i have create a table for the user when they sign up. But when i create the second table for the booking, it says no such table Below is my code

public class DBhelper extends SQLiteOpenHelper {
    public static final  String DBNAME="database.db";

    public DBhelper(@Nullable Context context) {
        super(context,"database.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

         db.execSQL("create table users(username TEXT primary key, password TEXT )");
         db.execSQL("create table booking(serviceID INTEGER primary key, name TEXT,email TEXT,phone TEXT )");    
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("drop table if exists users");
        db.execSQL("drop table if exists booking");
        onCreate(db);
    }

    public Boolean insertData(String username, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username",username);
        values.put("password", password);

        long result  = db.insert("users",null,values);
        if(result==-1)return false;
        else
            return true;   
    }

    public void addNewBooking( int serviceID, String name,String notes, String phone, String email) {

        SQLiteDatabase db = this.getWritableDatabase();  
        ContentValues values = new ContentValues();

        // on below line we are passing all values
        // along with its key and value pair.    
        values.put("serviceID", serviceID);
        values.put("name", name);
        values.put("notes", notes);
        values.put("phone", phone);
        values.put("email", email);    
        // after adding all values we are passing
        // content values to our table.
        db.insert("booking", null, values);    
        // at last we are closing our
        // database after adding database.            
    }
   
    public Boolean checkusername(String username){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from users where username = ?", new String[] {username});
        if(cursor.getCount()>0){
            return true;
        }else
            return false;
    }

    public Boolean checkuserpass(String username, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from users where username =? and password =?", new String[] {username,password});
        if(cursor.getCount()>0){
            return true;
        }else
            return false;
    }    
}

below is the error shown

E/SQLiteLog: (1) table booking has no column named serviceID in "INSERT INTO booking(serviceID,name,email,notes,phone) VALUES (?,?,?,?,?)"
E/SQLiteDatabase: Error inserting serviceID=0 name=name email=em notes=no phone=12
    android.database.sqlite.SQLiteException: table booking has no column named serviceID (code 1 SQLITE_ERROR): , while compiling: INSERT INTO booking(serviceID,name,email,notes,phone) VALUES (?,?,?,?,?)
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Uninstall the app from the device/emulator and rerun. – forpas Jan 08 '23 at 08:34
  • Does this answer your question? [android.database.sqlite.SQLiteException: no such column](https://stackoverflow.com/questions/16820106/android-database-sqlite-sqliteexception-no-such-column), https://stackoverflow.com/questions/17441927/android-sqlite-issue-table-has-no-column-named – forpas Jan 08 '23 at 08:43
  • yes tq very much – Muqri Hafiy Jan 09 '23 at 07:10

0 Answers0