多个表不适用于android

时间:2013-02-20 02:11:53

标签: android dao android-sqlite

我在网上搜索解决了我的问题,但我找不到任何解决方案,所以这就是我的问题。

我有一个道:

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,那就可以了。

有人知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您遇到问题是因为onCreate()仅在数据库文件不存在时被称为

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

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

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

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

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

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