在android中使用SQLite資料庫

來源:互聯網
上載者:User

在android中使用SQLite資料庫

SQLite資料庫以其輕量、體積小等特點,使其在開發中運用的非常廣泛,在前面的部落格中我也介紹過在Cocos2d-x中使用SQLite資料庫,這篇部落格是介紹在Android中使用SQLite資料庫,Android中直接整合了SQLite資料庫,使用起來非常方便,不需要向Cocos2d-x中那樣添加外部檔案

我將使用SQLite資料庫實現一個所示的效果,開啟app後會彈出所示的介面

 

單擊createDatabase按鈕後,在logcat中可以看到列印了一條提示資訊,表示資料庫建立成功,當再次點擊createDAtabase按鈕後就不會再列印提示資訊,因為資料庫建立成功後不會再建立資料庫

 

資料庫建立成功後可以在app所在的包下找到一個database檔案夾,databases檔案夾下可以找到建立好的資料庫檔案

將people.db匯出後,在使用SQLite Expert可以查看建立好的資料庫中的內容

單擊Insert按鈕後可以看到,向資料庫中插入了10個學生的資訊

 

按下Delete按鈕後會刪除"劉得意"資訊,可以看到資料庫中已經沒有劉得意的資訊了

單擊Update按鈕後,將所有學生的學號都減少了1

點擊Select按鈕後,尋找資料庫中所有學生的姓名和與姓名對應的C++成績,並在logcat中輸出

實現方式:
1、使用Android Studio建立一個Android工程,修改activity_main.xml檔案

 




2、建立一個MyOpenHelper.java檔案,在MyOpenHelper.java中添加下面的代碼

 

 

package com.fyt.databasedemo;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;//建立一個抽象類別SQLiteOpenHelper的實作類別MyOpenHelperpublic class MyOpenHelper extends SQLiteOpenHelper {    /**     * MyOpenHelper構造方法     * @param context 上下文     * @param name 資料庫檔案的名字     * @param factory 遊標工廠(結果集)     * @param version 資料庫的版本號碼(用於升級)     */    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        super(context, name, factory, version);    }    //建立資料庫時,調用此方法    @Override    public void onCreate(SQLiteDatabase db) {        Log.d("MainActivity", "資料庫建立成功");        //建立一個學生表        db.execSQL("create table student(_id integer primary key autoincrement, name char(10), age integer, no integer, cpp float, math float, english float)");    }    //資料庫升級時調用此方法    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        Log.d("MainActivity", "資料庫升級成功");    }}

3、修改MainActivity.java中的代碼

 

 

package com.fyt.databasedemo;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends Activity {    //用於建立協助器對象    private MyOpenHelper oh;    //用於建立資料庫物件    private SQLiteDatabase db;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    //建立資料庫    public void createDatabase(View view) {        //建立協助器對象        oh = new MyOpenHelper(this, "people.db", null, 1);        //建立資料庫物件        db = oh.getWritableDatabase();    }    //向資料庫中添加資料    public void Insert(View view) {        //向學生表中添加10名學生        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"劉得意", 19, 1001, 60, 98, 75});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王銳", 20, 1002, 63, 90, 96});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"何煜中", 19, 1003, 90, 73, 82});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"王磊", 21, 1004, 87, 86, 92});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"馮松", 19, 1005, 89, 98, 83});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"裴培", 20, 1006, 75, 82, 91});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"馬驍", 19, 1007, 62, 67, 90});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"馬婧", 20, 1008, 98, 84, 87});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"周俊升", 19, 1009, 57, 68, 96});        db.execSQL("insert into student(name, age, no, cpp, math, english) values(?, ?, ?, ?, ?, ?)", new Object[]{"賀祺", 21, 1010, 61, 96, 72});    }    //刪除資料庫中的資料    public void Delete(View view) {        //刪除姓名為"劉得意"的學生的資訊        db.execSQL("delete from Student where name = ?", new Object[]{"劉得意"});    }    //修改資料庫中的資料    public void Update(View view) {        //將資料庫中所有人的學號減少1        db.execSQL("update student set no = no -1");    }    //查詢資料庫中的資料    public void Select(View view) {        //查詢資料庫中學生的姓名和以其對應的C++成績,傳回值為一個結果集        Cursor cursor = db.rawQuery("select name, cpp from student", null);        while (cursor.moveToNext()) {            //cursor.getColumnIndex("name")獲得姓名所在的列            String name = cursor.getString(cursor.getColumnIndex("name"));            float cpp = cursor.getFloat(cursor.getColumnIndex("cpp"));            //輸出學生的姓名和與姓名對應的C++成績            Log.d("MainActivity", '[' + name + ", " + cpp + ']');        }    }}

 

 

聯繫我們

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