做期末项目中,本人在做一个简单的日程管理软件,设计出来有两张表:
日程(agenda)和日程类型(agendatype)
本人想怎么在程序中往数据库中建表呢?
日程表引用类型表,问一下该怎么建呢?
请大家指点,非常感谢!
(本人本人建了2个DAO类,不知道这样对不对。请大家指点)
第一个是AgendaDAO
日程(agenda)和日程类型(agendatype)
本人想怎么在程序中往数据库中建表呢?
日程表引用类型表,问一下该怎么建呢?
请大家指点,非常感谢!
(本人本人建了2个DAO类,不知道这样对不对。请大家指点)
第一个是AgendaDAO
package com.hj.db.dao; import java.util.ArrayList; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import com.hj.db.model.*; public class AgendaDAO extends SQLiteOpenHelper { private final static String DATABASE_NAME = "_AgendaManagement"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "_agenda"; private final static String FIELD_ID = "_id"; private final static String FIELD_TITLE = "_title"; private final static String FIELD_CONTENT = "_content"; private final static String FIELD_AGENDATIME = "_agendatime"; private final static String FIELD_AGENDADATE = "_agendadate"; private final static String FIELD_ALARMTIME = "_alarmtime"; private final static String FIELD_ALARMDATE = "_alarmdate"; private final static String FIELD_SETALARM = "_setalarm"; private SQLiteDatabase db; public AgendaDAO(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "Create table " + TABLE_NAME + "(" + FIELD_ID + " integer primary key autoincrement," + FIELD_TITLE + "," + FIELD_CONTENT + "," + FIELD_AGENDATIME + "," + FIELD_AGENDADATE + "," + FIELD_ALARMTIME + "," + FIELD_ALARMDATE + "," + FIELD_SETALARM + " integer," + "foreign key(_typeid) references _agendatype(_typeid) " + ")"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = " DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(sql); onCreate(db); db.execSQL(sql); } public ArrayList<Agenda> getAll() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from "+TABLE_NAME, null); ArrayList<Agenda> agendas = toArrayList(cursor); db.close(); return agendas; } public void delete(int id) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_ID + "=?"; String[] whereValue = { Integer.toString(id) }; db.delete(TABLE_NAME, where, whereValue); db.close(); } public void update(Agenda a) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_ID + "=?"; String[] whereValue = { Integer.toString(a.getId()) }; ContentValues cv = new ContentValues(); cv.put(FIELD_TITLE, a.getTitle()); cv.put(FIELD_CONTENT, a.getContent()); cv.put(FIELD_AGENDATIME, a.getAgendaTime()); cv.put(FIELD_AGENDADATE, a.getAgendaDate()); cv.put(FIELD_ALARMTIME, a.getAlarmTime()); cv.put(FIELD_ALARMDATE, a.getAlarmDate()); cv.put(FIELD_SETALARM, a.getSetAlarm()); cv.put("_typeid", a.getTypeId()); db.update(TABLE_NAME, cv, where, whereValue); db.close(); } public long insert(Agenda a) { SQLiteDatabase db = this.getReadableDatabase(); ContentValues cv = new ContentValues(); cv.put(FIELD_TITLE, a.getTitle()); cv.put(FIELD_CONTENT, a.getContent()); cv.put(FIELD_AGENDATIME, a.getAgendaTime()); cv.put(FIELD_AGENDADATE, a.getAgendaDate()); cv.put(FIELD_ALARMTIME, a.getAlarmTime()); cv.put(FIELD_ALARMDATE, a.getAlarmDate()); cv.put(FIELD_SETALARM, a.getSetAlarm()); cv.put("_typeid", a.getTypeId()); long row = db.insert(TABLE_NAME, null, cv); db.close(); return row; } private ArrayList<Agenda> toArrayList(Cursor c) { ArrayList<Agenda> arr = new ArrayList<Agenda>(); while (c.moveToNext()) { Agenda a = new Agenda(); a.setId(c.getInt(0)); a.setTitle(c.getString(1)); a.setContent(c.getString(2)); a.setAgendaTime(c.getString(3)); a.setAgendaDate(c.getString(4)); a.setAlarmTime(c.getString(5)); a.setAgendaDate(c.getString(6)); a.setSetAlarm(c.getInt(7)); a.setTypeId(c.getInt(8)); arr.add(a); } return arr; } }
第二个是AgendaTypeDAO:
package com.hj.db.dao; import java.util.ArrayList; import com.hj.db.model.Agenda; import com.hj.db.model.AgendaType; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class AgendaTypeDAO extends SQLiteOpenHelper { private final static String DATABASE_NAME = "_AgendaManagement"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "_agendatype"; private final static String FIELD_TYPEID = "_typeid"; private final static String FIELD_TYPENAME = "_typename"; private SQLiteDatabase db; public AgendaTypeDAO(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "Create table " + TABLE_NAME + "(" + FIELD_TYPEID + " integer primary key autoincrement," + FIELD_TYPENAME + ")"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = " DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(sql); onCreate(db); } public long insert(AgendaType at) { SQLiteDatabase db = this.getReadableDatabase(); ContentValues cv = new ContentValues(); cv.put(FIELD_TYPENAME, at.getTypeName()); long row = db.insert(TABLE_NAME, null, cv); db.close(); return row; } public void delete(int id) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_TYPEID + "=?"; String[] whereValue = { Integer.toString(id) }; db.delete(TABLE_NAME, where, whereValue); db.close(); } public void update(AgendaType at) { SQLiteDatabase db = this.getWritableDatabase(); String where = FIELD_TYPEID + "=?"; String[] whereValue = { Integer.toString(at.getTypeId()) }; ContentValues cv = new ContentValues(); cv.put(FIELD_TYPENAME, at.getTypeName()); db.update(TABLE_NAME, cv, where, whereValue); db.close(); } public ArrayList<AgendaType> getAll() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null); ArrayList<AgendaType> at = toArrayList(cursor); db.close(); return at; } public AgendaType getById(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where " + FIELD_TYPEID + "=" + id + "", null); AgendaType at = new AgendaType(); if (cursor.moveToNext()) { at.setTypeId(cursor.getInt(0)); at.setTypeName(cursor.getString(1)); } db.close(); return at; } private ArrayList<AgendaType> toArrayList(Cursor c) { ArrayList<AgendaType> arr = new ArrayList<AgendaType>(); while (c.moveToNext()) { AgendaType a = new AgendaType(); a.setTypeId(c.getInt(0)); a.setTypeName(c.getString(1)); arr.add(a); } return arr; } }
解决方案:20分
解决方案:20分