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"};

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM