gpt4 book ai didi

android - 为什么我在保存数据前后得到不同的结果?

转载 作者:行者123 更新时间:2023-11-30 01:27:33 25 4
gpt4 key购买 nike

我想制作一个闹钟应用。实际上,现在,我想将所有警报放入数据库中,并且我已经这样做了,但是当我想检查警报(通过 LogCat)时,我会收到不同的警报。为什么会发生这种情况?请帮帮我!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;
}
}

报警类:

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;
}
}

这就是我第一次设置时间、调用 addAlarm 并获取闹钟的方式

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.MINUTE, timePicker1.getCurrentMinute());
calendar1.set(Calendar.HOUR_OF_DAY,timePicker1.getCurrentHour());
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"));

一段时间后我收到了闹钟

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

当我尝试这个时,我得到了这个结果:

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

最佳答案

替换这个

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

由此

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALARM + " TEXT, "
+ KEY_NAME + " TEXT, UNIQUE ("+KEY_NAME+") ON CONFLICT REPLACE" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);

然后重新安装你的应用...

关于android - 为什么我在保存数据前后得到不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com