使用MyApplication类在SQLiteDatabase上使用MySQLiteHelper的方法

时间:2012-12-05 00:56:06

标签: android sql

所以我试图从一个活动中从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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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

SQLiteDatabase class中没有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();