-3

I tried to find why my app crashes at start, and debugger says that

"E/SQLiteLog: (1) no such table: Categories E/SQLiteDatabase: Error inserting Name=Salads android.database.sqlite.SQLiteException: no such table: Categories (code 1): , while compiling: INSERT INTO Categories(Name) VALUES (?)"

Even tho after I looked into my code, it seems like the table categories is made and error shouldnt appear.

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

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

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}
JasonM
  • 3
  • 6
  • 1
    Uninstall and run the app again in your device, I think issue is related to your database version(because you had changes in table column after first run so they are not getting update due to your same database version). – Ready Android Nov 23 '16 at 11:22
  • @ReadyAndroid Indeed. So awkward... Thanks man! – JasonM Nov 23 '16 at 11:25

3 Answers3

0

You didn't Give space before TEXT use this

private static final String CREATE_MEALS = "CREATE TABLE "
    + Meals + "(" + KEY_ID + " INTEGER PRIMARY KEY," + mealsName
    + " TEXT unique," + desc + " TEXT," + size + " TEXT,"
    + price + " TEXT" + ")";
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • Right now, I'm passing data only to "CREATE_CATEGORIES" and it says error there, so I doubt that the error causing is CREATE MEALS at this point, but thanks anyways. – JasonM Nov 23 '16 at 11:22
0

Change the value of private static final int DATABASE_VERSION = 1; to private static final int DATABASE_VERSION = 2; Maybe you have modified database structure and forgot to update it this way. Tell me if it worked.

imagdic
  • 331
  • 3
  • 5
0

//Create object of your database handler MyDatabaseHelper dbHelperDriver = new MyDatabaseHelper(this);

//insert category using the object dbHelperDriver.addCategory("TestCategory");

Vidit
  • 41
  • 4