10

I'm using the following code for creating multiple tables in a database. But, I don't understand why this problem happens.

private static final String TABLE_SMSFilter = "SMSFilter";


public void onCreate(SQLiteDatabase db)
    {
        Log.d("Test", "Control is in Oncreate()");
        String CREATE_SMSSSCHEDULE_TABLE = "CREATE TABLE " + TABLE_SMSSchedule
                + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
                + KEY_NUMBER + " TEXT)";

        String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE "
                + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID
                + " INTEGER PRIMARY KEY autoincrement,"
                + ProfileSchedule_NUMBER + " TEXT, "
                + ProfileSchedule_ProfileMode + " TEXT,"
                + ProfileSchedule_CalendarID + "INTEGER)";

        String CREATE_SMS_FILTER_TABLE = "CREATE TABLE " + TABLE_SMSFilter
                + "(" + SMSFilter_ID + " INTEGER PRIMARY KEY autoincrement,"
                + SMSFilter_NUMBER + " TEXT)";

        db.execSQL(CREATE_SMSSSCHEDULE_TABLE);
        db.execSQL(CREATE_PROFILE_SCHEDULE_TABLE);
        db.execSQL(CREATE_SMS_FILTER_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_SMSSchedule);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ProfileSchedule);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMSFilter);

        // Create tables again
        onCreate(db);
    }

here is the insert function

public void addSMSFilter(SMSFilter filterVariable)
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(SMSFilter_NUMBER, filterVariable.getPhoneNumber()); // Contact
                                                                        // Name
        // values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        Log.d("test", "inserting" + filterVariable.getPhoneNumber());
        // Inserting Row
        db.insert(TABLE_SMSFilter, null, values);

        db.close(); // Closing database connection
    }

and now in my main activity, I used this code to insert into this table

DatabaseHandler db = new DatabaseHandler(this);

        SMSFilter sms = new SMSFilter("1234556");
        db.addSMSFilter(sms);

but it gives me the error that no table "SMSFILTER" found.

Log result is here

11-25 22:52:22.643: I/Database(12209): sqlite returned: error code = 1, msg = no such table: SMSFilter
11-25 22:52:22.643: E/Database(12209): Error inserting PhoneNo=1234556
11-25 22:52:22.643: E/Database(12209): android.database.sqlite.SQLiteException: no such table: SMSFilter: , while compiling: INSERT INTO SMSFilter(PhoneNo) VALUES(?);
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1212)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1610)
11-25 22:52:22.643: E/Database(12209):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484)
11-25 22:52:22.643: E/Database(12209):  at com.scheduler.database.DatabaseHandler.addSMSFilter(DatabaseHandler.java:327)
11-25 22:52:22.643: E/Database(12209):  at com.schedule.test.TestActivity.onCreate(TestActivity.java:31)
11-25 22:52:22.643: E/Database(12209):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
11-25 22:52:22.643: E/Database(12209):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
11-25 22:52:22.643: E/Database(12209):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
11-25 22:52:22.643: E/Database(12209):  at android.app.ActivityThread.access$2300(ActivityThread.java:135)
11-25 22:52:22.643: E/Database(12209):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
11-25 22:52:22.643: E/Database(12209):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 22:52:22.643: E/Database(12209):  at android.os.Looper.loop(Looper.java:144)
11-25 22:52:22.643: E/Database(12209):  at android.app.ActivityThread.main(ActivityThread.java:4937)
11-25 22:52:22.643: E/Database(12209):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 22:52:22.643: E/Database(12209):  at java.lang.reflect.Method.invoke(Method.java:521)
11-25 22:52:22.643: E/Database(12209):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-25 22:52:22.643: E/Database(12209):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-25 22:52:22.643: E/Database(12209):  at dalvik.system.NativeStart.main(Native Method)
4
  • What does the whole LogCat say? Commented Nov 25, 2012 at 17:51
  • 2
    Did you tried uninstalling the app and then reinstalling it? Commented Nov 25, 2012 at 18:04
  • @Luksprog yesssss it works. if u plz tel me what was the prolem??? Commented Nov 25, 2012 at 18:09
  • 7
    onCreate() is called the first time the database is created. onUpgrade() is called when the DB_VERSION has changed. So in your case, you had an existing Db with the same version, and hence no modifications were made even after you added the other tables (I'm assuming you did this after you already had one table earlier) Commented Nov 25, 2012 at 18:11

3 Answers 3

1

onUpgrade is called when databse version number is changed. When updating your table you need to increment database version number, specify new table creation query in onCreate method and put ALTER TABLE to onUpgrade method to update previous version of table. When Android detects database version mismatch, it will call onUpgrade method automatically

Sign up to request clarification or add additional context in comments.

Comments

0

Note that onCreate is only called the first time the database physical file is being created. It seems that the onCreate method is not called because the database already exists somewhere in the SDCard. Hence the table SMSFilter is not created. You need to copy the code within onCreate to the onUpgrade method (after the drop table commands). Also, to trigger onUpgrade you must increase the database version.

Comments

0
String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE "
            + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID
            + " INTEGER PRIMARY KEY autoincrement,"
            + ProfileSchedule_NUMBER + " TEXT, "
            + ProfileSchedule_ProfileMode + " TEXT,"
            + ProfileSchedule_CalendarID + "INTEGER)";

Update this to

String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE "
            + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID
            + " INTEGER PRIMARY KEY autoincrement,"
            + ProfileSchedule_NUMBER + " TEXT, "
            + ProfileSchedule_ProfileMode + " TEXT,"
            + ProfileSchedule_CalendarID + " INTEGER)";

Your schedule table create code is not working as in the last line no space b/w integer and column name is creating problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.