Android 系統API實現資料庫的增刪改查和SQLite3工具的使用,androidsqlite3

來源:互聯網
上載者:User

Android 系統API實現資料庫的增刪改查和SQLite3工具的使用,androidsqlite3

在《Android SQL語句實現資料庫的增刪改查》中介紹了使用sql語句來實現資料庫的增刪改查操作,本文介紹Android 系統API實現資料庫的增刪改查和SQLite3工具的使用。

系統API實現資料庫的增刪改查

相關代碼修改如下:

package com.wuyudong.db.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.wuyudong.db.PersonSQLiteOpenHelper;import com.wuyudong.db.domain.Person;public class PersonDao {    private PersonSQLiteOpenHelper helper;    public PersonDao(Context context) {        helper = new PersonSQLiteOpenHelper(context);    }    /**     * 添加一條記錄到資料庫     *      * @param name     *            姓名     * @param number     *            電話     */    public void add(String name, String number) {        SQLiteDatabase db = helper.getWritableDatabase();        // db.execSQL("insert into person (name,number) values (?,?)",        // new Object[] { name, number });        ContentValues values = new ContentValues();        values.put("name", name);        values.put("number", number);        db.insert("person", null, values);        db.close();    }    /**     * 查詢記錄是否存在     *      * @param name     *            姓名 return true存在, false不存在     */    public boolean find(String name) {        SQLiteDatabase db = helper.getReadableDatabase();        // Cursor cursor = db.rawQuery("select * from person where name=?",        // new String[] { name });        Cursor cursor = db.query("person", null, "name=?",                new String[] { name }, null, null, null);        boolean result = cursor.moveToNext();        cursor.close();        db.close();        return result;    }    /**     *      * @param name     *            要修改的人的姓名     * @param newnumber     *            新的號碼     */    public void update(String name, String newnumber) {        SQLiteDatabase db = helper.getReadableDatabase();        // db.execSQL("update person set number=? where name=?", new Object[] {        // newnumber, name });        ContentValues values = new ContentValues();        values.put("number", newnumber);        db.update("person", values, "name=?", new String[] { newnumber });        db.close();    }    /**     * 刪除一條記錄     *      * @param name     */    public void delete(String name) {        SQLiteDatabase db = helper.getReadableDatabase();        // db.execSQL("delete from person where name=?", new Object[] { name });        db.delete("person", "name=?", new String[] { name });        db.close();    }    /**     * 返回全部的資料庫資訊     *      * @return     */    public List<Person> findAll() {        List<Person> persons = new ArrayList<Person>();        SQLiteDatabase db = helper.getReadableDatabase();        // Cursor cursor = db.rawQuery("select * from person", null);        Cursor cursor = db.query("person", new String[] { "name", "id",                "number" }, null, null, null, null, null);        while (cursor.moveToNext()) {            int id = cursor.getInt(cursor.getColumnIndex("id"));            String name = cursor.getString(cursor.getColumnIndex("name"));            String number = cursor.getString(cursor.getColumnIndex("number"));            Person person = new Person(id, name, number);            persons.add(person);        }        cursor.close();        db.close();        return persons;    }}

兩種資料庫增刪改查方式的優缺點
1. 直接利用sql語句增刪改查
優點:非常的靈活,多表查詢,級聯查詢。
缺點:代碼容易出錯,方法沒有傳回值。

2. 利用google封裝的api 執行增刪改查
優點: 代碼不容易出錯,寫起來簡單,方法有傳回值
缺點: 不容易多表查詢。複雜表的操作,視圖都無法實現。

運行代碼後查看結果十分麻煩,下面介紹一種簡單的方法

SQLite3工具的使用

cmd開啟命令列

輸入“adb shell” 出現如下錯誤

以為是模擬器沒開的原因,但是開啟模擬器後,還是一樣的錯誤出現

進入相應目錄

繼續進入databases 目錄

輸入如下命令:

sqlite3 person.db

執行一條sql語句如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.