2011-02-14 44 views
3

我已经用open()方法构建了一个数据库辅助类,并且用onCreate()覆盖了扩展的sqlite帮助器。 (如下所示)。尽管如此,我得到'SQLiteException,没有这样的表'的错误。我不明白,为什么openHelper没有帮助?没有这样的表SQLite Android

public void open() { 
    try{ 
     db = openHelper.getWritableDatabase(); 
    } catch (SQLiteException e) { 
     db = openHelper.getReadableDatabase(); 
    } 
} 

//other stuff 

public static final String database_create = "create table " + database_table + " (" + primary_key + " integer primary key autoincrement, " 
    + company_column + " text not null, " + product_column + " text not null);"; 

    @Override 
    public void onCreate(SQLiteDatabase _db) { 
     _db.execSQL(database_create); 
    } 

下面的代码是为了临时插入一个条目,因为数据库不能为其他原因为空。这似乎完美地执行,但代码的最后位,其中来自后就是引发错误

CompanyAndProductDatabaseAdapter cpdAdapter = new CompanyAndProductDatabaseAdapter(this); 
    cpdAdapter.open(); 
    errorguard = cpdAdapter.insertPair("Loading", "..."); 
    cpdAdapter.close(); 

//other stuff 

cpdAdapter.open(); 
    Cursor cursor = cpdAdapter.getAllPairsCursor(); //error here 
    cursor.requery(); 
    startManagingCursor(cursor); 
+0

那怎样?如果你解决了它。请告诉它。并考虑接受答案 – Beasly 2011-02-14 19:02:15

回答

2

我不知道为什么你实现了一个open - 方法,也database_create是不是应该是什么。
我假设第一个代码是CompanyAndProductDatabaseAdapter的一部分。

到这里看看:
Android - Sqlite database method undefined fot type

这是几乎所有你需要创建/获得DB与inherted SQLiteOpenHelper

+0

感谢您的帮助。问题在于数据库开放助手似乎不愿意在同一个数据库中创建两个表。简单地指定一个新的数据库解决了一切我的创建语句虽然不正确?它现在似乎工作,但如果它可以变得更高效,我会喜欢它。 – reader1 2011-02-15 02:49:07

1

你的问题是这样的功能:

db = openHelper.getWritableDatabase(); 
    db = openHelper.getReadableDatabase(); 

首先:检查数据库的您的路径/名称是正确的。如果没有找到数据库,它可以创建一个默认数据库,一个空数据库(没有表,没有)。

二:尝试打开你的数据库是这样的:

String myPath = DB_PATH + DB_NAME; 
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); // or OPEN_READONLY, depending on situation.