zoukankan      html  css  js  c++  java
  • Android SQLite 示例

    实体类:
    public class MyEntry
    {
    public String title;
    public String location;
    public String description;
    public Calendar startCalendar;
    public Calendar endCalendar;
    public String entryID;

    public MyEntry(String entryID, String title, String location, String description, Calendar startCalendar, Calendar endCalendar)
    {
    this.entryID = entryID;
    this.title = title;
    this.location = location;
    this.description = description;
    this.startCalendar = Calendar.getInstance();
    this.endCalendar = Calendar.getInstance();

    this.startCalendar.setTimeInMillis(startCalendar.getTimeInMillis());
    this.endCalendar.setTimeInMillis(endCalendar.getTimeInMillis());
    }
    }
    SQLHelper类:
    public class CalendarSQLHelper extends SQLiteOpenHelper
    {
    public static String tableName = "toAdd";
    public static String idRow = "id";
    public static String titleRow = "title";
    public static String locationRow = "location";
    public static String descriptionRow = "description";
    public static String startTimeRow = "startTime";
    public static String endTimeRow = "endTime";

    public CalendarSQLHelper(Context context)
    {
    super(context, "smarhit_db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public void createTable()
    {
    SQLiteDatabase db = getWritableDatabase();
    String sqlStr = "CREATE TABLE IF NOT EXISTS "
    + tableName
    + " ("/*+ID+" INTEGER PRIMARY KEY,"*/
    + idRow
    + " INTEGER PRIMARY KEY,"
    + titleRow
    + " VARCHAR,"
    + locationRow
    + " VARCHAR,"
    + descriptionRow
    + " VARCHAR,"
    + startTimeRow
    + " VARCHAR,"
    + endTimeRow
    + " VARCHAR)";
    db.execSQL(sqlStr);
    db.close();
    }

    public void insert(MyEntry entry)
    {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues value = new ContentValues();

    value.put(titleRow, entry.title);
    value.put(locationRow, entry.location);
    value.put(descriptionRow, entry.description);
    value.put(startTimeRow, entry.startCalendar.getTimeInMillis());
    value.put(endTimeRow, entry.endCalendar.getTimeInMillis());

    db.insert(tableName, null, value);

    db.close();
    }

    public void update(String id, MyEntry entry)
    {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(titleRow, entry.title);
    values.put(locationRow, entry.location);
    values.put(descriptionRow, entry.description);
    values.put(startTimeRow, entry.startCalendar.getTimeInMillis() + "");
    values.put(endTimeRow, entry.endCalendar.getTimeInMillis() + "");

    db.update(tableName, values, idRow + "=?", new String[] {id});
    db.close();
    }

    public int getLastId()
    {
    SQLiteDatabase db = getReadableDatabase();

    Cursor c = db.query(tableName,
    new String[] {},
    "",
    new String[] {},
    null,
    null,
    "");

    if(c.getCount() == 0)
    {
    db.close();
    return 0;
    }
    else
    {
    c.moveToLast();
    int id = c.getInt(0);
    db.close();
    return id;
    }
    }

    public List<MyEntry> getAllRow()
    {
    SQLiteDatabase db = getReadableDatabase();

    List<MyEntry> entries = new ArrayList<MyEntry>();

    Cursor c = db.query(tableName,
    new String[] {},
    "",
    new String[] {},
    null,
    null,
    "");

    c.moveToFirst();

    for(int n=0; n<c.getCount(); n++)
    {
    long time = Long.parseLong(c.getString(4));
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();

    startCalendar.setTimeInMillis(time);
    time = Long.parseLong(c.getString(5));
    endCalendar.setTimeInMillis(time);

    MyEntry entry = new MyEntry(c.getString(0), c.getString(1), c.getString(2), c.getString(3), startCalendar, endCalendar);
    entries.add(entry);
    c.moveToNext();
    }

    db.close();

    return entries;
    }

    public void deleteTable()
    {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS " + tableName);
    db.close();
    }
    }



  • 相关阅读:
    课后作业10.13
    大道至简:软件工程实践者的思想 读后感
    课程作业01
    动手动脑10.13
    动手动脑
    js矢量图类库:Raphaël—JavaScript Library
    OSGi bundle之间互相通信的方法
    OSGi bundle 与 fragment
    Spring.DM web 开发环境搭建
    Spring.DM版HelloWorld
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2366200.html
Copyright © 2011-2022 走看看