我有以下用于数据库连接和操作的代码:
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 时,我发现错误消息不存在这样的表。
请帮我。
首先它调用:public DBMessagesHandler (Context c)
然后:公共 DBMessagesHandler open() 抛出 SQLException
然后:公共 DBHelper(上下文上下文)
然后: public int getCount(int uid)
日志猫:
08-31 15:43:14.634:I/Database(380):sqlite 返回:错误代码 = 1,msg = 没有这样的表:消息