Android · SQLiteOpenHelper執行個體PrivateContactsDBHelper

來源:互聯網
上載者:User

標籤:

 

 

package privatecontact;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class PrivateContactsDBHelper extends SQLiteOpenHelper {    public static final String DATABASE_NAME = "pContacts.db";    public static final int DATABASE_VERSION = 1;    public static final String TABLE_NAME = "pcontacts";    public final static String ID = "_id";    public final static String NAME = "name";    public final static String MOBILE = "mobile";    public final static String EMAIL ="email";        public PrivateContactsDBHelper(Context context) {        super(context, DATABASE_NAME, null, DATABASE_VERSION);    }    @Override    public void onCreate(SQLiteDatabase db) {        String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID                + " INTEGER primary key autoincrement, " + NAME                + " text, " + MOBILE + " text, " + EMAIL + " text);";        db.execSQL(sql);    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);        onCreate(db);    }        public Cursor select() {        SQLiteDatabase db = this.getReadableDatabase();        Cursor cursor = db                .query(TABLE_NAME, null, null, null, null, null, null);        return cursor;    }    public long insert(String arg1, String arg2, String arg3) {        SQLiteDatabase db = this.getWritableDatabase();        /* ContentValues */        ContentValues cv = new ContentValues();        cv.put(NAME, arg1);        cv.put(MOBILE, arg2);        cv.put(EMAIL, arg3);        long row = db.insert(TABLE_NAME, null, cv);        return row;    }    public void delete(int id) {        SQLiteDatabase db = this.getWritableDatabase();        String where = ID + " = ?";        String[] whereValue = { Integer.toString(id) };        db.delete(TABLE_NAME, where, whereValue);    }    public void update(int id, String arg1, String arg2, String arg3) {        SQLiteDatabase db = this.getWritableDatabase();        String where = ID + " = ?";        String[] whereValue = { Integer.toString(id) };        ContentValues cv = new ContentValues();        cv.put(NAME, arg1);        cv.put(MOBILE, arg2);        cv.put(EMAIL, arg3);        db.update(TABLE_NAME, cv, where, whereValue);    }}

 

Android · SQLiteOpenHelper執行個體PrivateContactsDBHelper

聯繫我們

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