Android SQLite增删改查
//目录结构
//编写DbOpenHelper.java类,继承SQLiteOpenHelper
- package sn.len.sqlite.service;
- import Android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DbOpenHelper extends SQLiteOpenHelper
- {
- private static final String DATABASENAME="database.db";
- private static final int DATABASVERSION=1;
- public DbOpenHelper(Context context)
- {
- //实例化父类,创建数据库
- super(context, DATABASENAME, null, DATABASVERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- db.execSQL("create table sqlitetable(id integer primary key autoincrement,name varchar(20))");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- }
- /*
- *1.如果数据库文件不存在,SQLiteOpenHelper在自动创建数据库后会调用oncreate()方法,
- *在该方法中一般需要创建表、视图等组件。在创建前数据库一般是空的,因此不需要先删除数据库中相关的组件。
- *2.如果数据库文件存在,并且当前版本号高于上次创建或升级的版本号,SQLiteOpenHelper会调用onUpdate()方法
- *调用该方法后会更新数据库的版本号。在onupdate()方法中除了创建表、视图等组件外,还需要先删除这些相关的组件,
- *因此,在调用onupdate()方法前,数据库是存在的,里面还原许多数据库组建。
- *详细了解:请看:http://hi.baidu.com/%C8%F4%CB%AA/blog/item/3d839c132e80353bdc54017b.html
- * */
- }
- package sn.len.sqllite.domain;
- public class Person
- {
- private Integer id;
- private String name;
- public Integer getId()
- {
- return id;
- }
- public void setId(Integer id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public Person(Integer id, String name)
- {
- this.id = id;
- this.name = name;
- }
- public Person()
- {
- }
- @Override
- public String toString()
- {
- return "Person [id=" + id + + name + }
- package sn.len.sqlite.service;
- import java.util.ArrayList;
- import java.util.List;
- import sn.len.sqllite.domain.Person;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- public class PersonService
- {
- //拿到SQLiteDatabase对象
- private DbOpenHelper dbhelp;
- public PersonService(Context context)
- {
- this.dbhelp=new DbOpenHelper(context);
- }
- //保存
- public void save(Person person)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("insert into sqlitetable(name) values(?)", new Object[]{person.getName()});
- }
- //删除
- public void del(Integer id)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("delete from sqlitetable where id=?", new Object[]{id});
- }
- //更新
- public void update(Person person)
- {
- SQLiteDatabase dbopen=dbhelp.getWritableDatabase();
- dbopen.execSQL("update sqlitetable set name=? where id=?", new Object[]{person.getName(),person.getId()});
- }
- //查找
- public Person select(Integer id)
- {
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cursor=dbopen.rawQuery("select * from sqlitetable where id=?",new String[]{id.toString()});
- int db_id=cursor.getInt(cursor.getColumnIndex("id"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- return new Person(db_id,name);
- }
- //分页
- public List<Person> getScrollData(Integer offset,Integer maxResult)
- {
- List<Person> p=new ArrayList<Person>();
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cursor=dbopen.rawQuery("select * from sqlitetable limit ?,?", new String[]{offset.toString(),maxResult.toString()});
- while(cursor.moveToNext())
- {
- int id=cursor.getInt(cursor.getColumnIndex("id"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- Person persons=new Person(id,name);
- p.add(persons);
- }
- cursor.close();
- return p;
- }
- //得到总行数
- public long getCount()
- {
- SQLiteDatabase dbopen=dbhelp.getReadableDatabase();
- Cursor cusor=dbopen.rawQuery("select count(*) from sqlitetable", null);
- cusor.moveToFirst();//不用判断,不管有没能结果都会返回一条记录
- long count=cusor.getLong(0);
- return count;
- }
- //操作数据,基本都用execSQL();
- //查询数据,基本都用rawQuery();
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20