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

     }

    }

  • 相关阅读:
    Skimage=scikit-image SciKit 包的模块(转载)
    python day12
    python day11
    python day10
    python day9
    python day8
    python day7
    python day6
    python 第五天
    python 第四天
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206861.html
Copyright © 2011-2022 走看看