.sqlite是android內置的輕量級數據庫,功能強大。
先上一個xml文件,由於是練習,所以部分按鈕我就沒有修改了。
<?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">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button1"
android:text="添加" />
<Button android:text="刪除" android:id="@+id/button2"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button android:text="創建" android:id="@+id/button3"
android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
<Button android:text="查詢數據" android:id="@+id/button4"
android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
<Button android:text="刪除數據庫" android:id="@+id/button5"
android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
<TextView android:text="狀態信息" android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
需要兩個java文件
DBHelper.java
package com.fover;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
// 繼承了SQLiteOpenHelper之後的構造函數
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 當數據庫被創建時執行的代碼
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 當數據庫升級時執行的代碼
}
}
NotebookActivity.javapackage com.fover;
import java.util.Random;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class NotebookActivity extends Activity implements OnClickListener {
private Button addButton;
private Button delButton;
private Button createButton;
private DBHelper dbHelper;
private TextView infoTextView;
private Button dbQuerry;
private Button dbDel;
private Cursor cursor;
private final static String DATEBASE_NAME = "fover.db";
private final static int DATEBASE_VERSION = 1;
private final static String TABLE_NAME = "employee";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 要先把DBHelper通過new方法new出來才能對數據庫進行操作 ,否則會報錯哦
dbHelper = new DBHelper(this, DATEBASE_NAME, null, DATEBASE_VERSION);
// 取得按鈕控件設置標籤和監聽
addButton = (Button) findViewById(R.id.button1);
addButton.setOnClickListener(this);
delButton = (Button) findViewById(R.id.button2);
delButton.setOnClickListener(this);
createButton = (Button) findViewById(R.id.button3);
createButton.setOnClickListener(this);
infoTextView = (TextView) findViewById(R.id.textView1);
dbQuerry = (Button) findViewById(R.id.button4);
dbQuerry.setOnClickListener(this);
dbDel = (Button) findViewById(R.id.button5);
dbDel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 添加數據
case R.id.button1:
dbAdd();
break;
// 刪除數據
case R.id.button2:
dbDel();
break;
// 創建數據庫
case R.id.button3:
dbCreate();
break;
// 查詢數據
case R.id.button4:
dbQuerry();
break;
// 刪除數據庫
case R.id.button5:
delDB();
break;
default:
break;
}
}
// 從字面上就能夠理解 我就不做過多說明了
private void delDB() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
try {
db.execSQL(sql);
infoTextView.setText("刪除數據庫成功");
} catch (Exception e) {
infoTextView.setText("Fail" + e.toString());
}
}
private void dbQuerry() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
try {
String[] column = { "id", "name", "age" };
cursor = db.query(TABLE_NAME, column, null, null, null,
null, null);
int num = cursor.getCount();
infoTextView.setText("共有" + Integer.toString(num) + "條記錄");
} catch (Exception e) {
infoTextView.setText("讀取數據失敗" + e.toString());
}finally{
if(cursor!=null){
cursor.close();
}
}
}
private void dbCreate() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
+ " (ID INTEGER PRIMARY KEY, Name VARCHAR, Age INTEGER);";
try {
db.execSQL(sql);
infoTextView.setText("數據表成功創建");
} catch (Exception e) {
infoTextView.setText("創建失敗!");
}
}
private void dbDel() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
try {
db.delete(TABLE_NAME, "id=1", null);
infoTextView.setText("刪除記錄成功");
} catch (Exception e) {
infoTextView.setText("失敗" + e.toString());
}
}
private void dbAdd() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
try {
Random random = new Random();
for (int i = 0; i < 5; i++) {
String sql = "insert into " + TABLE_NAME
+ " (name, age) values ('name" + String.valueOf(i)
+ "', " + random.nextInt() + ")";
db.execSQL(sql);
}
infoTextView.setText("成功添加五條記錄");
} catch (Exception e) {
infoTextView.setText("記錄添加失敗" + e.toString());
}
}
}
代碼比較粗糙,請見諒!
運行截圖: