首页 > 代码库 > android数据库操作
android数据库操作
一、首先新建Adnroid项目 然后就是新建 数据库文件
创建DBSQLiteOpenHelper类 并让它继承 SQLiteOpenHelper
public class DBSQLiteOpenHelper extends SQLiteOpenHelper { // 数据库的名称 private static final String name = "CSDN"; // 数据库的版本 private static final int version = 2; public DBSQLiteOpenHelper(Context context) { super(context, name, null, version); Log.v("DBSQLiteOpenHelper", "构造器....."); } // 当数据 库第一次创建的时候 执行的方法 @Override public void onCreate(SQLiteDatabase db) { // execSQL来挨靠 sql语句 db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),age integer)"); Log.v("DBSQLiteOpenHelper", "onCreate...创建执行第一次。"); } // 当版本发生变化则执行此方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("alter table person add account integer"); Log.v("DBSQLiteOpenHelper", "每次更新时都执行....."); } }
------接下来就是对数据库进行操作了----------------------------------------------------------------------
新建 domain包 中Person 类 其中字段如下:
public class Person {
private Integer id;
private String name;
private Integer age;
private Integer account;
生成 空构造器,带参数的构造器,和set/get 方法
为了方便测试 生成String
接下来就是实现PersonDao接口和PersonDaoImpl类了。----------------------------------------------------------------------
public interface PersonDao { /** * 插入数据操作 * @param db * @param entity */ public void insert(SQLiteDatabase db,Person entity); /** * 更新操作 * @param db * @param entity */ public void update(SQLiteDatabase db,Person entity); /** * 删除操作 * @param db * @param id */ public void delete(SQLiteDatabase db,Integer id); //查找所有 public List<Person> findAll(SQLiteDatabase db); //获取当前页信息 public List<Person> getNowPageInfo(SQLiteDatabase db,int nowpage,int pagesize); //根据ID查询 public Person findById(SQLiteDatabase db,Integer id); }
public class PersonDaoImpl implements PersonDao { @Override public void insert(SQLiteDatabase db, Person entity) { if (db.isOpen()) { db.execSQL( "insert into person(name,age,account) values(?,?,?)", new Object[] { entity.getName(), entity.getAge(), entity.getAccount() }); db.close(); } } @Override public void update(SQLiteDatabase db, Person entity) { if (db.isOpen()) { db.execSQL( "update person set name=?,age=?,account=? where personid=?", new Object[] { entity.getName(), entity.getAge(), entity.getAccount(), entity.getId() }); db.close(); } } @Override public void delete(SQLiteDatabase db, Integer id) { if (db.isOpen()) { db.execSQL("delete from person where personid=?", new Object[] { id }); db.close(); } } public List<Person> findAll(SQLiteDatabase db) { List<Person> persons = new ArrayList<Person>(); if (db.isOpen()) { Cursor cursor = db.rawQuery( "select personid,name,age,account from person", null); // 判断 是否含有下一个 while (cursor.moveToNext()) { // 创建 person对象 Person person = new Person(); // 为对象 的属性赋值 person.setId(cursor.getInt(0)); person.setName(cursor.getString(1)); person.setAge(cursor.getInt(2)); person.setAccount(cursor.getInt(3)); // 添加到集合中 persons.add(person); } } return persons; } @Override public List<Person> getNowPageInfo(SQLiteDatabase db, int nowpage, int pagesize) { // 开始记录 int start = (nowpage - 1) * pagesize; List<Person> persons = new ArrayList<Person>(); if (db.isOpen()) { Cursor cursor = db.rawQuery( "select personid,name,age,account from person limit ?,?", new String[] { start + "", pagesize + "" }); // 判断 是否含有下一个 while (cursor.moveToNext()) { // 创建 person对象 Person person = new Person(); // 为对象 的属性赋值 person.setId(cursor.getInt(0)); person.setName(cursor.getString(1)); person.setAge(cursor.getInt(2)); person.setAccount(cursor.getInt(3)); // 添加到集合中 persons.add(person); } } return persons; } @Override public Person findById(SQLiteDatabase db, Integer id) { Person person =null; if (db.isOpen()) { // 查询 Cursor cursor = db.rawQuery("select personid,name,age,account from person where personid = ?", new String[]{""+id}); // 判断是否含有下一个 if (cursor.moveToNext()) { person = new Person(); // 创建person对象 // 为对象的属性赋值 person.setId(cursor.getInt(0)); person.setName(cursor.getString(1)); person.setAge(cursor.getInt(2)); person.setAccount(cursor.getInt(3)); } } return person; } }
------------------------功能写好后,接下来就是对此方法 进行测试 了-----------------------------------------------------
第一步:首先在AndroidManifest.xml中加入下面红色代码:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.lession05_db" >
</instrumentation>
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.lession05_db" >
</instrumentation>
<uses-library android:name="android.test.runner" />
第二步:就是写测试方法DBTest 继承AndroidTestCase
public class DBTest extends AndroidTestCase { PersonDao personDao = new PersonDaoImpl(); public void createDB() { DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); SQLiteDatabase sdb = db.getWritableDatabase(); } public void insert() { DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); for(int i=1;i<=10;i++){ SQLiteDatabase sdb = db.getWritableDatabase(); Person entity = new Person(null, "chrp"+i, 23+i, 1000+i); personDao.insert(sdb, entity); } } public void update() { DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); SQLiteDatabase sdb = db.getWritableDatabase(); Person entity = new Person(2, "chrp99", 33, 11000); personDao.update(sdb, entity); } public void delete() { DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); SQLiteDatabase sdb = db.getWritableDatabase(); personDao.delete(sdb,11 ); } public void getNowPageInfo(){ DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); SQLiteDatabase sdb = db.getWritableDatabase(); List<Person> persons = personDao.getNowPageInfo(sdb, 1, 5); for(Person p:persons){ System.out.println(p.toString()); } } public void findAll(){ //创建数据库的管理的对象 DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); //获取 SqLiteDatebase实例对象 才能创建数据库 SQLiteDatabase sdb = db.getWritableDatabase(); List<Person> persons = personDao.findAll(sdb); for(Person p:persons){ System.out.println(p.toString()); } } public void findById(){ //创建数据库的管理的对象 DBSQLiteOpenHelper db = new DBSQLiteOpenHelper(this.getContext()); //获取 SqLiteDatebase实例对象 才能创建数据库 SQLiteDatabase sdb = db.getWritableDatabase(); Person p = personDao.findById(sdb, 3); if(p!=null){ System.out.println(p.toString()); } } }
android数据库操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。