標籤:
SQLite 是一個輕量級的資料庫,常用於各種嵌入式裝置當中。
android 提供了SQLiteOpenHelper的抽象類別用於協助開發資料庫。
在實際使用中經常定義一個類繼承SQLiteOpenHelper,並重寫構造方法,onCreate()方法和 onUpdate()的方法;
//注意:上面三個方法必須重寫:重寫構造方法一般用於初始化
onCreate()方法:
onUpdate()用於資料庫版本的更新時調用,只在資料庫更新時調用一次。
如:MySQLiteHelper類繼承SQLiteOpenHelper
// MySQLiteOpenHelper extends SQLiteOpenHelper;
//MySQLiteOpenHelper myhelper;
myhelper = new MySQLiteOpenHelper(contenext);
** SQLiteDatabase是直接操作資料庫的對象,可以使用SQLiteOpenHelper的 getReadableDatabase
或getWritableDatabase方法得到SQLiteDatabase對象。
//SQLiteDatabase db = myhelper.getReadableDatabase();
//注意:只有調用了getReadableDatabase()或getWritableDatabase方法,才算真正建立了資料庫,接下來就可以使用資料庫的各種方法,
處理資料;
查詢結果的傳回值:Cursor cursor ;Cursor相當於結果集ResulytSet;
Cursor是一個遊標介面,提供了遍曆查詢結果的方法,如移動指標方法move(),獲得列值方法getString()等.
總結:使用SQLite的一般步驟:
a:自訂一個繼承SQLiteOpenHelper的類,然後使用資料庫協助類的對象調用getReadableDatabase()或getWritableDatabase方法建立資料庫
b: 對資料庫db進行增刪改查的操作
c:對於查詢的結果集儲存在Cursor中。
d:資料庫db和結果集Cursor使用完畢後注意調用close的方法來釋放資源
http://wenku.baidu.com/view/423a00dca58da0116c17491e.html
//該文檔比較長比較詳細
package com.itheima.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.view.View;public class PersonSQLiteOpenHelper extends SQLiteOpenHelper { /*資料庫的構造方法, 用來定義資料庫的名稱, 資料庫的結果集和資料庫的版本 建立出一個Person的資料庫 */ public PersonSQLiteOpenHelper(Context context) { super(context, "Person.db", null, 1); /* * 一共四個參數,每個參數的含義:1.context上下文,一般都需要 2:資料庫的名字 遊標工廠:移動的指標指向結果集,一般遊標工廠用null; version:代表資料庫的版本。從1開始的,一般使用1; */ } /* * 在資料庫第一次建立的時候使用onCreate(), * 一般用來建立資料庫的表結構和存一些初始化的表參數 * 傳入的參數db代表被建立的資料庫 * */ @Override public void onCreate(SQLiteDatabase db) { //初始化表結構 db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))"); //注意:SQLite 實際上是一個嵌入式的資料庫,沒有MySQL那麼多的資料類型。 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}
以上代碼是建立了一個繼承了SQLiteOpenHelper的類。
package com.itheima.db.dao;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.itheima.db.PersonSQLiteOpenHelper;import com.itheima.db.domain.Person;public class PersonDao { //因為要對helper進行操作,所以該類需要有一個helper的對象 private PersonSQLiteOpenHelper helper; public PersonDao(Context context){ helper = new PersonSQLiteOpenHelper(context); } //任何一個函數的構造方法都要傳入context的對象,因為context相當於全域變數為整個應用的介面 //這樣寫代碼的好處,在構造方法中已經將personhelper初始化 //養成習慣:只要一拿到資料庫就在後面寫上close的方法。 public void insert(String name , String num){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("insert into person (name,number) vlaues(?,?) ",new Object[]{name ,num}); db.close(); } //資料庫db,只有在helper調用了getWritableDatabase()或者getReadableDatabase()的方法後才會被建立出來 //使用任何一個資料庫最後都要關閉資料庫來釋資源,所以當你在開始建立資料庫的時候就直接關閉資料庫以防忘記 //資料庫db可以調用execSQl的方法來執行SQL語句,SQL語句中的?代表了預留位置可以用參數實現 public void delete(String name){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("delete from person where name =?",new Object[]{name}); db.close(); } public void update(String name ,String newname ){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("update person set name =? where name =?",new Object[]{name,newname} ); db.close(); } public boolean find(String name){ SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person where name = ?", new String[]{name}); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } //cursor使用完也要關閉來釋放資源。cursor的常用方法要掌握 public List<Person> findAll(){ SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person",null); List<Person> list = new ArrayList<Person>(); while(cursor.moveToNext()){ int id = cursor.getInt(0); String name = cursor.getString(1); String number = cursor.getString(2); Person p = new Person(name,number,id); list.add(p); } cursor.close(); db.close(); return list; }}//以上是使用SQL語句是實現的增刪改查//可以使用Android 提供的API來實現增刪改查的//使用SQL語句比較麻煩,會出現一些小細節的錯誤
編寫一個Dao類用於操作資料庫。
package com.itheima.db.domain;public class Person { private String name; private String number; private int id; public Person(String name, String number, int id) { super(); this.name = name; this.number = number; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } }//建立PersonBean類來封裝查詢到的結果,快速鍵右鍵source,產生getter 和setter的方法,右鍵source還可以產生構造方法//一般 構造方法的作用是初始化對象參數值
//自訂了一個Person類用來封裝查詢結果。
package com.itheima.db;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(this); /* //只執行以上代碼實際資料庫並沒有被建立出來,單純的new出來了一個對象 helper.getReadableDatabase(); helper.getWritableDatabase(); //只有執行以上的兩種方法中任意一種資料庫才被建立出來 */ helper.getReadableDatabase(); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
//主函數
Android之SQLite總結