zoukankan      html  css  js  c++  java
  • Head Fisrt Android Development读书笔记(7)Database Persistent

    Creating the database

    Create a new class called TimeTrackerOpenHelper that extends SQLiteOpenHelper. Pass the database name and the database version to super.

    TimeTrackerOpenHelper(Context context)

     super(context, "timetracker.db", null, 1)

    // timetracker.db is the db name, 1 is version

    public void onCreate(SQLiteDatabase database) {

     database.execSQL(

     "create table timerecords " +

     "(id integer primary key, time text, notes text)");

    );

    }

    public void onUpgrade(SQLiteDatabase database) {

     database.execSQL("DROP TABLE IF EXISTS timerecords");

     onCreate(database);

    }

    instead of using execSQL, we can use ContentValues

    ContentValues contentValues = new ContentValues();

    contentValues.put(COLUMN_NAME, VALUE);

    database.insert(TABLE_NAME, null, contentValues);

    query the database

    sqlite queries return cursors

    public Cursor getAllTimeRecords() {

     return database.rawQuery(

     "select * from " + TABLE_NAME, null

    );

    }

    public class TimeTrackerAdapter extends CursorAdapter {

     public TimeTrackerAdapter(Context context, Cursor cursor) {

      super(content, cursor);

     }

    }

  • 相关阅读:
    一本通1647迷路
    一本通1646GT 考试
    矩阵
    矩阵快速幂
    数学基础
    清北学堂学习经验(论颓废)
    钟皓曦第二天讲课
    P3275 [SCOI2011]糖果
    P1270 “访问”美术馆
    P2015 二叉苹果树
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206861.html
Copyright © 2011-2022 走看看