SQLite Database connection : Table does not exists
I have following code for DB connection and operations:
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.
No comments:
Post a Comment