我有以下的數據庫連接代碼和操作:SQLite數據庫連接:表不存在
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);
}
}
我通過調用這個代碼如下:
DBMessagesHandler messageEntry=new DBMessagesHandler(Message.this);
messageEntry.open();
int cnt=messageEntry.getCount(Integer.parseInt(uid));
messageEntry.deleteMessage(Integer.parseInt(uid));
messageEntry.close();
,但我發現,代碼是達不到:
@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, "+
");");
因此,當我看到logcat,我發現錯誤信息不存在這樣的表存在。
請幫幫我。
首先,它要求:公共DBMessagesHandler(上下文C)
然後:公共DBMessagesHandler打開()拋出的SQLException
然後:公共DBHelper(上下文的背景下)
然後:公衆詮釋getCount將(INT UID)
logcat的:
08-31 15:43:14.634:I /數據庫(380):源碼返回:錯誤代碼= 1,MSG =沒有這樣的表:消息
這數據庫版本ar你在用嗎? – Piyush
請在這裏添加你的logcat。 –
private static final int DATABASE_VERSION = 1; –