Android 程式開發:(十九)資料庫 —— 19.2 使用資料庫

來源:互聯網
上載者:User

DBAdapter已經建立好了,現在我們可以去使用資料庫了。下面的章節,將介紹常規的CRUD(增加,讀取,更新,刪除)。

往表中新增連絡人...。

1. 使用之前的項目,在DatabasesActivity.java中添加一些代碼。

public class DatabasesActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        DBAdapter db = new DBAdapter(this);        //---add a contact---        db.open();        long id = db.insertContact("Manoel", "manoel@hotmail.com");        id = db.insertContact("Mary", "mary@hotmail.com");        db.close();    }    }

2. 按F11在模擬器上面調試。

首先,建立一個DBAdapter類的執行個體:

DBAdapter db = new DBAdapter(this);

insertContact()方法返回被插入的行的id。如果這個過程發生錯誤,返回-1。

使用DDMS查看Android裝置或模擬器,在databases檔案夾下面將會看見名為MyDB的資料庫。

擷取所有的連絡人。

使用getAllContacts() 方法,可以擷取所有的連絡人資訊。
1. 使用之前的例子,並添加一些代碼。

public class DatabasesActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        DBAdapter db = new DBAdapter(this);       /* //---add a contact---        db.open();        long id = db.insertContact("Manoel", "manoel@hotmail.com");        id = db.insertContact("Mary", "Mary@hotmail.com");        db.close();        */        //--get all contacts---        db.open();        Cursor c = db.getAllContacts();        if (c.moveToFirst())        {            do {                DisplayContact(c);            } while (c.moveToNext());        }        db.close();            public void DisplayContact(Cursor c)    {        Toast.makeText(this,                "id: " + c.getString(0) + "\n" +                "Name: " + c.getString(1) + "\n" +                "Email:  " + c.getString(2),                Toast.LENGTH_LONG).show();    }}

2. 按F11在模擬器上面測試。

擷取單獨一條連絡人資訊。

        db.open();        Cursor c = db.getContact(2);        if (c.moveToFirst())                    DisplayContact(c);        else            Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();        db.close();     

更新連絡人資訊。

        db.open();        if (db.updateContact(1, "Manoel, "Manoel@gmail.com"))            Toast.makeText(this, "Update successful.", Toast.LENGTH_LONG).show();        else            Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();        db.close();

刪除一條連絡人資訊。

        db.open();        if (db.deleteContact(1))            Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();        else            Toast.makeText(this, "Delete failed.", Toast.LENGTH_LONG).show();        db.close();

這些操作都十分的類似,我就不一一贅述了。

更新資料庫。

有些時候,可能需要往資料庫中添加一個新的表,或者是往表裡添加一些新的列。在這種情況下,就需要把資料從舊資料庫遷移到新資料庫。

如果想要更新資料庫,那麼改變DATADASE_VERSION常量即可。

DATABASE_VERSION之前為1,現改為2。

public class DBAdapter {    static final String KEY_ROWID = "_id";    static final String KEY_NAME = "name";    static final String KEY_EMAIL = "email";    static final String TAG = "DBAdapter";    static final String DATABASE_NAME = "MyDB";    static final String DATABASE_TABLE = "contacts";    static final int DATABASE_VERSION = 2;

重新跑一遍程式,就會看到如下的log資訊。

這個例子裡面,只是簡單地刪除資料庫,然後重新建立了一個新的空白資料庫。在真實的項目中,可能會儲存之前的資料,然後把這些資料重新放入新的資料庫裡面。

聯繫我們

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