-3

I have following code for DB connection and operations:

public class DBMessagesHandler 
{

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

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

    // Contacts table name


    private static final String TABLE_MESSAGES = "Messages";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_MID = "mid";
    private static final String KEY_UID = "uid";
    private static final String KEY_MESSAGE = "message";
    private static final String KEY_NAME = "name";



    private DBHelper  ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDB;

private static class DBHelper extends SQLiteOpenHelper{


        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            db.execSQL( "create table "+TABLE_MESSAGES+ " ( "+
                    KEY_MID+" integer primary key autoincrement,"  +           
                    KEY_UID+" integer NOT NULL, "+
                    KEY_MESSAGE+" TEXT NOT NULL, "+
                    ");");

        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int arg2) {
            // TODO Auto-generated method stub

            db.execSQL("drop table if exists"+ TABLE_MESSAGES );
            onCreate(db);
        }



    }


    public DBMessagesHandler (Context c)
    {
        ourContext=c;

    }

    public DBMessagesHandler open() throws SQLException
    {
        ourHelper=new DBHelper(ourContext);
        ourDB=ourHelper.getWritableDatabase();
        return this;    
    }

    public void close()
    {
        ourHelper.close();
    }

     // Add Messages
    public long addMessage(int uid,String message)
    {
        ContentValues cv=new ContentValues();
        cv.put(KEY_UID, uid);
        cv.put(KEY_MESSAGE, message);

        return ourDB.insert(TABLE_MESSAGES, null, cv);
    }


    public int getCount(int uid)
    {
        String query = "select count(*) from "+TABLE_MESSAGES+ " WHERE uid=?";

        Cursor c = ourDB.rawQuery(query,null);

        return c.getCount(); 
    }


    // Add Messages
    public void deleteMessage(int uid)
    {

        String ALTER_TBL ="delete from " + TABLE_MESSAGES +
                 " where "+KEY_ID+" in (select "+ KEY_ID +" from "+ TABLE_MESSAGES+" order by _id LIMIT 5) and "+KEY_UID+" = "+uid+";";

        ourDB.execSQL(ALTER_TBL);
    }

}

I am calling this Code through following:

 DBMessagesHandler messageEntry=new DBMessagesHandler(Message.this);
                    messageEntry.open();
                    int cnt=messageEntry.getCount(Integer.parseInt(uid));
                    messageEntry.deleteMessage(Integer.parseInt(uid));
messageEntry.close();

But i am finding that, code is not reaching upto :

@Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub

                db.execSQL( "create table "+TABLE_MESSAGES+ " ( "+
                        KEY_MID+" integer primary key autoincrement,"  +           
                        KEY_UID+" integer NOT NULL, "+
                        KEY_MESSAGE+" TEXT NOT NULL, "+
                        ");");

Hence when i see logcat, i finds error messsage no such table exists.

Please help me.

First it calls: public DBMessagesHandler (Context c)

Then : public DBMessagesHandler open() throws SQLException

Then: public DBHelper(Context context)

Then : public int getCount(int uid)

LOGCAT:

08-31 15:43:14.634: I/Database(380): sqlite returned: error code = 1, msg = no such table: Messages

C Sharper
  • 8,284
  • 26
  • 88
  • 151

2 Answers2

1

I think your code oncreate needs some slight modification,i will post my code you look at my code and edit according to your need

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String DATABASE_CREATE_PROJ = "CREATE TABLE " +  DATABASE_TABLE_PROJ + "( "
                + CATEGORY_COLUMN_ID + " integer primary key, "
                + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer, " + CATEGORY_COLUMN_DATE + " integer );" ;
                db.execSQL(DATABASE_CREATE_PROJ); 
}

Change your db name if its requires,because if you add any table means,your db will not update.so for that case you have to change the db name and check and version also

Karthick M
  • 769
  • 2
  • 11
  • 29
  • but its not hitting debugger on oncreate method – C Sharper Aug 31 '13 at 10:13
  • can you try like this in your code,is it your code creating problem in database actiivity oncreate or your javafile acivity oncreate – Karthick M Aug 31 '13 at 10:15
  • 08-31 15:43:14.634: I/Database(380): sqlite returned: error code = 1, msg = no such table: Messages – C Sharper Aug 31 '13 at 10:15
  • you please change your database name and change version 3 or 5 and write like my code your oncreae,table is not creating – Karthick M Aug 31 '13 at 10:21
  • sir, by changing DBName, it worked, but i still havent understood how??? and i have another tables in existing databse which i may have to use in future..what can i do in such a case? – C Sharper Aug 31 '13 at 10:24
  • 1
    actually in one db itself we can able to create more table,but that the thing is if you add any column in your table later it will gives problem because db will not update so in that case you have to change your db name and you have to check whether have u given all the thing properly. – Karthick M Aug 31 '13 at 10:26
1

Just Update your DATABSE_VERSION to 3...

Piyush
  • 18,895
  • 5
  • 32
  • 63