码迷,mamicode.com
首页 > 移动开发 > 详细

Android · SQLiteOpenHelper实例PrivateContactsDBHelper

时间:2015-01-05 23:14:58      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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

标签:

原文地址:http://www.cnblogs.com/manhua/p/4204652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!