Android · SQLiteOpenHelper实例PrivateContactsDBHelper

本文介绍了一个用于管理私密联系人的Android应用程序数据库实现。该应用使用SQLiteOpenHelper创建了一个名为pContacts.db的数据库,并定义了包含_id、name、mobile和email字段的pcontacts表。文章提供了插入、查询、更新和删除联系人信息的方法。

 

 

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);
    }

}

 

转载于:https://www.cnblogs.com/manhua/p/4204652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值