Android自學筆記-10-Sqlite的簡單使用

來源:互聯網
上載者:User

在android中已經內建了Sqlite資料庫,如果我們需要使用Sqlite資料庫,Google已經給我們提供了比較方便的方法,我們只需要自己編寫一個java類,然後繼承SQLiteOpenHelper,實現其中的方法就可以使用了。下面是一段代碼:

package com.mxy;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {public PersonSQLiteOpenHelper(Context context) {//參數分別為上下文、資料庫檔案、CursorFactory、資料庫版本//CursorFactory如果為null 則使用預設的CursorFactorysuper(context, "person.db", null, 1);}/** * 資料庫第一次被建立的時候調用 可以初始化表和參數等 */@Overridepublic void onCreate(SQLiteDatabase db) {//建立表 底層以string格式儲存  所以varchar的字元不會起作用db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))");}/** * 資料庫版本發生變化的時候調用 可以用於修改資料表結構 資料庫參數等 */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}
然後我們可以建立該類的執行個體去操作資料庫,但是我們在調用getReadableDatabase()或getWritableDatabase()之前,並不會真正的建立資料庫檔案。在真正操作資料庫的時候,我們可以直接使用sql語句,也可以使用Google給我們封裝好的一些方法,這裡建議使用Google提供的方法,可以提高我們的正確率。下面是一個代碼:
package com.mxy;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                //直接使用sql語句操作資料庫//        sqlString();        //使用android封裝的api        PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(this);        //調用下面兩者之一 才會真正的建立資料庫檔案//        helper.getReadableDatabase();        SQLiteDatabase db = helper.getWritableDatabase();                ContentValues values = new ContentValues();        values.put("name", "lisi");        values.put("number", "222222");                long rowid = db.insert("person", null, values);                if(rowid != -1){        Log.i("mxy", "添加成功");        }else{        Log.i("mxy", "添加失敗");        }                        Cursor cursor = db.query("person", null, "name=?", new String[]{"zhangsan"}, null, null, null);                while(cursor.moveToNext()){        int id = cursor.getInt(cursor.getColumnIndex("id"));        String number = cursor.getString(2);        Log.i("mxy", "id:" + id + "  number:" + number);        }                cursor.close();                db.close();    }private void sqlString() {PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(this);        //調用下面兩者之一 才會真正的建立資料庫檔案//        helper.getReadableDatabase();        SQLiteDatabase db = helper.getWritableDatabase();                db.execSQL("insert into person (name,number) values (?,?)", new Object[]{"zhangsan","12222"});                Cursor cursor = db.rawQuery("select * from person where name=?", new String[]{"zhangsan"});                while(cursor.moveToNext()){        int id = cursor.getInt(cursor.getColumnIndex("id"));        String number = cursor.getString(2);        Log.i("mxy", "id:" + id + "  number:" + number);        }                cursor.close();                db.close();}    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }
轉載請註明出處:http://blog.csdn.net/mengxiangyu

聯繫我們

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