1.安卓提供了sqliteDatabase代表一个数据库,一单应用程序获得了代表数据库的sqliteDATAbase对象,接下来就可以通过对象来管理操作数据库
实例一:
package com.xiaoxin.MobleSafe.dao; import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteDatabase.CursorFactory; import android.database.sqlite.sqliteOpenHelper; public class BlackNumberDBOpenHelper extends sqliteOpenHelper{ /** * 数据库创建的构造方法 数据库名称 blacknumber.db * @param context */ public BlackNumberDBOpenHelper(Context context) { super(context,"blacknumber.db",null,1); } //初始化数据库的表结构 @Override public void onCreate(sqliteDatabase db) { db.execsql("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))"); } @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { } }可以看出单独创出一个类 将数据库和表结构实现出来
2.同时创建一个业务bean方法进行集合接受数据
package com.xiaoxin.mobilesafe.domain; /** * 黑名单号码的业务bean * @author Administrator * */ public class BlackNumberInfo { private String number; private String mode; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } @Override public String toString() { return "BlackNumberInfo [number=" + number + ",mode=" + mode + "]"; } }
业务bean中有号码 和拦截模式
package com.itheima.mobilesafe.db.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import com.itheima.mobilesafe.db.BlackNumberDBOpenHelper; import com.itheima.mobilesafe.domain.BlackNumberInfo; /** * 黑名单数据库的增删改查业务类 * @author Administrator * */ public class BlackNumberDao { private BlackNumberDBOpenHelper helper; /** * 构造方法 * @param context 上下文 */ public BlackNumberDao(Context context) {
<span style="white-space:pre"> </span>//将数据库传入 helper = new BlackNumberDBOpenHelper(context); } /** * 查询黑名单号码是是否存在 * @param number * @return */ public boolean find(String number){ boolean result = false; sqliteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from blacknumber where number=?",new String[]{number}); if(cursor.moveToNext()){ result = true; } cursor.close(); db.close(); return result; } /** * 查询黑名单号码的拦截模式 * @param number * @return 返回号码的拦截模式,不是黑名单号码返回null */ public String findMode(String number){ String result = null; sqliteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select mode from blacknumber where number=?",new String[]{number}); if(cursor.moveToNext()){ result = cursor.getString(0); } cursor.close(); db.close(); return result; } /** * 查询全部黑名单号码 * @return */ public List<BlackNumberInfo> findAll(){ List<BlackNumberInfo> result = new ArrayList<BlackNumberInfo>(); sqliteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select number,mode from blacknumber order by _id desc",null); while(cursor.moveToNext()){ BlackNumberInfo info = new BlackNumberInfo(); String number = cursor.getString(0); String mode = cursor.getString(1); info.setMode(mode); info.setNumber(number); result.add(info); } cursor.close(); db.close(); return result; } /** * 添加黑名单号码 * @param number 黑名单号码 * @param mode 拦截模式 1.电话拦截 2.短信拦截 3.全部拦截 */ public void add(String number,String mode){ sqliteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("number",number); values.put("mode",mode); db.insert("blacknumber",values); db.close(); } /** * 修改黑名单号码的拦截模式 * @param number 要修改的黑名单号码 * @param newmode 新的拦截模式 */ public void update(String number,String newmode){ sqliteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("mode",newmode); db.update("blacknumber",values,"number=?",new String[]{number}); db.close(); } /** * 删除黑名单号码 * @param number 要删除的黑名单号码 */ public void delete(String number){ sqliteDatabase db = helper.getWritableDatabase(); db.delete("blacknumber",new String[]{number}); db.close(); } }可以看出增删该查与JDBC是差不多的
读入
sqliteDatabase db = helper.getReadableDatabase();
写入
sqliteDatabase db = helper.getWritableDatabase();
原文链接:https://www.f2er.com/sqlite/200362.html