SQLite没有这样的表异常

时间:2014-01-04 10:53:18

标签: android sql database sqlite

您好,祝你有愉快的一天,

我有一个已填充的数据库,我设法使用特定列的值填充ListActivity,但我的问题是:我想要一个菜单​​项“清除数据”并点击它我的(唯一的)我的数据库中的表已被删除,但我得到的只是

  

01-04 12:43:48.768:E / AndroidRuntime(18563):引起:   android.database.sqlite.SQLiteException:没有这样的表:MAIN_TABLE   (代码1):,编译时:从MAIN_TABLE中选择rowid _id,*

非常感谢任何帮助。我会发布一些代码。如果需要,可以要求更多,谢谢。

public class SavedProfilesSQLiteDb extends SQLiteOpenHelper {

static final String TAG="database";

static final int DATABASE_VERSION=1;
static final String DATABASE_NAME="savedProfilesDB.db";

public SavedProfilesSQLiteDb(Context context, String name,
        CursorFactory factory, int version) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Contract.Columns.CREATE_TABLE);
    Log.d(TAG, "Database created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    Log.d(TAG, "onUpgrade()");
    db.execSQL("DROP TABLE IF EXISTS "+ Contract.Columns.TABLE);
    onCreate(db);

}



static class Contract{
    private Contract(){};

    public static abstract class Columns implements BaseColumns{

    static final String _id="_id";
    static final String TABLE="MAIN_TABLE";
    static final String NAME="NAME_EMPTY";
    static final String PERSONAL_MESSAGE="PERSONAL_MESSAGE_EMPTY";
    static final String FACEBOOK_PAGE="FACEBOOK_PAGE_EMPTY";
    static final String E_MAIL="E_MAIL_EMPTY";
    static final String MOBILE="MOBILE_EMPTY";
    static final String ADDITIONAL_INFORMATION="ADDITIONAL_INFORMATION_EMPTY";

    static final String CREATE_TABLE="CREATE TABLE " + TABLE + "( "+_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "
              + NAME + " TEXT,"
            + PERSONAL_MESSAGE + " TEXT," +FACEBOOK_PAGE+ " TEXT,"+E_MAIL+ " TEXT,"+MOBILE+ " TEXT,"+ADDITIONAL_INFORMATION+ ")";

    static final String ColumnsArray[] ={ NAME,PERSONAL_MESSAGE,FACEBOOK_PAGE,E_MAIL,MOBILE,ADDITIONAL_INFORMATION};
    }


}

}

和drop table Sql语句:

@Override
    public boolean onOptionsItemSelected(MenuItem menuItem){
        int itemId = menuItem.getItemId();
        if (itemId == R.id.Clearhistory) {
            String sql="DROP TABLE IF EXISTS "+SavedProfilesSQLiteDb.Contract.Columns.TABLE;
            db.execSQL(sql);
            finish();
            startActivity(getIntent());
        }

            return true;

}

不要太苛刻,还在学习!!

3 个答案:

答案 0 :(得分:1)

您不能删除不存在的表,这就是您正在执行的操作。对我来说,“清除”应该只删除表中的所有行,而不是删除表。

答案 1 :(得分:0)

static final String CREATE_TABLE="CREATE TABLE " + TABLE + "( "+_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "
          + NAME + " TEXT,"
        + PERSONAL_MESSAGE + " TEXT," +FACEBOOK_PAGE+ " TEXT,"+E_MAIL+ " TEXT,"+MOBILE+ " TEXT,"+ADDITIONAL_INFORMATION+ ")"; is wrong.


ADDITIONAL_INFORMATION column dont have type. so table wont get created 

还有一个问题

while compiling: select rowid _id,* from MAIN_TABLE这表示在删除时没有发生选择语句错误

答案 2 :(得分:0)

使用

String sql = "DELETE FROM " + SavedProfilesSQLiteDb.Contract.Columns.TABLE;

由于您不想删除表,只是删除其内容。