安卓開發SQLite增刪改查操作例項
本文講如何使用Android中內建的SQLite輕量資料庫,Android SDK中已經對其進行了封裝,使用起來相當簡單。建立類繼承SQLiteOpenHelper就可以將資料庫的建立和應用版本更新後資料庫的重建納入自動管理中。本文實現一個簡單的Sqlite資料庫,儲存人名和電話號碼。
主Activity 類SqliteSample.java 程式碼:
public class SqliteSample extends Activity {
private DBHelper mDBHelper = null;
private Button btCreateDb = null;
private Button btInsertData = null;
private Button btViewData = null;
private Button btDelOne = null;
private Button btClearAll = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btCreateDb = (Button)findViewById(R.id.Button01);
btCreateDb.setOnClickListener(new ClickViewHandler());
btInsertData = (Button)findViewById(R.id.Button02);
btInsertData.setOnClickListener(new ClickViewHandler());
btInsertData.setEnabled(false);
btViewData = (Button)findViewById(R.id.Button03);
btViewData.setOnClickListener(new ClickViewHandler());
btViewData.setEnabled(false);
btDelOne = (Button)findViewById(R.id.Button04);
btDelOne.setOnClickListener(new ClickViewHandler());
btDelOne.setEnabled(false);
btClearAll = (Button)findViewById(R.id.Button05);
btClearAll.setOnClickListener(new ClickViewHandler());
btClearAll.setEnabled(false);
}
public class ClickViewHandler implements OnClickListener {
@Override
public void onClick(View v) {
if (v == btCreateDb) {
createDB();
} else if (v == btInsertData) {
insertSomeRecords();
} else if (v == btViewData) {
ViewRecords();
} else if (v == btDelOne) {
DelOne();
} else if (v == btClearAll) {
mDBHelper.delAllPeople();
}
}
}
private void createDB() {
String DB_NAME = "sqlitedb1";
mDBHelper = new DBHelper(
SqliteSample.this, DB_NAME, null, 1);
assert(mDBHelper != null);
btCreateDb.setEnabled(false);
btInsertData.setEnabled(true);
btViewData.setEnabled(true);
btDelOne.setEnabled(true);
btClearAll.setEnabled(true);
}
private void insertSomeRecords() {
mDBHelper.addPeople("張三", "18600000000");
mDBHelper.addPeople("李四", "13200000000");
mDBHelper.addPeople("王五", "13200000000");
}
private void ViewRecords() {
// Make the query
Cursor c = mDBHelper.getWritableDatabase().query(
DBHelper.TB_NAME,null,null,null,null,null,
DBHelper.NAME + " ASC");
StringBuilder sbRecords = new StringBuilder("");
if (c.moveToFirst()) {
int idxID = c.getColumnIndex(DBHelper.ID);
int idxName = c.getColumnIndex(DBHelper.NAME);
int idxNumber = c.getColumnIndex(DBHelper.NUMBER1);
// Iterator the records
do {
sbRecords.append(c.getInt(idxID));
sbRecords.append(".");
sbRecords.append(c.getString(idxName));
sbRecords.append(",");
sbRecords.append(c.getString(idxNumber));
sbRecords.append("/n");
} while (c.moveToNext());
}
c.close();
// Refresh the content of TextView
((TextView)(findViewById(
R.id.TextView01))).setText(sbRecords);
}
private void DelOne() {
int id;
Cursor c = mDBHelper.getWritableDatabase().query(
DBHelper.TB_NAME,null,null,null,null,null,
DBHelper.NAME + " ASC");
if (c.moveToFirst()) {
int idxID = c.getColumnIndex(DBHelper.ID);
id = c.getInt(idxID);
mDBHelper.delPeople(id);
}
}
}
ui介面 main.xml 程式碼,主要是幾個button的放置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="建立資料庫" android:id="@+id/Button01"
android:height="30px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="插入幾條記錄" android:id="@+id/Button02"
android:height="30px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="刪除一條" android:id="@+id/Button04"
android:height="30px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="清除所有" android:id="@+id/Button05"
android:height="30px" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<Button android:text="檢視記錄" android:id="@+id/Button03"
android:height="30px" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"/>
<TextView android:text="..." android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
資料庫操作類 DBHelper.java 程式碼:
package jtapp.sqlitesamples;
public class DBHelper extends SQLiteOpenHelper {
public static final String TB_NAME = "people";
public static final String ID = "_id";
public static final String NAME = "name";
public static final String NUMBER1 = "number1";
public DBHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
this.getWritableDatabase();
}
/**
* should be invoke when you never use DBhelper
* To release the database and etc.
*/
public void Close() {
this.getWritableDatabase().close();
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " ("
+ ID + " INTEGER PRIMARY KEY,"
+ NAME + " VARCHAR,"
+ NUMBER1 + " VARCHAR)");
}
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
onCreate(db);
}
public void addPeople(String name, String number) {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, name);
values.put(DBHelper.NUMBER1, number);
this.getWritableDatabase().insert(
DBHelper.TB_NAME, DBHelper.ID, values);
}
public void delPeople(int id) {
this.getWritableDatabase().delete(
DBHelper.TB_NAME, this.ID + " = " + id, null);
}
public void delAllPeople() {
this.getWritableDatabase().delete(
DBHelper.TB_NAME, null, null);
}
}
本文主要以例子的方式展示了安卓開發中SQLite的增啥改查的使用,本文提供一個SQLite增刪改查的DBHelper類,主要用來操作SQLite資料庫,讀者可以自己複製該類的程式碼使用,感謝閱讀本文
相關文章
- sqlite 個人寫增刪改查例項SQLite
- 使用mybatis開發的增刪改查操作MyBatis
- MongoDB增刪改查操作MongoDB
- Numpy array資料的增、刪、改、查例項
- PHP操作MongoDB(增刪改查)PHPMongoDB
- python 連線mongodb實現增刪改查例項PythonMongoDB
- mysql資料增刪改查操作MySql
- MySQL基礎操作(增刪改查)MySql
- JS字串操作之增刪改查JS字串
- 資料庫操作增刪改查模糊查資料庫
- jquery基本操作增刪改查有哪些?jQuery
- JavaAPI操作MongoDB--基本增刪改查JavaAPIMongoDB
- MyBatis框架搭建及增刪改查操作MyBatis框架
- 增刪改查
- jQuery實現的對元素的增刪改查程式碼例項jQuery
- 用thinkphp進行增刪改查的操作PHP
- iOS操作屬性列表plist(增刪改查)iOS
- indexedDB 增刪改查Index
- SQL增刪改查SQL
- mysql增刪改查MySql
- Mongoose查增改刪Go
- FMDB增刪改查
- mysql增查刪改MySql
- 使用Django開發簡單介面:文章增刪改查Django
- Java實現簡單的增刪改查操作Java
- YII1 增、刪、改、查資料庫操作資料庫
- imutable.js常用增刪改查操作說明JS
- 表的建立修改及增刪改查-DML操作
- SQLAlchemy - 模組檔案以及增刪改查(CURD操作)SQL
- Python操作SQLServer資料庫增刪改查PythonSQLServer資料庫
- 基於gin的golang web開發:mysql增刪改查GolangWebMySql
- layui的增刪改查UI
- sql指令,增,刪,查,改SQL
- EFCore之增刪改查
- 列表的增刪改查
- 字典的增刪改查
- redist的增刪改查Redis
- Mybatis的增刪改查MyBatis