dimanche 27 mars 2016

Why I get different result before and after saving data?

I want to make an alarm clock app. Actually now, I want to put all my alarms in database and I've already done this but when I want to check alarms(through LogCat) I get different alarms.Why does this happen? Help me please! mySQLiteHelper.class:

public class mySQLiteHelper extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    public static final int DATABASE_VERSION = 2;

    // Database Name
    private static final String DATABASE_NAME = "alarms.db";

    // Contacts table name
    private static final String TABLE_ALARMS = "alarms";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_ALARM = "alarm";
    private static final  String KEY_NAME = "name";

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

    // Creating Tables"create table alarms ("
    //+ "id integer primary key,"
      //      + "alarm text,"
        //    + "name text," // added a ','
          //  + "alarmname text" + ");"
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALARM + " TEXT, "
                +  KEY_NAME + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations"," + alarm.getName() +,name
     */

    // Adding new contact
    void addAlarm(Alarm alarm) {
        SQLiteDatabase db = this.getWritableDatabase();
        String sql =
                "INSERT or replace " +
                        "INTO alarms (alarm,"+KEY_NAME+") " +
                        "VALUES("+ alarm.getAlarm() +  ",'" + alarm.getName() + "')";
        //String sql =
          //      "INSERT or replace INTO alarms (alarm,alarmname) VALUES("+ alarm.getAlarm() + ",'" + alarm.getName() + "')" ;
        db.execSQL(sql);
        db.close(); // Closing database connection
    }

    // Getting single contact
    String getAlarm(String alarmname) {
        String query = "Select * FROM " + TABLE_ALARMS + " WHERE " + KEY_NAME + " =  \"" + alarmname + "\"";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.getCount()>0)
            cursor.moveToFirst();
        String alarm = cursor.getString(cursor.getColumnIndex(KEY_ALARM));
        cursor.close();
        // return contact
        return alarm;
    }

    // Deleting single contact
    public void deleteAlarm(Alarm alarm) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_ALARMS, KEY_ID + " = ?",
                new String[] { String.valueOf(alarm.getID()) });
        db.close();
    }


    // Getting contacts Count
    public int getAlarmsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_ALARMS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int i = cursor.getCount();
        cursor.close();

        // return count
        return i;
    }
}

Alarm.class:

public class Alarm {
    //private variables
    int _id;
    String _name;
    String _alarm;

    // Empty constructor
    public Alarm(String alarm,String alarmname){
        this._alarm = alarm;
        this._name = alarmname;
    }
    // constructor
    public Alarm(int id, String name, String alarm){
        this._id = id;
        this._name = name;
        this._alarm = alarm;
    }

    // constructor
    public Alarm(String alarm){
        this._alarm = alarm;
    }
    // getting ID
    public int getID(){
        return this._id;
    }

    // getting name
    public String getName(){
        return this._name;
    }

    // getting phone number
    public String getAlarm(){
        return this._alarm;
    }
}

This is how I call addAlarm and get my alarms for the first time

String alarm1 = Long.toString(calendar.getTimeInMillis());
                String alarm2 = Long.toString(calendar1.getTimeInMillis());
                Log.i("Alarm1.1",alarm1);
                Log.i("Alarm2.1",alarm2);
                mySQLiteHelper mySQLiteHelper = new mySQLiteHelper(MainActivity2.this);
                Log.i("Point_version",Integer.toString(mySQLiteHelper.DATABASE_VERSION));
                mySQLiteHelper.addAlarm(new Alarm(alarm1,"alarm1"));
                mySQLiteHelper.addAlarm(new Alarm(alarm2,"alarm2"));

And after some time I get my alarms

String alarm1 = mySQLiteHelper.getAlarm("alarm1");
        String alarm2 = mySQLiteHelper.getAlarm("alarm2");
        Log.i("Alarm1.2",alarm1);
        Log.i("Alarm2.2",alarm2);

When I'd tried this I got this results:

Alarm1.1﹕ 1459098157713
Alarm2.1﹕ 1459098230836
Alarm1.2﹕ 1459095666194
Alarm2.2﹕ 1459095664808

Aucun commentaire:

Enregistrer un commentaire