openOrCreateDatabase错误

时间:2012-05-08 18:47:16

标签: android

public class DBActivities {
    private final String tableName = "Table_SMS";
    private final String CREATE_TABLE_SMS = "CREATE TABLE " + tableName + "(" + "db_date TEXT " + "db_sms_count INTEGER);";
    private SQLiteDatabase db;

    public DBActivities() {
        DoDBInitialization();
    }

    private void DoDBInitialization() {
        db = openOrCreateDatabase(tableName, 0, null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
    }

我收到错误“方法openOrCreateDatabase(String,int,null)未定义类型DBActivities”。这可能是什么问题? DBActivities类正由主类用于所有与数据库相关的活动。

我做了一些研究,并尝试使用:

db = openOrCreateDatabase(tableName, null, Context.MODE_PRIVATE);

结果仍然相同。

1 个答案:

答案 0 :(得分:1)

这是一个基本的java错误。你试图调用一个不存在的函数。

openOrCreateDatabase(...)未在您的代码中定义。

请参阅本教程:http://www.vogella.com/articles/AndroidSQLite/article.html

您可能希望使用以下内容扩展SQLiteOpenHelper:

private static class DbHelper extends SQLiteOpenHelper {


    public DbHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //called when the version of the existing db is less than the current
        Log.w(this.getClass().getName(), "Upgrading db from "+oldVersion+" to "+newVersion);
        db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
        onCreate(db);
    }

}