一、SQLiteOpenHelper(數(shù)據(jù)庫抽象幫助類)
1.抽象方法
- onCreate()
創(chuàng)建數(shù)據(jù)庫 - onUpgrade()
升級(jí)數(shù)據(jù)庫
2.實(shí)現(xiàn)方法
- getReadableDatabase()
創(chuàng)建或打開現(xiàn)有數(shù)據(jù)庫(數(shù)據(jù)庫不存在則創(chuàng)建),并返回?cái)?shù)據(jù)庫讀寫操作對(duì)象。
當(dāng)數(shù)據(jù)庫不可寫入(磁盤已滿),返回只讀卵蛉。 - getWritableDatabase()
創(chuàng)建或打開現(xiàn)有數(shù)據(jù)庫(數(shù)據(jù)庫不存在則創(chuàng)建),并返回?cái)?shù)據(jù)庫讀寫操作對(duì)象。
當(dāng)數(shù)據(jù)庫不可寫入(磁盤已滿)派昧,返回異常。
3.構(gòu)造方法
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
對(duì)應(yīng)參數(shù):(上下文, 數(shù)據(jù)庫名稱拢切,null,當(dāng)前數(shù)據(jù)庫版本號(hào))
4.實(shí)例
package com.demon.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* @author Demon
* @version V1.0
* @Description : 數(shù)據(jù)庫幫助類
* @DATE 2017/1/18
*/
public class DemonDatabaseHelp extends SQLiteOpenHelper {
/**
* 上下文
*/
private Context context;
/**
* 建表SQL
*/
public static final String CREATE_BOOK = "create table Book("
+ "id integer primary key autoincrement, "
+ "author text, "
+ "price real, "
+ "pages integer, "
+ "name text)";
/**
* 數(shù)據(jù)庫幫助類構(gòu)造方法
*
* @param context 上下文
* @param name 數(shù)據(jù)庫名稱
* @param factory 查詢時(shí)返回自定義Cursor蒂萎,一般傳入null
* @param version 版本號(hào)
*/
public DemonDatabaseHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
/**
* 創(chuàng)建數(shù)據(jù)庫
*
* @param sqLiteDatabase DB
*/
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// 創(chuàng)建 CREATE_BOOK
sqLiteDatabase.execSQL(CREATE_BOOK);
Toast.makeText(context, "建表成功", Toast.LENGTH_SHORT).show();
}
/**
* 更新數(shù)據(jù)庫
*
* @param sqLiteDatabase DB
* @param oldVersion 舊版本
* @param newVersion 新版本
*/
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// 刪除表
sqLiteDatabase.execSQL("drop table if exists Book");
// 建表
onCreate(sqLiteDatabase);
}
}
二、SQLiteDatabase
1.insert : 插入
insert(String table, String nullColumnHack, ContentValues values)
- table : 表名
- nullColumnHack : 未指定添加數(shù)據(jù)的情況下給某些可為空的列自動(dòng)賦值NULL淮椰。一般不適用此功能直接設(shè)置為null五慈。
- values : ContentValues對(duì)象
2.upData : 更新
update(String table, ContentValues values, String whereClause, String[] whereArgs)
- table : 表名
- values : ContentValues對(duì)象
- whereClause : where條件
- whereArgs : where條件參數(shù)
3.delete : 刪除
delete(String table, String whereClause, String[] whereArgs)
- table : 表名
- whereClause : where條件
- whereArgs : where條件參數(shù)
4.query : 查詢
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
query()方法參數(shù) | 對(duì)應(yīng)SQL部分 | 描述 |
---|---|---|
table | from table_name | 指定查詢的表名 |
columns | select column1,column1 | 指定查詢的列名 |
selection | where column = value | 指定where的約束條件 |
selectionArgs | - | where中的占位符提供值 |
groupBy | group by column | 指定group by的列 |
having | having column = value | 指定having |
orderBy | order by column1,column2 | 排序 |
5.實(shí)例
package com.demon.sqlite;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/**
* 插入數(shù)據(jù)按鈕
*/
private Button btnInsert;
/**
* 更新數(shù)據(jù)按鈕
*/
private Button btnUpData;
/**
* 刪除數(shù)據(jù)按鈕
*/
private Button btnDelete;
/**
* 查詢數(shù)據(jù)按鈕
*/
private Button btnQuery;
/**
* DB
*/
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = ((Application) getApplication()).getDb();
initView();
initOnListener();
}
private void initView() {
btnInsert = (Button) findViewById(R.id.btnInsert);
btnUpData = (Button) findViewById(R.id.btnUpData);
btnDelete = (Button) findViewById(R.id.btnDelete);
btnQuery = (Button) findViewById(R.id.btnQuery);
}
private void initOnListener() {
btnInsert.setOnClickListener(this);
btnUpData.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnQuery.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
// 插入數(shù)據(jù)
case R.id.btnInsert:
insert(db);
break;
// 更新數(shù)據(jù)
case R.id.btnUpData:
upData(db);
break;
// 刪除數(shù)據(jù)
case R.id.btnDelete:
delete(db);
break;
// 查詢數(shù)據(jù)
case R.id.btnQuery:
query(db);
break;
}
}
/**
* 插入數(shù)據(jù)
*/
private void insert(SQLiteDatabase db) {
ContentValues values = new ContentValues();
// SQLite方式 插入一條數(shù)據(jù)
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values);
// SQL方式 插入一條數(shù)據(jù)
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)", new String[]{"The Lost Symbol", "Dan Brown", "510", "19.95"});
}
/**
* 更新數(shù)據(jù)
*/
private void upData(SQLiteDatabase db) {
ContentValues values = new ContentValues();
// SQLite方式 更新一條數(shù)據(jù)
values.put("price", 17.9);
db.update("Book", values, "name = ?", new String[]{"The Da Vinci Code"});
// SQL方式 更新一條數(shù)據(jù)
db.execSQL("update Book set price = ? where name = ?", new String[]{"20.96", "The Lost Symbol"});
}
/**
* SQLite方式 刪除數(shù)據(jù)
*/
private void delete(SQLiteDatabase db) {
// SQLite方式 刪除一條數(shù)據(jù)
db.delete("Book", "pages > ?", new String[]{"500"});
// SQL方式 刪除一條數(shù)據(jù)
db.execSQL("delete from Book where pages < ?", new String[]{"500"});
}
/**
* 查詢數(shù)據(jù)
*/
private void query(SQLiteDatabase db) {
// SQLite方式 查詢數(shù)據(jù)
// 查詢Book表所有數(shù)據(jù)
Cursor cursor = db.query("Book", null, null, null, null, null, null, null);
// cursor.moveToFirst() :指針第一個(gè)位置
if (cursor.moveToFirst()) {
do {
// cursor.getColumnIndex :指定列的位置索引
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("YX", name);
Log.d("YX", author);
Log.d("YX", pages + "");
Log.d("YX", price + "");
}
// cursor.moveToNext() :移動(dòng)指針到下一位置
while (cursor.moveToNext());
}
// 關(guān)閉
cursor.close();
// SQL方式 查詢數(shù)據(jù)
Cursor sqlCursor = db.rawQuery("select * from Book", null);
// cursor.moveToFirst() :指針第一個(gè)位置
if (sqlCursor.moveToFirst()) {
do {
// cursor.getColumnIndex :指定列的位置索引
String name = sqlCursor.getString(sqlCursor.getColumnIndex("name"));
String author = sqlCursor.getString(sqlCursor.getColumnIndex("author"));
int pages = sqlCursor.getInt(sqlCursor.getColumnIndex("pages"));
double price = sqlCursor.getDouble(sqlCursor.getColumnIndex("price"));
Log.d("YXSQL", name);
Log.d("YXSQL", author);
Log.d("YXSQL", pages + "");
Log.d("YXSQL", price + "");
}
// cursor.moveToNext() :移動(dòng)指針到下一位置
while (sqlCursor.moveToNext());
}
// 關(guān)閉
sqlCursor.close();
}
}
2017/1/18 15:56:12