Android在API推薦的方式來實現SQLite資料庫的增長、刪除、變化、檢查操作

來源:互聯網
上載者:User

標籤:

package com.examp.use_SQLite.dao;import java.util.ArrayList;import java.util.List;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.examp.use_SQLite.PersonSQLiteOpenHelper;import com.examp.use_SQLite.domain.Person;public class PersonDaoForAndroid {// 擷取要操作的資料庫物件private PersonSQLiteOpenHelper helper;/** * 在構造方法中完畢helper的初始化 *  * @param context */public PersonDaoForAndroid(Context context) {helper = new PersonSQLiteOpenHelper(context);}/** * 加入資料 *  * @param name姓名 * @param number電話 * @return 返回新插入一行的行ID,或者-1說明發生一個錯誤 */public long add(String name, String number) {// 擷取資料庫的連線物件SQLiteDatabase db = helper.getWritableDatabase();// 以下是API推薦的寫法ContentValues values = new ContentValues();values.put("name", name);values.put("number", number);// 目標資料表// 返回新插入一行的行ID,或者-1說明發生一個錯誤long id = db.insert("person", null, values);// 及時的關閉資料庫連接db.close();return id;}/** * 依據使用者姓名查詢資料 *  * @param name *            查詢條件 * @return 查詢結果 */public Person findByName(String name) {// 擷取資料庫連接SQLiteDatabase db = helper.getReadableDatabase();// 目標資料表名|指定返回的資料欄位,假設設定null表示返回全部的資料欄位|條件陳述式,不包含wherekeyword|條件的參數列表|分組的語句|having查詢的語句|排序的語句,假設不設定表示使用預設Cursor cursor = db.query("person", null, "name=?",new String[] { name }, null, null, null);// 聲明結果對象Person person = null;// 推斷是否有結果返回if (cursor.moveToNext()) {// 取出資料集中的單行資料的個欄位的資料// cursor.getColumnIndex("id");擷取指定的欄位的下標// cursor.getInt(index);或getString(index);是擷取指定的下標的資料index為int類型;int id = cursor.getInt(cursor.getColumnIndex("id"));String named = cursor.getString(cursor.getColumnIndex("name"));String number = cursor.getString(cursor.getColumnIndex("number"));// 執行個體化查詢結果資料對象person = new Person(id, named, number);}// 關閉資料庫連接,釋放資源db.close();// 返回資料結果對象return person;}/** * 依據name更新一條資料 *  * @param name *            跟新條件 * @param newnumber *            新的資料 * @return rows受影響的行數 */public int update(String name, String newnumber) {SQLiteDatabase db = helper.getWritableDatabase();ContentValues values = new ContentValues();values.put("number", newnumber);// 目標資料表|要更新的資料|更新條件陳述式|更新條件參數列表int rows = db.update("person", values, "name=?", new String[] { name });db.close();return rows;}/** * 依據指定的名字刪除資料 *  * @param name *            刪除條件 * @return rows受影響的行數 */public int delete(String name) {SQLiteDatabase db = helper.getWritableDatabase();// 目標資料庫表名稱|條件陳述式|條件參數int rows = db.delete("person", "name=?

", new String[] { name });db.close();return rows;}/** * 查詢全部的操作 * * @return 查詢的結果集 */public List<Person> findAll() {SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("person", null, null, null, null, null, null);// 聲明結果集// 初始化結果集List<Person> persons = new ArrayList<Person>();// 推斷是否有結果返回while (cursor.moveToNext()) {// 聲明結果對象Person person = null;// 取出資料集中的單行資料的個欄位的資料// cursor.getColumnIndex("id");擷取指定的欄位的下標// cursor.getInt(index);或getString(index);是擷取指定的下標的資料index為int類型;int id = cursor.getInt(cursor.getColumnIndex("id"));String named = cursor.getString(cursor.getColumnIndex("name"));String number = cursor.getString(cursor.getColumnIndex("number"));// 執行個體化查詢結果資料對象person = new Person(id, named, number);// 向結果集中加入資料persons.add(person);}// 關閉串連,釋放資源db.close();// 放回結果集return persons;}}


看到代碼的其餘部分,以及一個部落格Android實現SQLite資料庫的增長、刪除、變化、檢查操作

Android在API推薦的方式來實現SQLite資料庫的增長、刪除、變化、檢查操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.