插入sqlite时自动递增'id'值 [英] Auto incrementing 'id' value when inserting into sqlite
本文介绍了插入sqlite时自动递增'id'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何以自动增量的方式将id值输入数据库,即根据输入的内容自动将id设置为self.
I'm trying to figure out how to get id value into database in an auto increment way, i.e it sets it self automatically depending on what entry is.
这是我的DatabaseHandler代码当前的外观(仅重要位)
here is how my DatabaseHandler code looks at the moments (only important bits)
// Adding new appointment
void addAppointment(Appointment appointment) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, appointment.getId()); // Appointment ID
values.put(KEY_TITLE, appointment.getTitle()); // Appointment Title
values.put(KEY_DETAILS, appointment.getDetails()); // Appointment Details
values.put(KEY_DATE, appointment.getDate()); // Appointment Date
values.put(KEY_TIME, appointment.getTime()); // Appointment Time
// Inserting Row
db.insert(TABLE_APPOINTMENTS, null, values);
db.close(); // Closing database connection
}
如您所见,我在其中使用了不带id的构造函数,它始终为id返回0,因此返回错误,因为id必须为PRIMARY KEY must be unique
as you can see I use constructor without id in it, this always returns 0 for id, thus returns an error as id must be PRIMARY KEY must be unique
如何使其自动递增?
推荐答案
删除
values.put(KEY_ID, appointment.getId()); // Appointment ID
未指定PRIMARY KEY
时,值是自动生成的.
这篇关于插入sqlite时自动递增'id'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文