-3

我有以下用于数据库连接和操作的代码:

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 = 没有这样的表:消息

4

2 回答 2

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); 
}

如果需要,请更改您的数据库名称,因为如果您添加任何表意味着,您的数据库将不会更新。所以在这种情况下,您必须更改数据库名称并检查和版本

于 2013-08-31T10:10:42.643 回答
1

只需将您的DATABSE_VERSION更新为 3...

于 2013-08-31T10:19:46.803 回答