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

     }

    }

  • 相关阅读:
    【8-21】java学习笔记03
    【每天一点点】
    【8-20】java学习笔记02
    【8-19】java学习笔记01
    【8-18】JS学习01
    【8-17】HTML测试
    Selenium之WebDriverWait
    Selenium之XPATH定位方法
    Fiddler用法
    微服务架构系统的测试
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206861.html
Copyright © 2011-2022 走看看