0

我已经在网上搜索以解决我的问题,但我找到的解决方案都没有为我工作,所以这是我的问题。

我有道:

public class AqntCategoryDAO extends SQLiteOpenHelper{


public static abstract class AqntCategoryEntry implements BaseColumns {
    public static final String TABLE_NAME = "category";
    public static final String COLUMN_NAME_NAME = "name";
    public static final String COLUMN_NAME_TAG = "tag";
    public static final String COLUMN_NAME_STATUS = "status";
}

//globals
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AquiNaoTemPlaces.db";

public static String[] columns = {
    AqntCategoryEntry._ID,
    AqntCategoryEntry.COLUMN_NAME_NAME,
    AqntCategoryEntry.COLUMN_NAME_TAG,
    AqntCategoryEntry.COLUMN_NAME_STATUS
    };

public AqntCategoryDAO(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    StringBuffer sql_create = new StringBuffer();
    sql_create.append("CREATE TABLE IF NOT EXISTS "+AqntCategoryEntry.TABLE_NAME);
    sql_create.append("( ");
        sql_create.append(AqntCategoryEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT,");
        sql_create.append(AqntCategoryEntry.COLUMN_NAME_NAME+" TEXT,");
        sql_create.append(AqntCategoryEntry.COLUMN_NAME_TAG+" TEXT,");
        sql_create.append(AqntCategoryEntry.COLUMN_NAME_STATUS+" INTEGER");
    sql_create.append(" )");
    Log.i("SQL: ",sql_create.toString());
    db.execSQL(sql_create.toString());
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + AqntCategoryEntry.TABLE_NAME);
    onCreate(db);
}
...

和:

public class AqntPlacesDAO extends SQLiteOpenHelper{


public static abstract class AqntPlacesEntry implements BaseColumns {
    public static final String TABLE_NAME = "places";
    public static final String COLUMN_NAME_NAME = "name";
    public static final String COLUMN_NAME_LATITUDEE6 = "latitudeE6";
    public static final String COLUMN_NAME_LONGITUDEE6 = "longitudeE6";
    public static final String COLUMN_NAME_RATE = "rate";
    public static final String COLUMN_NAME_STATUS = "status";
}

//globals
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AquiNaoTemPlaces.db";

public static String[] columns = {
    AqntPlacesEntry._ID,
    AqntPlacesEntry.COLUMN_NAME_NAME,
    AqntPlacesEntry.COLUMN_NAME_LATITUDEE6,
    AqntPlacesEntry.COLUMN_NAME_LONGITUDEE6,
    AqntPlacesEntry.COLUMN_NAME_RATE,
    AqntPlacesEntry.COLUMN_NAME_STATUS
    };

public AqntPlacesDAO(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    StringBuffer sql_create = new StringBuffer();
    sql_create.append("CREATE TABLE IF NOT EXISTS "+AqntPlacesEntry.TABLE_NAME);
    sql_create.append("( ");
        sql_create.append(AqntPlacesEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT,");
        sql_create.append(AqntPlacesEntry.COLUMN_NAME_NAME+" TEXT,");
        sql_create.append(AqntPlacesEntry.COLUMN_NAME_LATITUDEE6+" TEXT,");
        sql_create.append(AqntPlacesEntry.COLUMN_NAME_LONGITUDEE6+" TEXT,");
        sql_create.append(AqntPlacesEntry.COLUMN_NAME_RATE+" REAL,");
        sql_create.append(AqntPlacesEntry.COLUMN_NAME_STATUS+" INTEGER");
    sql_create.append(" )");
    Log.i("SQL: ",sql_create.toString());
    db.execSQL(sql_create.toString());
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF `EXISTS` " + AqntPlacesEntry.TABLE_NAME);
    onCreate(db);
}
...

当我尝试

AqntPlacesDAO daoPlace = new AqntPlacesDAO(context);
AqntCategoryDAO catAuxDAO = new AqntCategoryDAO(context);

List<AqntPlaces> places = daoPlace.getPlaces();
List<AqntCategory> catss = catAuxDAO.getCategories();
daoPlace.close();
catAuxDAO.close();

它只创建第一个,另一个向我显示错误:

02-19 22:51:29.083: E/AndroidRuntime(9336): android.database.sqlite.SQLiteException: no such table: category: , while compiling: SELECT _id, name, tag, status FROM category

但如果我只运行一个 DAO,它就可以工作。

有人知道我在做什么错吗?

4

1 回答 1

1

您遇到问题是因为仅在数据库文件不存在时才onCreate()调用。

您的两个 DAO 共享数据库文件AquiNaoTemPlaces.db,但只有实际使用的第一个会导致其onCreate()方法被调用。在您的情况下,它是“地方”DAO - 它将在这一行的引擎盖下初始化:

List<AqntPlaces> places = daoPlace.getPlaces();

之后,该方法将初始化数据库文件AqntPlacesDAO.onCreate()。当 VM 执行该行时:

List<AqntCategory> catss = catAuxDAO.getCategories();

数据库文件已经存在并被初始化,所以AqntCategoryDAO.onCreate() 不会被调用。

解决方案是每个数据库文件使用一个 SQLiteOpenHelper子类(我们称之为)。Helper当然,您可以Helper为每个逻辑 DAO 子类化 - 设计取决于您。这里的关键是Helper.onCreate()/onUpgrade()应该创建/修改存储在底层数据库中的所有表。

于 2013-02-20T02:28:03.057 回答