Do final project, I was doing a simple schedule management software, designed with two tables:
Agenda (agenda) and schedule type (agendatype)
I would like how to build a database table in the program do?
Schedule reference type table, ask how to build it?
Please advise, thank you very much!
(I myself built two DAO class, do not know that right. Please advise)
The first one is 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 & lt; Agenda & gt; getAll () {
SQLiteDatabase db = this.getReadableDatabase ();
Cursor cursor = db.rawQuery ("select * from" + TABLE_NAME, null);
ArrayList & lt; Agenda & gt; 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 & lt; Agenda & gt; toArrayList (Cursor c) {
ArrayList & lt; Agenda & gt; arr = new ArrayList & lt; Agenda & gt; ();
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;
}
}
The second is 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 & lt; AgendaType & gt; getAll () {
SQLiteDatabase db = this.getReadableDatabase ();
Cursor cursor = db.rawQuery ("select * from" + TABLE_NAME, null);
ArrayList & lt; AgendaType & gt; 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 & lt; AgendaType & gt; toArrayList (Cursor c) {
ArrayList & lt; AgendaType & gt; arr = new ArrayList & lt; AgendaType & gt; ();
while (c.moveToNext ()) {
AgendaType a = new AgendaType ();
a.setTypeId (c.getInt (0));
a.setTypeName (c.getString (1));
arr.add (a);
}
return arr;
}
}
Reply:
I hope you enlighten ah ~ ~! Really I do not understand ah. . .
Reply:
After you run the program look at your database files right on the line
Reply:
Will you be able to say it in detail? How to see it
Reply:
1 generally built directly into a succession SQLiteOpenHelper implementation class can be, because you only need to construct a db file, multiple tables, it is entirely enough, you that the whole of the two tables completely unnecessary.
2 As to view the db file, it wants to see the next SQLiteSpy software.
3 other should be no problem, update the database, then change the values DATABASE_VERSION on the line, sql, then run without mistakes no problem yourself, but I generally use a third-party package, through the bean using annotations to operate the database, more convenient.
Reply:
Reply:
To data / data / your application / db / directory inside the database file you created
Lead out with
http://sourceforge.net/projects/sqlitebrowser/
Reply:
sqlite is nothing more than building a database built table but I would say you write the program can add a comment? Not read, it is very difficult to understand. I have always felt that write programs without comment, as if the toilet does not flush the toilet, like ......
No comments:
Post a Comment