方法openDatabase(String,null)未定义类型Context -----问题是什么

时间:2011-03-31 23:24:48

标签: android

package com.reader;

import java.io.FileNotFoundException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class feedsDB{
    private static final String CREATE_TABLE_FEEDS = "create table feeds (feed_id integer primary key autoincrement, title text not null, url text not null);";
    private static final String CREATE_TABLE_ARTICLES = "create table articles (article_id primary key autoincrement, feed_id text not null, title text not null, url text not null);";


    private static final String FEEDS_TABLE = "feeds";
    private static final String ARTICLES_TABLE ="articles";
    private static final String DATABASE_NAME = "reader";
    private static final int DATABASE_VERSION = 1;

    private SQLiteDatabase db;

    public feedsDB(Context feeder) {

        try{
            db=feeder.openDatabase(DATABASE_NAME, null);}
            catch (FileNotFoundException e) {
                        try {
                                db = feeder.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0, null);
                                db.execSQL(CREATE_TABLE_FEEDS);
                                db.execSQL(CREATE_TABLE_ARTICLES);
                            }  
                        catch (FileNotFoundException e1) {
                                db = null;}

           }
        }

    }

1 个答案:

答案 0 :(得分:2)

openDatabase不是Context上的方法。它是SQLiteDatabase上的静态方法。看起来您可以将代码更改为db = SQLiteDatabase.openDatabase(...)。

我建议您阅读以下链接中的“使用数据库”部分:http://developer.android.com/guide/topics/data/data-storage.html#db。建议创建数据库的方法是扩展SQLiteOpenHelper,覆盖构造函数和onCreate方法。