Android SQLite資料庫基礎(1),androidsqlite

來源:互聯網
上載者:User

Android SQLite資料庫基礎(1),androidsqlite

SQLiteOpenHelper類:是一個抽象類別,通過從此類繼承實現使用者類,來提供資料庫開啟、關閉等操作函數。

SQLiteDatabase 類:資料庫訪問類:執行對資料庫的插入記錄、查詢記錄等操作。

SQLiteCursor類: 查詢結構操作類:用來訪問查詢結果中的記錄。

本小節先討論下SQLiteOpenHelper類。

1、必須實現的方法為:“onCreate()”用於建立資料時調用,一般用於做初始化工作。

            “onUpdate()”方法用來更新資料庫。

2、需完成super

3、SQLiteOpenHelper提供方法getReadableDatabase()和getWriteableDatabase()方法來獲得一個SQLiteDatabase對象,在對象可以允許訪問資料庫,而不管是在讀或寫的模式下。

 

 

 

代碼:

package com.example.sqllites.dbs;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/**
*
* @author Lin
* SQLite協助類
*/
public class sqlite_Persondb extends SQLiteOpenHelper{

/**
* sqlite資料庫構造方法,用來定義資料庫名稱,資料庫查詢結果集,資料庫版本
* super了父類的方法
* @param context:為上下文
*/
public sqlite_Persondb(Context context){
super(context,"lin_Person.db",null,1);
}
/**
* sqlite資料庫構造方法,用來定義資料庫名稱,資料庫查詢結果集,資料庫版本
* @param context 傳入上下文參數
* @param name 傳入資料庫名稱
* @param factory 傳入遊標方式,null為預設格式
* @param version 傳入資料庫版本,要求1以上,否則會報錯
*/
public sqlite_Persondb(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
/**
* 第一次建立資料庫調用的方法
* 傳入db為被建立的資料庫,在這裡假定為lin_Person.db
* execSQL建立資料庫表內容,忽略大小寫。
*/
@Override
public void onCreate(SQLiteDatabase db) {
/**
* 文法:create table 表名(列名 類型 設定主鍵(可選) 設定自動成長(可選));
* @param primary key 設定主鍵
* @param autoincrement 自動成長
*/
db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phoneNum varchar(11))");
}

/**
* 升級資料庫
*/

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  //暫忽略
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.