做android的数据库方面用的很少,所以用的时候记录下来,下次碰到直接copy,一下代码是最基本的实现:
首先是继承helper类:
package com.create.rycreateim.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import com.create.rycreateim.utils.Constant; /** * Created by Administrator on 2016/4/14 0014. */ public class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) {
/**
* 四个参数
* 上下文
* 数据库名字,根据自己的习惯起,必须是string
* 第三个cursor工厂,一般不用管,设为null
* 第四个为版本号
*/ super(context, Constant.DB_NAME, null, 1);
} @Override public void onCreate(SQLiteDatabase db) {
//创建表格,只在第一次初始化的时候执行 db.execSQL("create table "+ Constant.USERTABLENAME+"(_id integer primary key autoincrement, userid varchar(20), usernick varchar(20), portraituri varchar(100));"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //根据版本号更新数据库的操作,只有版本号升高时 才会执行 } }
下面为操作数据库的工具类:
package com.create.rycreateim.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import com.create.rycreateim.utils.Constant; import java.util.ArrayList; import io.rong.imlib.model.UserInfo; /** * 设置为单例 * Created by Administrator on 2016/4/15 0015. */ public class DbUtils { private final DbHelper dbHelper; private DbUtils(Context context) { //在构造方法中创建helper对象,即保持了helper也为单一对象。 dbHelper = new DbHelper(context); } private static DbUtils instance; public synchronized static DbUtils getInstance(Context context) { if (instance == null) { instance = new DbUtils(context); } return instance; } /** * 插入新的用户 * * @param userInfo */ public boolean saveNewUser(UserInfo userInfo) { SQLiteDatabase db = this.dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("userid", userInfo.getUserId()); values.put("usernick", userInfo.getName()); values.put("portraituri", (userInfo.getPortraitUri()) + ""); long insert = db.insert(Constant.USERTABLENAME, null, values); if(insert==-1){ return false; } return true; } /** * 更新用户信息 * @param userInfo */ public boolean updateUserInfo(UserInfo userInfo) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("usernick", userInfo.getName()); values.put("portraituri", userInfo.getPortraitUri() + ""); int update = db.update(Constant.USERTABLENAME, values, "userid=?", new String[]{userInfo.getUserId()}); if(update<1){ return false; } return true; } /** * 用户信息的删除操作 * @param userInfo */ public boolean deletUserInfo(UserInfo userInfo){ SQLiteDatabase db = dbHelper.getWritableDatabase(); int delete = db.delete(Constant.USERTABLENAME, "userid=?", new String[]{userInfo.getUserId()}); if(delete<1){ return false; } return true; } /** * 查询所有的用户信息 * @return */ public ArrayList<UserInfo> queryUserInfo(){ SQLiteDatabase db = dbHelper.getReadableDatabase(); ArrayList<UserInfo> userInfos=new ArrayList<>(); Cursor cursor = db.query(Constant.USERTABLENAME, new String[]{"userid,usernick,portraituri"}, null, null, null, null, null); UserInfo userInfo=null; while (cursor.moveToNext()){ String userid = cursor.getString(cursor.getColumnIndex("userid")); String usernick = cursor.getString(cursor.getColumnIndex("usernick")); String portraituri = cursor.getString(cursor.getColumnIndex("portraituri")); Uri uri=Uri.parse(portraituri); userInfo = new UserInfo(userid,usernick,uri); userInfos.add(userInfo); } cursor.close(); return userInfos; } }