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



  • 相关阅读:
    jquery-easyUI第一篇【介绍、入门、使用常用的组件】
    Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】
    Lucene第一篇【介绍Lucene、快速入门】
    Oracle总结第三篇【PLSQL】
    Oracle总结第二篇【视图、索引、事务、用户权限、批量操作】
    Oracle卸载
    纳税服务系统【统计图Fusionchart】
    纳税服务系统【自动受理,Quartz任务调度】
    纳税服务系统【投诉受理管理,显示投诉信息、处理回复、我要投诉】
    导航条样式代码
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2366200.html
Copyright © 2011-2022 走看看