加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

8.SQLite分页读取

发布时间:2020-12-12 20:15:59 所属栏目:百科 来源:网络整理
导读:Android内置了常用于嵌入式系统的SQLite,免去了开发者自己安装的功夫。SQLite 支持多数 SQL92 标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xmlversion="1.0"encoding"utf-8"?><RelativeLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"fill_parent"android:layout_height"wrap_content"android:paddingBottom"4dip">TextViewandroid:id"@+id/ItemText""wrap_content""wrap_content"android:layout_below"@+id/ItemImage"android:layout_centerHorizontal"true"android:text"TextView01"/></RelativeLayout>

main.xml源码

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 LinearLayoutandroid:orientation"vertical""fill_parent""fill_parent"Button"@+id/btnCreateDB""fill_parent""创建数据库"/>Button"@+id/btnInsertRec""fill_parent""wrap_content""插入一串实验数据"/>Button"@+id/btnClose""fill_parent""wrap_content""关闭数据库"/>EditText"@+id/EditText01""fill_parent""256dip""@+id/EditText01"/>GridView"@+id/gridview""fill_parent""32dip"android:numColumns"auto_fit"android:columnWidth"40dip"/>LinearLayout 本文程序源码

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 package com.testSQLite; import java.util.ArrayList; java.util.HashMap; android.app.Activity; android.database.Cursor; android.database.SQLException; android.database.sqlite.SQLiteDatabase; android.os.Bundle; android.util.Log; android.view.View; android.widget.AdapterView; android.widget.AdapterView.OnItemClickListener; android.widget.Button; android.widget.EditText; android.widget.GridView; android.widget.SimpleAdapter; public class testSQLite extends Activity { /** Called when the activity is first created. */ Button btnCreateDB,btnInsert,btnClose; EditText edtSQL; // 显示分页数据 SQLiteDatabase db; int id; // 添加记录时的id累加标记,必须全局 static final PageSize = 10 ; // 分页时,每页的数据总数 private final String TABLE_NAME = "stu" ; String ID = "id" ; String NAME = "name" ; SimpleAdapter saPageID; // 分页栏适配器 ArrayList<HashMap<String,String>> lstPageID; // 分页栏的数据源,与PageSize和数据总数相关 @Override void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); btnCreateDB = (Button) this .findViewById(R.id.btnCreateDB); btnCreateDB.setOnClickListener( new ClickEvent()); btnInsert = (Button) .findViewById(R.id.btnInsertRec); btnInsert.setOnClickListener( ClickEvent()); btnClose = (Button) .findViewById(R.id.btnClose); btnClose.setOnClickListener( ClickEvent()); edtSQL = (EditText) .findViewById(R.id.EditText01); GridView gridview = (GridView) findViewById(R.id.gridview); // 分页栏控件 // 生成动态数组,并且转入数据 lstPageID = // 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应 saPageID = SimpleAdapter(testSQLite. , // 没什么解释 lstPageID,0)!important">// 数据来源 R.layout.pagebuttons,0)!important">// XML实现 String[] { "ItemText" }, new [] { R.id.ItemText }); // 添加并且显示 gridview.setAdapter(saPageID); // 添加消息处理 gridview.setOnItemClickListener( OnItemClickListener() { @Override onItemClick(AdapterView<?> arg0,View arg1,monospace!important; font-size:1em!important; min-height:inherit!important">arg2, long arg3) { LoadPage(arg2); // 根据所选分页读取对应的数据 } }); } ClickEvent implements View.OnClickListener { @Override onClick(View v) { if (v == btnCreateDB) { CreateDB(); } else (v == btnInsert) { InsertRecord( 16 ); // 插入16条记录 RefreshPage(); (v == btnClose) { db.close(); } } } /** * 读取指定ID的分页数据 SQL:Select * From TABLE_NAME Limit 9 Offset 10; * 表示从TABLE_NAME表获取数据,跳过10行,取9行 */ LoadPage( pageID) { String sql = "select * from " + TABLE_NAME + " Limit " + String.valueOf(PageSize) + " Offset " + String.valueOf(pageID * PageSize); Cursor rec = db.rawQuery(sql,153)!important">null ); setTitle( "当前分页的数据总数:" + String.valueOf(rec.getCount())); // 取得字段名称 String title = "" ; colCount = rec.getColumnCount(); for ( i = 0 ; i < colCount; i++) title = title + rec.getColumnName(i) + " " ; // 列举出所有数据 String content = ; recCount = rec.getCount(); ; i < recCount; i++) { // 定位到一条数据 rec.moveToPosition(i); ii = ; ii < colCount; ii++) // 定位到一条数据中的每个字段 { content = content + rec.getString(ii) + ; } content = content + "/r/n" ; } edtSQL.setText(title + + content); // 显示出来 rec.close(); } /** * 在内存创建数据库和数据表 */ CreateDB() { // 在内存创建数据库 db = SQLiteDatabase.create( ); Log.e( "DB Path" String amount = String.valueOf(databaseList().length); "DB amount" // 创建数据表 "CREATE TABLE " " (" + ID + " text not null," + NAME + " text not null " ");" ; try { db.execSQL( "DROP TABLE IF EXISTS " + TABLE_NAME); db.execSQL(sql); catch (SQLException e) { } } /** * 插入N条数据 */ InsertRecord( n) { total = id + n; (; id < total; id++) { "insert into " + ID + ",monospace!important; font-size:1em!important; min-height:inherit!important">+ NAME ") values('" + String.valueOf(id) + "','test');" ; { db.execSQL(sql); (SQLException e) { } } } /** * 插入之后刷新分页 */ RefreshPage() { "select count(*) from " + TABLE_NAME; ); rec.moveToLast(); recSize = rec.getLong( // 取得总数 rec.close(); pageNum = ( ) (recSize / PageSize) + 1 // 取得分页数 lstPageID.clear(); ; i < pageNum; i++) { HashMap<String,String> map = map.put( "No." + String.valueOf(i)); lstPageID.add(map); } saPageID.notifyDataSetChanged(); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

Android内置了常用于嵌入式系统的SQLite,免去了开发者自己安装的功夫。SQLite 支持多数 SQL92 标准,很多常用的SQL命令都能在SQLite上面使用,除此之外Android还提供了一系列自定义的方法去简化对SQLite数据库的操作。不过有跨平台需求的程序就建议使用标准的SQL语句,毕竟这样容易在多个平台之间移植。

先贴出本文程序运行的结果:

本文主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个分页栏(关于GridView的用法),用于把数据分页显示。

分页栏的pagebuttons.xml的源码

?