从SQLiteDatabase中输入和检索数据

时间:2016-03-23 10:41:35

标签: android sqlite

我想制作闹钟应用。我遇到了一个问题:重新启动设备后,我没有得到任何警报。然后我问到如何解决这个问题,答案是:我应该创建一个数据库,并将所有警报保存在那里,并在重新启动比较警报之后。如果当前时间少于当前时间,则报警'我应该设置一个新的闹钟。好吧,我做了这个并且出错了。请帮我解决。

java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
        at com.example.amadey.myapplication.MainActivity2$1.onClick(MainActivity2.java:52)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

输入闹钟:

AlarmsDataSource datasource = new AlarmsDataSource(this);
        datasource.open();
Alarms alarm = null;
                Long[] alarms = new Long[] { rt,calendar1.getTimeInMillis() };
                alarm = datasource.createComment(alarms[1]);
                alarm = datasource.createComment(alarms[2]);

Alarms.class:

public class Alarms {
    private long id;
    private String alarms;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getAlarms() {
        return alarms;
    }

    public void setAlarms(String alarms) {
        this.alarms = alarms;
    }
}

AlarmsDataSource.class

public class AlarmsDataSource {
    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
            MySQLiteHelper.COLUMN_ALARM };

    public AlarmsDataSource(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Alarms createComment(Long alarm) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_ALARM, alarm);
        long insertId = database.insert(MySQLiteHelper.TABLE_ALARMS, null,
                values);
        Cursor cursor = database.query(MySQLiteHelper.TABLE_ALARMS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        Alarms newAlarms = cursorToAlarm(cursor);
        cursor.close();
        return newAlarms;
    }

    /*public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<Comment>();

        Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
                allColumns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Comment comment = cursorToComment(cursor);
            comments.add(comment);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();
        return comments;
    }*/

    private Alarms cursorToAlarm(Cursor cursor) {
        Alarms alarms = new Alarms();
        alarms.setId(cursor.getLong(0));
        return alarms;
    }
}

MySQLiteHelper.class:

public class MySQLiteHelper extends SQLiteOpenHelper {
    public static final String TABLE_ALARMS = "alarms";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_ALARM = "alarm";
    private static final String DB_PATH = "/DATA/data/db1/databases/";

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

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table "
            + TABLE_ALARMS + "(" + COLUMN_ID
            + " integer primary key autoincrement, " + COLUMN_ALARM
            + " text not null);";

    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) {

    }
    public void openDataBase() throws SQLException {
        //Open the database
        String myPath = DB_PATH + DATABASE_NAME;
        SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }
}

这是我检索警报的方式:

public class MyReceiver extends BroadcastReceiver {
    MySQLiteHelper mySQLiteHelper;
    public String COLUMN_ID = MySQLiteHelper.COLUMN_ID;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            //context.startService(new Intent(context, MyService.class));
            mySQLiteHelper = new MySQLiteHelper(context);
            mySQLiteHelper.openDataBase();

            getData(context); //this is the method to query
            mySQLiteHelper.close();
        }
    }
    public void getData(Context context) {

        final String TABLE_NAME = MySQLiteHelper.TABLE_ALARMS;

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db  = mySQLiteHelper.getReadableDatabase();
        Cursor cursor      = db.rawQuery(selectQuery, new String[]{COLUMN_ID});
        Long[] data      = null;
        Intent intent = new Intent(context,MyReceiver1.class);
        Intent intent1 = new Intent(context,MyReceiver2.class);
        PendingIntent pi = PendingIntent.getBroadcast(context,0,intent,0);
        PendingIntent pi1 = PendingIntent.getBroadcast(context,0,intent1,0);
        if (cursor.moveToFirst()) {
            do {
                int keyRowIdColumnIndex = cursor.getColumnIndex(COLUMN_ID);
                data[keyRowIdColumnIndex] = cursor.getLong(keyRowIdColumnIndex);

                }while (cursor.moveToNext());
            }
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (data[1]>System.currentTimeMillis()){

                am.set(AlarmManager.RTC,data[1],pi);
            am.set(AlarmManager.RTC,data[2],pi1);
        }else if(data[2]>System.currentTimeMillis() && data[1]<System.currentTimeMillis()){
            am.set(AlarmManager.RTC,data[2],pi1);
            context.startService(new Intent(context, MyService2.class));
        }
        cursor.close();
    }

}

帮助我,请求!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

尝试:

alarm = datasource.createComment(alarms[0]);
alarm = datasource.createComment(alarms[1]);

见这里:https://www.cs.umd.edu/~clin/MoreJava/Container/arr-length.html