惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(三十一)------SQLite分页读取(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

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

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

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

分页栏的pagebuttons.xml的源码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="wrap_content" android:paddingBottom="4dip"  
  4.     android:layout_width="fill_parent">  
  5.     <TextView android:layout_width="wrap_content"  
  6.         android:layout_below="@+id/ItemImage" android:layout_height="wrap_content"  
  7.         android:text="TextView01" android:layout_centerHorizontal="true"  
  8.         android:id="@+id/ItemText">  
  9.     </TextView>  
  10. </RelativeLayout>    

main.xml的源码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Button android:layout_height="wrap_content"  
  6.         android:layout_width="fill_parent" android:id="@+id/btnCreateDB"  
  7.         android:text="创建数据库"></Button>  
  8.     <Button android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent" android:text="插入一串实验数据" android:id="@+id/btnInsertRec"></Button>  
  10.     <Button android:layout_height="wrap_content" android:id="@+id/btnClose"  
  11.         android:text="关闭数据库" android:layout_width="fill_parent"></Button>  
  12.     <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"  
  13.         android:layout_width="fill_parent" android:layout_height="256dip"></EditText>  
  14.     <GridView android:id="@+id/gridview" android:layout_width="fill_parent"  
  15.         android:layout_height="32dip" android:numColumns="auto_fit"  
  16.         android:columnWidth="40dip"></GridView>  
  17. </LinearLayout>  

本文程序源码如下:

  1. package com.testSQLite;    
  2.     
  3. import java.util.ArrayList;    
  4. import java.util.HashMap;    
  5. import android.app.Activity;    
  6. import android.database.Cursor;    
  7. import android.database.SQLException;    
  8. import android.database.sqlite.SQLiteDatabase;    
  9. import android.os.Bundle;    
  10. import android.util.Log;    
  11. import android.view.View;    
  12. import android.widget.AdapterView;    
  13. import android.widget.AdapterView.OnItemClickListener;    
  14. import android.widget.Button;    
  15. import android.widget.EditText;    
  16. import android.widget.GridView;    
  17. import android.widget.SimpleAdapter;    
  18.     
  19. public class testSQLite extends Activity {    
  20.         
  21.     Button btnCreateDB, btnInsert, btnClose;    
  22.     EditText edtSQL;  
  23.     SQLiteDatabase db;    
  24.     int id;  
  25.     static final int PageSize=10;  
  26.     private static final String TABLE_NAME = "stu";    
  27.     private static final String ID = "id";    
  28.     private static final String NAME = "name";    
  29.         
  30.     SimpleAdapter saPageID;  
  31.     ArrayList<HashMap<String, String>> lstPageID;  
  32.     
  33.     @Override    
  34.     public void onCreate(Bundle savedInstanceState) {    
  35.         super.onCreate(savedInstanceState);    
  36.         setContentView(R.layout.main);    
  37.         btnCreateDB = (Button) this.findViewById(R.id.btnCreateDB);    
  38.         btnCreateDB.setOnClickListener(new ClickEvent());    
  39.     
  40.         btnInsert = (Button) this.findViewById(R.id.btnInsertRec);    
  41.         btnInsert.setOnClickListener(new ClickEvent());    
  42.     
  43.         btnClose = (Button) this.findViewById(R.id.btnClose);    
  44.         btnClose.setOnClickListener(new ClickEvent());    
  45.             
  46.         edtSQL=(EditText)this.findViewById(R.id.EditText01);    
  47.             
  48.         GridView gridview = (GridView) findViewById(R.id.gridview);  
  49.           
  50.         lstPageID = new ArrayList<HashMap<String, String>>();    
  51.     
  52.           
  53.         saPageID = new SimpleAdapter(testSQLite.this  
  54.                 lstPageID,  
  55.                 R.layout.pagebuttons,  
  56.                 new String[] { "ItemText" },    
  57.                 new int[] { R.id.ItemText });    
  58.     
  59.           
  60.         gridview.setAdapter(saPageID);    
  61.           
  62.         gridview.setOnItemClickListener(new OnItemClickListener(){    
  63.     
  64.             @Override    
  65.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    
  66.                     long arg3) {    
  67.                 LoadPage(arg2);  
  68.             }    
  69.         });    
  70.     
  71.     }    
  72.     
  73.         
  74.     class ClickEvent implements View.OnClickListener {    
  75.     
  76.         @Override    
  77.         public void onClick(View v) {    
  78.             if (v == btnCreateDB) {    
  79.                 CreateDB();    
  80.             } else if (v == btnInsert) {    
  81.                 InsertRecord(16);  
  82.                 RefreshPage();    
  83.             }else if (v == btnClose) {    
  84.                 db.close();    
  85.             }    
  86.         }    
  87.     
  88.     }    
  89.         
  90.     
  91.      
  92.  
  93.  
  94.  
  95.     
  96.     void LoadPage(int pageID)    
  97.     {    
  98.         String sql= "select * from " + TABLE_NAME +     
  99.         " Limit "+String.valueOf(PageSize)+ " Offset " +String.valueOf(pageID*PageSize);    
  100.         Cursor rec = db.rawQuery(sql, null);    
  101.     
  102.         setTitle("当前分页的数据总数:"+String.valueOf(rec.getCount()));    
  103.             
  104.           
  105.         String title = "";    
  106.         int colCount = rec.getColumnCount();    
  107.         for (int i = 0; i < colCount; i++)    
  108.             title = title + rec.getColumnName(i) + "     ";    
  109.     
  110.             
  111.           
  112.         String content="";    
  113.         int recCount=rec.getCount();    
  114.         for (int i = 0; i < recCount; i++) {  
  115.             rec.moveToPosition(i);    
  116.             for(int ii=0;ii<colCount;ii++)  
  117.             {    
  118.                 content=content+rec.getString(ii)+"     ";    
  119.             }    
  120.             content=content+"/r/n";    
  121.         }    
  122.             
  123.         edtSQL.setText(title+"/r/n"+content);  
  124.         rec.close();    
  125.     }    
  126.         
  127.      
  128.  
  129.     
  130.     void CreateDB() {    
  131.           
  132.         db = SQLiteDatabase.create(null);    
  133.         Log.e("DB Path", db.getPath());    
  134.         String amount = String.valueOf(databaseList().length);    
  135.         Log.e("DB amount", amount);    
  136.           
  137.         String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID    
  138.                 + " text not null, " + NAME + " text not null " + ");";    
  139.         try {    
  140.             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);    
  141.             db.execSQL(sql);    
  142.         } catch (SQLException e) {}    
  143.     }    
  144.     
  145.      
  146.  
  147.     
  148.     void InsertRecord(int n) {    
  149.         int total = id + n;    
  150.         for (; id < total; id++) {    
  151.             String sql = "insert into " + TABLE_NAME + " (" + ID + ", " + NAME    
  152.                     + ") values('" + String.valueOf(id) + "', 'test');";    
  153.             try {    
  154.                 db.execSQL(sql);    
  155.             } catch (SQLException e) {    
  156.             }    
  157.         }    
  158.     }    
  159.     
  160.      
  161.  
  162.     
  163.     void RefreshPage()    
  164.     {    
  165.         String sql = "select count(*) from " + TABLE_NAME;    
  166.         Cursor rec = db.rawQuery(sql, null);    
  167.         rec.moveToLast();    
  168.         long recSize=rec.getLong(0);  
  169.         rec.close();    
  170.         int pageNum=(int)(recSize/PageSize) + 1;  
  171.             
  172.         lstPageID.clear();    
  173.         for (int i = 0; i < pageNum; i++) {    
  174.             HashMap<String, String> map = new HashMap<String, String>();    
  175.             map.put("ItemText""No." + String.valueOf(i));  
  176.     
  177.             lstPageID.add(map);    
  178.         }    
  179.         saPageID.notifyDataSetChanged();    
  180.     }    
  181. }