SQLiteDatabase使用


     asc  升序

     desc  降序

_id integer  primary key  autoincrement  (id 整型 主键  自增长)

Integer类型主键 会自动自增长,可以不加autoincrement


SQL分页显示?

① selete * from testtable limit 2,1;

② selete * from testtable limit 2 offset 1;

这两个都是能完成需要,但是他们之间是有区别的:

①是从数据库中第三条开始查询,取一条数据,即第三条数据

②是从数据库中的第二条数据开始查询两条数据,即第二条和第三条。


增删改 execSQl();

rawQuery();

Cursor cursor=db.rawQuery (select *....)    将查询结果存在结果游标集里


SQLiteDatabase Demo

 

 

Class extends DatabaseOpenHelper 

 

 

DBhelper helper=new DBhelper(context);

SQLiteDatabase db=null ;

db=helper .getReadable()/getWirteable

Try{

查询方法:  Cursor cursor=db.rawQuery()

}finally{

If(db!=null)

db.close();

While(cursor.movetoNext)

New一个实体类list对象存放信息

String name=cursor.getString(cursor.getcolmnIndex(name));

                                                通过cursor获取name所在列

String pwd。。。。。。。。。





//写一个类,类中写入static方法,用于调用

public class DBManager {

 //保存

 public static void saveInfo(Context context,ScoreInfo item){

  DBOpenHelper helper=new DBOpenHelper(context);

  SQLiteDatabase db=null;

  try {

   db= helper.getWritableDatabase();

   db.execSQL("insert into info ('name','score') values (?,?)", new Object[]{item.getName(),item.getScore()});

  } catch (SQLException e) {

   e.printStackTrace();

  }finally{

   if(db!=null)

    db.close();

  }

 }

 public static List<Info> getInfoList(Context context,int diff){

  List<Info> list=new ArrayList<Info>();

  DBOpenHelper helper=new DBOpenHelper(context);

  SQLiteDatabase db=null;

  try {

   db= helper.getWritableDatabase();

   Cursor cursor=db.rawQuery("select name,score from info where____ order by score asc", new String[]{});

   int i=1;

   while(cursor.moveToNext()){

    Info item=new Info();

    item.setId(i++);

                                //通过cursor中方法用列名获取所处的列数

    item.setName(cursor.getString(cursor.getColumnIndex("name")));

    item.setScore(cursor.getInt(cursor.getColumnIndex("score")));

    list.add(item);

   }

  } catch (SQLException e) {

   e.printStackTrace();

  }finally{

   if(db!=null)

    db.close();

  }

  return list;

 }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值