android-sqlite資料庫<五>

來源:互聯網
上載者:User

標籤:

1.sqlite

   它是android用來管理資料的資料庫來的,輕巧且高效。

   在android中查看和管理sqlite資料庫

   http://www.cnblogs.com/walkingp/archive/2011/03/28/1997437.html

2.SQLiteOpenHelper類(抽象類別)

   它用來建立,開啟資料庫。即管理sqlite資料庫,注意:它是抽象類別,需要繼承實現

   構造方法:factory一般為null,它可以建立/開啟/管理資料庫

      SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

    成員方法:

         SQLiteDatabase  getReadableDatabase() ;//Create and/or open a database且只可以進行讀操作

         SQLiteDatabase  getWritableDatabase();//Create and/or open a database 且可以進行讀或寫操作.

         onCreate(SQLiteDatabase db) ;//Called when the database is created for the first time.

         onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);//Called when the database needs to be upgraded.

3.SQLiteDatabase類

   資料庫類,用來管理資料庫,可以使用sql語句。

   它的建立需要使用,SQLiteOpenHelper的getReadableDatabase()方法或getWritableDatabase()方法,只要這兩個方法才會建立。

   成員方法:

              execSQL(String sql) ;//eg: db.execSQL("create table user(id int,name varchar(20))")

              insert(String table, String nullColumnHack, ContentValues values);//插入方法

              update(String table, ContentValues values, String whereClause, String[] whereArgs)

               //eg:  db.update("user", values, "id=?", new String[]{"1"});
                        
第一個參數是要更新的表名,第二個參數是一個ContentValeus對象,第三個參數是where子句

              delete(String table, String whereClause, String[] whereArgs);//刪除方法

              查詢操作:

    class QueryListener implements OnClickListener{        @Override        public void onClick(View v) {            System.out.println("aaa------------------");            Log.d("myDebug", "myFirstDebugMsg");                        DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db");            SQLiteDatabase db = dbHelper.getReadableDatabase();            Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);            while(cursor.moveToNext()){                String name = cursor.getString(cursor.getColumnIndex("name"));                System.out.println("query--->" + name);            }        }    }

 

android-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.