0
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);
    }

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

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

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

结果还是一样。

4

1 回答 1

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);
    }

}
于 2012-05-08T18:56:44.790 回答