以編程的方式使用SQLite資料庫 執行CRUD(建立 讀取 更新 刪除)操作

來源:互聯網
上載者:User

標籤:android   des   style   blog   color   使用   os   資料   

DBAdapter資料庫輔助類

package com.databses;    import android.content.ContentValues;  import android.content.Context;  import android.database.Cursor;  import android.database.SQLException;  import android.database.sqlite.SQLiteDatabase;  import android.database.sqlite.SQLiteOpenHelper;  import android.util.Log;    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 = 1;      final Context context;            //建立表sql語句      static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement,name text not null,email text not null)";            SQLiteDatabase db;      DatabaseHelper dBHelper;            public DBAdapter(Context context){          this.context = context;          dBHelper = new DatabaseHelper(context);      }            //資料庫輔助類 繼承SQLiteOpenHelper      private static class DatabaseHelper extends SQLiteOpenHelper{          public DatabaseHelper(Context context) {              super(context, DATABASE_NAME, null, DATABASE_VERSION);          }            //建立資料庫          @Override          public void onCreate(SQLiteDatabase db) {              try {                  db.execSQL(DATABASE_CREATE);              } catch (Exception e) {                  e.printStackTrace();              }          }          //升級資料庫版本          @Override          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {              Log.w(TAG, "upgrading db from version "+oldVersion+"to"+newVersion+",which will destory all old data");              db.execSQL("drop table if exists contacts");              onCreate(db);          }      }            //開啟資料庫      public DBAdapter open() throws SQLException      {          db = dBHelper.getWritableDatabase();          return this;      }            //關閉資料庫      public void close(){          dBHelper.close();      }            //插入一條連絡人記錄      public long insertContact(String name,String email){          ContentValues initalValues = new ContentValues();          initalValues.put(KEY_NAME,name);          initalValues.put(KEY_EMAIL, email);          return db.insert(DATABASE_TABLE, null, initalValues);      }            //刪除一條記錄      public boolean deleteContact(long rowId){          return db.delete(DATABASE_TABLE,KEY_ROWID+"="+rowId,null)>0;      }            //查詢所有連絡人      public Cursor getAllContacts()      {          return db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},null, null, null, null, null, null);      }            //查詢一條記錄      public Cursor getContact(long rowId) throws SQLException{          Cursor cursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL}, KEY_ROWID+"="+rowId, null, null, null, null, null);          if(cursor != null){              cursor.moveToFirst();          }          return cursor;      }            //更新一個連絡人      public boolean updateContact(long rowId,String name,String email){          ContentValues contentValues = new ContentValues();          contentValues.put(KEY_NAME, name);          contentValues.put(KEY_EMAIL, email);          return db.update(DATABASE_TABLE, contentValues, KEY_ROWID+"="+KEY_ROWID, null)>0;      }  }  

DatabaseActivity

package com.databses;    import android.os.Bundle;  import android.app.Activity;  import android.view.Menu;    public class DatabasesActivity extends Activity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_databases);          DBAdapter dbAdapter = new DBAdapter(this);                    dbAdapter.open();          dbAdapter.insertContact("weiwei", "[email protected]");          dbAdapter.insertContact("Jackson", "[email protected]");                }      }  
相關文章

聯繫我們

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