1. 程式人生 > 其它 >SQLite資料庫以及增刪改查的案例

SQLite資料庫以及增刪改查的案例

Android使用開源的與作業系統無關的SQL資料庫——SQLite

一:在命令列下建立資料庫:

1.啟動模擬器後,開啟命令列,執行adb shell

2.進入所在工程目錄

3.執行sqlite3 mydb建立資料庫檔案

:表示結尾,--表示註解

二:包Android.database.sqlite包含了使用SQLite資料庫的所有API

SQL資料庫主要概念之一就是Schema——一個關於如何組織資料庫的定義

單表定義表明和列表:

public final class FeedReaderContract{
    public FeedReaderContract(){
        /*Inner class that defines the table contents*/
              public static abstract class FeedEntry implements BaseColumns{
                  public static final String TABLE_NAME="entry";
                  public static final String COLUMN_NAME_ENTRY_ID="entryid";
                  public static final String COLUMN_NAME_TITLE="title";
                  public static final String COLUMN_NAME_SUBTITLE="subtitle";
     }
}

使用SQLiteOpenHelper建立資料庫

程式碼案例:

//從SQLiteOpenHelper派生一個類
public class FeedReaderDbHelper extents SQLiteOpenHelper{
    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="FeedReader.db";
     //建構函式產生一個庫
    public FeedReaderDbHelper(Context context){super(context,DATABASE_NAME,null,DATABASE_VERSION);}
     //重點,庫產生之後形成一個表
    public void onCreate(SQLiteDatabase db){db.execSQL(SQL_CREATE_ENTRIES);}
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        db.execSQL(SQL_DELETE_ENTRIES);onCreate(db);
        }
     public void onDowngrade(SQLiteDatabase db,int oldVersion,int newVersion){
        onUpgrade(db,oldVersion,newVersion);
    }
}
//使用以下語句建立資料庫助手類物件
FeedReaderDbHelper mDbHelper =new FeedReaderDbHelper(getContext());

插入資料:

程式碼案例:

//在私有目錄產生db,檔案和表,然後插入資料
//插入兩種方法
SQLiteDatabase db=new mDbHelper.getWritableDatabase();
//Create a new map of values,where column names are the keys
ContentValues values=new ContentValues();
values.put{FeedEntry.COLUMN_NAME_ENTRY_ID,id);
values.put{FeedEntry.COLUMN_NAME_TITLE,title);
values.put{FeedEntry.COLUMN_NAME_CONTENT,content);
//Insert the new row,returning the primary key values of the new row
long new Rowld;
newRowld=db.inset{
     FeedEntry.TABLE_NAME,
     FeedEntry.COLUMN_NAME_NULLABLE,
     values);

查詢資料:

程式碼案例:

SQLiteDatabase db=mDbHelper.getReadableDatabase();
String[] projection={FeedEntry._ID,FeedEntry.COLUMN_NAME_TITLE,FeedEntry.COLUMN_NAME_UPDATED};
String sortOrder=FeedEntry.COLUMN_NAME_UPDATED+"DESC";
Cursor c=db.query{
    FeedEntry.TABLE_NAME,//表名
    projection,//要查詢的列名
    selection,//查詢條件
    selectionArgs,//查詢條件的引數
    null,//分組條件
    null,//分組條件的引數
    sortOrder,//排序條件
    limit//分頁條件
};

在讀數值之前,必須要呼叫move方法,首先呼叫moveToFirst()方法,遊標到了第一個位置,取值就用get()方法,getString(),getLong()....但是get方法要傳入index做引數,這個引數可以通過getColumnIndex()和getColumnIndexThrow()方法獲取

//指標置首
cursor.moveToFirst();
//根據給定的列名,取值
long itemld=cursor.getLong{
     cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry_ID)
};

刪除資料:

要是想刪除一個表裡的行,就要提供篩選標準來確定要刪除的行。

資料庫API提供一個機制來建立篩選標準,來防止SQL注入攻擊。

//定義查詢的WHERE部分
String selection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+"LIKE?";
    //Specify arguments in placeholder order.
    String[] selectionArgs={String.valueOf(rowld)};
    //組裝SQL語句
    //delete()方法中

引數1:表名

引數2:WHERE語句

引數3:要查的欄位

db.delete{table_name,selection,selectionArgs);

更新資料:

SQLiteDatabase db=mDbHelper.getReadableDatabase();
//列新的值
ContentValues values=new ContentValues();
    values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,title);
//根據ID,確定需要update的列
String selection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+"LIKE?";
    String[] selelectionArgs={String.valueOf(rowld)};
//執行update
int count=db.update{
    FeedReaderDbHelper.FeedEntry.TABLE_NAME,
    values,
    selection,
    selectionArgs);

使用原生SQL語句:

通過databaseHelper.getWritableDatabase()或getReadableDatabase()獲取SQLiteDatabase物件後;

插入資料:

db.execSQL("insert into person(name,age)values(?,?)",new Object[]{"hhj",20});

查詢資料:

Cursor cursor=db.rawQuery("select * from person where name like ? and age=?",new String[]{"%ne%","20"});

刪除資料:

db.execSQL("delete from person where personid=2");

更新資料:

db.execSQL("update person set name='hhj',age=20 where personid=1"};