android封装好的方法写增删改查
2014-02-23 17:11
232 查看
PersonDBOpenHelper.java
AndroidManifest.xml
package com.demo.introductiontothedb; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * 1 .写一个类 继承 SQLiteOpenHelper 帮助创建数据库 版本的控制 * @author Administrator * */ public class PersonDBOpenHelper extends SQLiteOpenHelper { /** * 数据库创建帮助类的构造方法 * @param context */ public PersonDBOpenHelper(Context context) { //name 数据库文件的名称 //factory 访问数据库一个数据库的游标工厂 // version 数据库的版本 super(context, "person.db", null, 2); } /** * 当数据库第一次被创建的是 调用的方法. * 适合做数据库表结构的初始化 * 注意:数据库被创建后,onCreate不会再被调用 * 如果想更改数据库,比如更改表结构,更新版本等 * 使用下面的onUpgrade()方法,这里要想添加money列, * 还必须更改数据库版本,onUpgrade()才会执行 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table personInfo (id integer primary key autoincrement, name varchar(20), phone varchar(20),address varchar(50)) "); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("alter table personInfo add money varchar(20)"); Log.i("TiShi", "数据库被更改啦"); } }PersonDao.java
package com.demo.introductiontothedb.dao; import com.demo.introductiontothedb.PersonDBOpenHelper; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class PersonDao { //增删改查 //javaweb 1.加载jdbc驱动.连接 2.准备sql 3.查询 private PersonDBOpenHelper helper; //任何人使用 dao 都要传递一个上下文 public PersonDao(Context context) { helper = new PersonDBOpenHelper(context); } /** * 添加一条记录 */ public boolean add(String name,String phone,String address){ SQLiteDatabase db = helper.getWritableDatabase(); // db.execSQL("insert into personInfo (name,phone,address) values (?,?,?)", // new Object[]{name,phone,address}); ContentValues values = new ContentValues(); //map集合 values.put("name", name); values.put("phone", phone); values.put("address", address); long result = db.insert("personInfo", null, values); db.close(); if(result != -1){ return true; }else{ return false; } } /** * 根据名字查找一条记录 */ public int find(String name){ int id = -1; SQLiteDatabase db = helper.getReadableDatabase(); // Cursor cursor = db.rawQuery("select id from personInfo where name=?", // new String[]{name}); Cursor cursor = db.query("personInfo", new String[]{"id"}, "name=?", new String[]{name}, null, null, null); if(cursor.moveToFirst()){ id = cursor.getInt(0); } cursor.close(); db.close(); return id; } /** * 删除一条记录 */ public boolean delete(int id){ SQLiteDatabase db = helper.getWritableDatabase(); //db.execSQL("delete from personInfo where id=?", new Object[]{id}); int result = db.delete("personInfo", "id=?", new String[]{id + ""}); db.close(); if(result > 0){ return true; }else{ return false; } } /** * 更改一条记录 */ public boolean update(String name,String phone,int id){ SQLiteDatabase db = helper.getWritableDatabase(); // db.execSQL("update personInfo set name=?,phone=? where id=?", // new Object[]{name,phone,id}); //String table, ContentValues values, String whereClause, String[] whereArgs) ContentValues values = new ContentValues(); values.put("name", name); values.put("phone", phone); int result = db.update("personInfo", values, "id=?", new String[]{id + ""}); db.close(); if(result > 0){ return true; }else{ return false; } } }TestPerson.java
package com.demo.introductiontothedb.test; import com.demo.introductiontothedb.dao.PersonDao; import android.test.AndroidTestCase; public class TestPerson extends AndroidTestCase { private PersonDao dao; @Override protected void setUp() throws Exception { dao = new PersonDao(getContext()); super.setUp(); } public void testAdd(){ assertEquals(true,dao.add("美女","7777", "中国北京")); } public void testDelete(){ int id = dao.find("麻生希"); //dao.delete(id); assertEquals(true, dao.delete(id)); } public void testUpdate(){ //dao.update("景甜", "888888", 10); assertEquals(true, dao.update("景甜", "888888", 10)); } public void testFind(){ //dao.find("麻生希"); assertEquals(8, dao.find("麻生希")); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.introductiontothedb" android:versionCode="1" android:versionName="1.0" > <instrumentation android:name="android.test.InstrumentationTestRunner" android:label="Tests for My App" android:targetPackage="com.demo.introductiontothedb" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 在application节点下添加 使用的测试library --> <uses-library android:name="android.test.runner" /> <activity android:name="com.demo.introductiontothedb.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- 通过Sql语句和android封装好的方法控制SQLite数据库增删改查
- 封装数据库查询学生信息(的增删查改)的方法
- dao层各种增删改查封装成对象方法 以student表 操作
- 将HTML5封装成android应用APK文件的几种方法
- Hibernate的手动基本配置和DAO类增删改查方法的封装
- Android:ContentProvider的基本方法以及ContentResolver的对Provider的简单增删改查
- 【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)
- unity 调用android 常用方法封装
- 将HTML5封装成android应用APK文件的几种方法
- Android中封装View提供接口供点击事件回调的方法及使用
- <Android>之三级缓存(及封装方法)
- Android对SQLite数据库进行封装使用反射来进行表的增删改查
- Android BaseActivity、BaseFragment等 常用方法封装
- 最新google play内购(com.android.billingclient) 教程 代码 封装 使用方法
- Android使用OkHttp实例,以及OkHttp方法封装
- JDBC封装增删改查,增加后返回主键等方法
- android编程实现图片库的封装方法
- Android封装字符串非空方法
- android下创建数据库的步骤 增删改查 和 测试程序的方法流程
- Android 开发改变vuforia样例文件的背景图、模型方法的简单封装