如何从SQLite的android系统中特定记录 [英] how to retrieve particular records from SQLite in android

查看:113
本文介绍了如何从SQLite的android系统中特定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从DB&放数据;它显示与分页列表。

即。如果我检索到4 items..i需要显示在第一页第一2项。

当我点击下一步按钮,其余2项应显示它取代老2。

我怎么会从数据库限制数据为2成这样?

我的code ..

  db.open();   // db.insertTitle(货币);
    //db.insertTitle(\"make);
    //db.insertTitle(\"make);
    //db.insertTitle(\"make);    光标C = db.getAllTitles();
    如果(c.moveToFirst())
    {
        做{
              字符串的firstName = c.getString(c.getColumnIndex(用户));
              results.add(名字);
           }而(c.moveToNext());
    }
  setListAdapter(新ArrayAdapter<串GT;(DisplayAll.this,android.R.layout.simple_list_item_1,结果));    db.close();
}

我DBAdapter ..

公开光标getAllTitles()
        {

 返回db.query(DATABASE_TABLE,新的String []
                    {
                KEY_ROWID,
                KEY_USER,
                },
                空值,
                空值,
                空值,
                空值,
                空值);
    }


解决方案

请试试这个

 公共光标getAllRecords(INT页,诠释totalRecord)
{
   返回db.rawQuery(选择your_table_name限制*+(页1)* totalRecord +,+ totalRecord,NU​​LL);
}

其中限制=你想在同一时间有多少记录,如果你想记录2然后通过限制= 2,如果10的记录然后设置上限= 10 ..
和页面=第一初始页面变量,1,当你第二次读取下一个记录增加1你的页面的变量。

I need to get data from DB & displayed it as list with pagination.

i.e. If i retrieved 4 items..i need to display first 2 items in first page.

When i click next button.,remaining 2 items should be displayed which replaces old 2.

How could i restrict data from DB as 2 like that?

My code..

 db.open();

   // db.insertTitle("Money");
    //db.insertTitle("make");
    //db.insertTitle("make");
    //db.insertTitle("make");

    Cursor c = db.getAllTitles();
    if (c.moveToFirst())
    {
        do {          
              String firstName = c.getString(c.getColumnIndex("user"));
              results.add( firstName );
           } while (c.moveToNext());
    }
  setListAdapter(new ArrayAdapter<String>(DisplayAll.this, android.R.layout.simple_list_item_1,results));

    db.close();
}

My DBAdapter..

public Cursor getAllTitles() {

        return db.query(DATABASE_TABLE, new String[] 
                    {
                KEY_ROWID, 
                KEY_USER,
                }, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

解决方案

Please try this

public Cursor getAllRecords(int page,int totalRecord)
{
   return db.rawQuery("select * from your_table_name limit "+(page-1)*totalRecord+","+totalRecord, null);
}

Where limit = how many record you want at a time if you want 2 record then pass limit = 2 if 10 record then set limit = 10.. and page = first initial page variable with 1 and when second time you fetch next record increase your page variable by 1 .

这篇关于如何从SQLite的android系统中特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆