zoukankan      html  css  js  c++  java
  • Android · SQLiteOpenHelper实例PrivateContactsDBHelper

    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);
        }
    
    }
  • 相关阅读:
    在winform下实现左右布局多窗口界面的方法(一)
    C# 使用API检查域用户名和密码是否正确
    C#检查网络是否可以连接互联网
    总结:实体类和(XML或二进制)之间相互转(序列化和反序列化)
    XML和实体类之间相互转换(序列化和反序列化)
    C# XML反序列化与序列化举例:XmlSerializer
    XML文件与实体类的互相转换
    Message类的属性Msg所关联的消息ID
    C# Message 消息处理
    在.net中读写config文件的各种方法(自定义config节点)
  • 原文地址:https://www.cnblogs.com/manhua/p/4204652.html
Copyright © 2011-2022 走看看