Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android sqlite database

android sqlite database

編輯:關於Android編程

好吧,現在來看一下在android裡面使用sqlite數據庫的一些基本操作吧。

對於大多數app而言對數據庫的要求很簡單,無非CURD,僅此而已。而我個人比較喜歡將所要使用的相應數據    表封裝成javabean再進行操作,這樣會顯得邏輯比較清晰。

首先,我們寫一個Entity:

package wenyue.justdoit.entity;

public class todoData {
private String todotadkname;
private int id;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public todoData() {

}

public String getTodotadkname() {
    return todotadkname;
}

public void setTodotadkname(String todotadkname) {
    this.todotadkname = todotadkname;
}

}

在這之後呢,我們需要一個繼承了SQLiteOpenHelper的工具類。在這之後,我們只需要在進行對entity進行數據庫操作的時候對其進行實例化即可。

SQLiteOpenHelper需要重寫onCreate,onUpgrade方法。android已經對sqlite數據庫的操作進行了封裝,所以若是你想要更深入了解數據庫操作,可以參考JDBC的使用。OnCreate再首次創建數據庫時調用。而我們要進行數據庫操作時可用SQLiteDatabase db = this.getWritableDatabase()。文檔說該方法使db獲得了寫數據庫的權限,感覺其實就是拿到了相應數據庫的廉潔實體。

那麼下面就直接貼出繼承了SQLiteOpenHelper的工具類代碼吧,畢竟都是些很簡單的東西,看方法名就能知道是干什麼的了。

package wenyue.justdoit.util;

import java.util.ArrayList;
import java.util.List;

import wenyue.justdoit.entity.todoData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class dbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "todolistManager";

// tasks table name
private static final String TABLE_TASKS = "todolist";

// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TASKNAME = "todotadkname";

public dbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_TASKS + " ( "
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_TASKNAME + " TEXT" + ")";
    db.execSQL(sql);
    // db.close();

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
    // Create tables again
    onCreate(db);

}

public void save(todoData testdata) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TASKNAME, testdata.getTodotadkname());
    db.insert(TABLE_TASKS, null, values);
    db.close();
}

public List getAllTasks() {
    List taskList = new ArrayList();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_TASKS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            todoData task = new todoData();
            task.setId(cursor.getInt(0));
            task.setTodotadkname(cursor.getString(1));
            // Adding contact to list
            taskList.add(task);
        } while (cursor.moveToNext());
    }
    db.close();
    return taskList;
}

public int checkIsExits(String s) {
    List taskList = new ArrayList();
    String selectQuery = "SELECT * FROM " + TABLE_TASKS + " WHERE "
            + KEY_TASKNAME + " = ?";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, new String[] { s });
    if (cursor.moveToFirst()) {
        do {
            todoData task = new todoData();
            task.setId(cursor.getInt(0));
            task.setTodotadkname(cursor.getString(1));
            // Adding contact to list
            taskList.add(task);
        } while (cursor.moveToNext());
    }
    Log.i("checkIsExits", String.valueOf(taskList.size()));
    db.close();
    return taskList.size();

}

public int getTaskID(String tsakName) {

    List taskList = new ArrayList();
    String selectQuery = "SELECT * FROM " + TABLE_TASKS + " WHERE "
            + KEY_TASKNAME + " = ?";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, new String[] { tsakName });
    if (cursor.moveToFirst()) {
        do {
            todoData task = new todoData();
            task.setId(cursor.getInt(0));
            task.setTodotadkname(cursor.getString(1));
            // Adding contact to list
            taskList.add(task);
        } while (cursor.moveToNext());
    }
    Log.i("GET_TASK_ID", String.valueOf(taskList.get(0).getId()));
    db.close();
    return taskList.get(0).getId();

}

public void updateTask(String taskName, int id) {
    Log.i("updateTask", String.valueOf(id) + "-------------" + taskName);
    // updating row
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_TASKNAME, taskName);
    String[] args = { String.valueOf(id) };

    db.update(TABLE_TASKS, values, KEY_ID + " = ?", args);
    Log.i("UPDATESUCCESS", String.valueOf(id));
    db.close();
}

public void deleteTask(String taskname) {
    SQLiteDatabase db = this.getWritableDatabase();
    String[] args = { String.valueOf(taskname) };
    db.delete(TABLE_TASKS, KEY_TASKNAME + "=?", args);
    db.close();
}

}

至於具體的數據處理就不用多說了吧,大家拿到實例後,調用相應的方法即可。好吧,先寫這麼多了。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved