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



  • 相关阅读:
    POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)
    UVaLive 5031 Graph and Queries (Treap)
    Uva 11996 Jewel Magic (Splay)
    HYSBZ
    POJ 3580 SuperMemo (Splay 区间更新、翻转、循环右移,插入,删除,查询)
    HDU 1890 Robotic Sort (Splay 区间翻转)
    【转】ACM中java的使用
    HDU 4267 A Simple Problem with Integers (树状数组)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4417 Super Mario (树状数组/线段树)
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2366200.html
Copyright © 2011-2022 走看看