android內部提供了SQLiteOpenHelper工具類,封裝了操作內置數據庫SqLite的基本方法,完成實現對數據庫的增刪查改操作;
首先創建項目,實現幾個操作按鈕;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sqlitedemo.MainActivity" >
<Button
android:id="@+id/createDatabase"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="創建數據庫"
/>
<Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="插入數據庫"
/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="更新數據"
/>
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查詢數據"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刪除數據"
/>
</LinearLayout>
package com.example.sqlitedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBhelper extends SQLiteOpenHelper{
public static final String ID="_id";
public static final String NAME="name";
public static final String AGE="age";
public static final String SEX="sex";
public static final String TABLE_NAME="person";
public MyDBhelper(Context context) {
super(context,"persons", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_NAME+"("+
ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
+NAME+" TEXT NOT NULL,"
+AGE+" INTEGER,"
+SEX+" TEXT"+")");
System.out.println("database created!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("database update!");
}
}
MyDBhelper繼承了SQLiteOpenHelper,並且重寫了父類構造方法和兩個回調方法;當實例化一個MyDBhelper對象,並通過該對象獲取一個可寫SQLiteDatabase實例的時候,會執行回調方法onCreate();當構造函數中的version改變的時候會執行onUpgrade()方法;
回到mainActivity中實現具體功能:
package com.example.sqlitedemo;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button createDatabase;
private Button insert;
private Button query;
private Button update;
private Button delete;
private MyDBhelper dbHelper;
SQLiteDatabase db;
Cursor cursor;
public static int counter=0;
public String name,sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
createDatabase =(Button) findViewById(R.id.createDatabase);
insert=(Button) findViewById(R.id.insert);
query=(Button) findViewById(R.id.query);
update=(Button) findViewById(R.id.update);
delete=(Button) findViewById(R.id.delete);
createDatabase.setOnClickListener(this);
insert.setOnClickListener(this);
query.setOnClickListener(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
}
//創建數據庫
private void createDB(){
dbHelper =new MyDBhelper(this);
db=dbHelper.getWritableDatabase();
Toast.makeText(this, "database created!", Toast.LENGTH_SHORT);
}
//插入數據庫;
private void insertDB(){
counter++;
name="用戶"+counter;
if(counter%2==0){
sex="male";
}else{
sex="female";
}
dbHelper =new MyDBhelper(this);
db=dbHelper.getWritableDatabase();
ContentValues cv =new ContentValues();
cv.put("name",name);
cv.put("age",26);
cv.put("sex",sex);
db.insert(MyDBhelper.TABLE_NAME, null, cv);
Toast.makeText(this, name+"添加成功", Toast.LENGTH_SHORT).show();
db.close();
}
//查詢數據庫
private void queryDB(){
dbHelper =new MyDBhelper(this);
SQLiteDatabase rdb=dbHelper.getReadableDatabase();
cursor=rdb.query(MyDBhelper.TABLE_NAME, null, null, null, null, null, null);
StringBuffer sb=new StringBuffer();
while(cursor.moveToNext()){
String rs=cursor.getInt(cursor.getColumnIndex(MyDBhelper.ID))+","+cursor.getString(cursor.getColumnIndex(MyDBhelper.NAME))+","+cursor.getInt(cursor.getColumnIndex(MyDBhelper.AGE))+","+cursor.getString(cursor.getColumnIndex(MyDBhelper.SEX))+"\n";
sb.append(rs);
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
db.close();
}
//刪除數據
private void deleteDB(){
dbHelper =new MyDBhelper(this);
db=dbHelper.getWritableDatabase();
String whereClause="_id>?";
String[] whereArgs={String.valueOf(1)};
db.delete(MyDBhelper.TABLE_NAME, whereClause, whereArgs);
db.close();
Toast.makeText(this, "刪除成功!", Toast.LENGTH_SHORT).show();
}
//更新數據
private void updateDB(){
dbHelper =new MyDBhelper(this);
db=dbHelper.getWritableDatabase();
ContentValues values =new ContentValues();
values.put("age", 21);
String whereClause="name=?";
String[] whereArgs={String.valueOf("小強")};
db.update(MyDBhelper.TABLE_NAME, values, whereClause, whereArgs);
db.close();
Toast.makeText(this, "數據更新成功!", Toast.LENGTH_SHORT).show();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.createDatabase:
createDB();
break;
case R.id.insert:
insertDB();
break;
case R.id.query:
queryDB();
break;
case R.id.update:
updateDB();
break;
case R.id.delete:
deleteDB();
break;
}
}
}
效果: