我有以下数据库连接和操作代码:
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)
然后:public DBMessagesHandler open()抛出SQLException
然后:public DBHelper(Context context)
然后:public int getCount(int uid)
logcat的:
08-31 15:43:14.634:I / Database(380):sqlite返回:错误代码= 1,msg =没有这样的表:消息
答案 0 :(得分:1)
我认为您的代码oncreate需要稍加修改,我会发布我的代码,您可以查看我的代码并根据需要进行编辑
@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);
}
如果需要,请更改您的数据库名称,因为如果添加任何表意味着,您的数据库将不会更新。因此,您必须更改数据库名称并检查和版本
答案 1 :(得分:1)
只需将 DATABSE_VERSION 更新为3 ...