1

因此,我试图从 MySQLiteHelper 类访问我的自定义函数,该活动从 MyApplication 中提取应用程序范围的数据库,但 Eclipse 说我的方法无法被 SQLiteDatabase 类识别(这就是 db )。如何转移我的方法以便在我的数据库中使用它们?

    public class MyApplication extends Application {

        private static SQLiteDatabase db = null;

        @Override
        public void onCreate() {
            super.onCreate();
        MySQLiteHelper SQLiteHelper = new MySQLiteHelper( this );
        db = SQLiteHelper.getWritableDatabase();
        }

        public static SQLiteDatabase getDB() {
            return db;
        }
    }

这是活动:

    public class MainActivity extends Activity {
        MyApplication app;
        private SQLiteDatabase db;
        int sessionNo;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity);
            app = (MyApplication) getApplication();
            db = MyApplication.getDB();
    // it's complaining about the following line because getLastSession is in my MySQLiteHelper class:
            sessionNo = db.getLastSession() + 1; 
        }

MySQLiteHelper 类的功能:

    public class MySQLiteHelper extends SQLiteOpenHelper {

            public static final String TABLE_RECORDS = "records";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_SESSION = "session";
        public static final String COLUMN_ROUNDNO = "roundno";
        public static final String COLUMN_ARROWS = "arrows";
        public static final String COLUMN_ROUNDSUM = "roundsum";
        public static final String COLUMN_ROUNDAVE = "average";

        private static final String DATABASE_NAME = "records.db";
        private static final int DATABASE_VERSION = 1;

        // Database creation sql statement
        private static final String DATABASE_CREATE = "create table "
                + TABLE_RECORDS + "(" + COLUMN_ID
                + " integer primary key autoincrement, " + COLUMN_SESSION
                + " integer, " + COLUMN_ROUNDNO + " integer, " + COLUMN_ARROWS
                + " integer, " + COLUMN_ROUNDSUM + " integer, " + COLUMN_ROUNDAVE
                + " double " + ");";

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(MySQLiteHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
            onCreate(db);
        }


        public void addRecord(Record record) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(COLUMN_SESSION, record.getSession()); // Contact Name
            values.put(COLUMN_ROUNDNO, record.getRoundno()); // Contact Phone Number
            values.put(COLUMN_ARROWS, record.getArrows()); // Contact Phone Number
            values.put(COLUMN_ROUNDSUM, record.getRoundsum());
            values.put(COLUMN_ROUNDAVE, record.getAverage());

            // Inserting Row
            db.insert(TABLE_RECORDS, null, values);
            db.close(); // Closing database connection
        }

        public ArrayList<Record> getAllRecords() {
            ArrayList<Record> recordList = new ArrayList<Record>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_RECORDS;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Record record = new Record();
                    record.setSession(cursor.getInt(1));
                    record.setRoundno(cursor.getInt(2));
                    record.setArrows(cursor.getInt(3));
                    record.setRoundsum(cursor.getInt(4));
                    record.setAverage(cursor.getDouble(5));


                    recordList.add(record);
                } while (cursor.moveToNext());
            }
            cursor.close();


            return recordList;
        }


        public int getLastSession() {
            SQLiteDatabase db = this.getReadableDatabase();
            String countQuery = "SELECT  * FROM " + TABLE_RECORDS;
            Cursor cursor = db.rawQuery(countQuery, null);
            if (cursor != null) {
                cursor.moveToLast();
                return cursor.getInt(1);
            } else {
                return 0;
            }
        }
    }
4

1 回答 1

0
    private SQLiteDatabase db;
    sessionNo = db.getLastSession() + 1; 

SQLiteDatabase 类中没有 getLastSession() 方法。
这就是您看到错误的原因。
在您进行编辑和评论之后,这是一种干净的方法。
解决方案:

public class DatabaseHelper {

            public static final String TABLE_RECORDS = "records";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_SESSION = "session";
        public static final String COLUMN_ROUNDNO = "roundno";
        public static final String COLUMN_ARROWS = "arrows";
        public static final String COLUMN_ROUNDSUM = "roundsum";
        public static final String COLUMN_ROUNDAVE = "average";

        private static final String DATABASE_NAME = "records.db";
        private static final int DATABASE_VERSION = 1;

        // Database creation sql statement
        private static final String DATABASE_CREATE = "create table "
                + TABLE_RECORDS + "(" + COLUMN_ID
                + " integer primary key autoincrement, " + COLUMN_SESSION
                + " integer, " + COLUMN_ROUNDNO + " integer, " + COLUMN_ARROWS
                + " integer, " + COLUMN_ROUNDSUM + " integer, " + COLUMN_ROUNDAVE
                + " double " + ");";



    private DbHelper helper;
    private SQLiteDatabase database;
    private final Context context;


    private class DbHelper extends SQLiteOpenHelper{
        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(MySQLiteHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
            onCreate(db);
        }       
    }

    // constructor
    public DatabaseHelper(Context c){
        context = c;
    }

    // open
    public DatabaseHelper openDB(){
        helper = new DbHelper(context);
        database = helper.getWritableDatabase();
        return this;
    }

    // close
    public void closeDB(){
        helper.close();
    }

    //
        public void addRecord(Record record) {

            ContentValues values = new ContentValues();
            values.put(COLUMN_SESSION, record.getSession()); // Contact Name
            values.put(COLUMN_ROUNDNO, record.getRoundno()); // Contact Phone Number
            values.put(COLUMN_ARROWS, record.getArrows()); // Contact Phone Number
            values.put(COLUMN_ROUNDSUM, record.getRoundsum());
            values.put(COLUMN_ROUNDAVE, record.getAverage());

            // Inserting Row
            database.insert(TABLE_RECORDS, null, values);
            //database.close();  Use closeDB() instead // Closing database connection
        }

        public ArrayList<Record> getAllRecords() {
            ArrayList<Record> recordList = new ArrayList<Record>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_RECORDS;

            Cursor cursor = database.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Record record = new Record();
                    record.setSession(cursor.getInt(1));
                    record.setRoundno(cursor.getInt(2));
                    record.setArrows(cursor.getInt(3));
                    record.setRoundsum(cursor.getInt(4));
                    record.setAverage(cursor.getDouble(5));


                    recordList.add(record);
                } while (cursor.moveToNext());
            }
            cursor.close();


            return recordList;
        }


        public int getLastSession() {
            String countQuery = "SELECT  * FROM " + TABLE_RECORDS;
            Cursor cursor = database.rawQuery(countQuery, null);
            if (cursor != null) {
                cursor.moveToLast();
                return cursor.getInt(1);
            } else {
                return 0;
            }
        }
}

如何在活动中访问;

        DatabaseHelper entry = new DatabaseHelper(this);
        entry.openDB();
                sessionNo = entry.getLastSession() + 1;
                 entry.closeDB();
于 2012-12-05T01:07:31.080 回答