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

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

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

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

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

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_height="wrap_content"android:paddingBottom="4dip"
  4. android:layout_width="fill_parent">
  5. <TextViewandroid: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的源码如下:

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

本文程序源码如下:

view plain copy to clipboard print ?
  1. packagecom.testSQLite;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importandroid.app.Activity;
  5. importandroid.database.Cursor;
  6. importandroid.database.SQLException;
  7. importandroid.database.sqlite.SQLiteDatabase;
  8. importandroid.os.Bundle;
  9. importandroid.util.Log;
  10. importandroid.view.View;
  11. importandroid.widget.AdapterView;
  12. importandroid.widget.AdapterView.OnItemClickListener;
  13. importandroid.widget.Button;
  14. importandroid.widget.EditText;
  15. importandroid.widget.GridView;
  16. importandroid.widget.SimpleAdapter;
  17. publicclasstestSQLiteextendsActivity{
  18. /**Calledwhentheactivityisfirstcreated.*/
  19. ButtonbtnCreateDB,btnInsert,btnClose;
  20. EditTextedtSQL;//显示分页数据
  21. SQLiteDatabasedb;
  22. intid;//添加记录时的id累加标记,必须全局
  23. staticfinalintPageSize=10;//分页时,每页的数据总数
  24. privatestaticfinalStringTABLE_NAME="stu";
  25. privatestaticfinalStringID="id";
  26. privatestaticfinalStringNAME="name";
  27. SimpleAdaptersaPageID;//分页栏适配器
  28. ArrayList<HashMap<String,String>>lstPageID;//分页栏的数据源,与PageSize和数据总数相关
  29. @Override
  30. publicvoidonCreate(BundlesavedInstanceState){
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. btnCreateDB=(Button)this.findViewById(R.id.btnCreateDB);
  34. btnCreateDB.setOnClickListener(newClickEvent());
  35. btnInsert=(Button)this.findViewById(R.id.btnInsertRec);
  36. btnInsert.setOnClickListener(newClickEvent());
  37. btnClose=(Button)this.findViewById(R.id.btnClose);
  38. btnClose.setOnClickListener(newClickEvent());
  39. edtSQL=(EditText)this.findViewById(R.id.EditText01);
  40. GridViewgridview=(GridView)findViewById(R.id.gridview);//分页栏控件
  41. //生成动态数组,并且转入数据
  42. lstPageID=newArrayList<HashMap<String,String>>();
  43. //生成适配器的ImageItem<====>动态数组的元素,两者一一对应
  44. saPageID=newSimpleAdapter(testSQLite.this,//没什么解释
  45. lstPageID,//数据来源
  46. R.layout.pagebuttons,//XML实现
  47. newString[]{"ItemText"},
  48. newint[]{R.id.ItemText});
  49. //添加并且显示
  50. gridview.setAdapter(saPageID);
  51. //添加消息处理
  52. gridview.setOnItemClickListener(newOnItemClickListener(){
  53. @Override
  54. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
  55. longarg3){
  56. LoadPage(arg2);//根据所选分页读取对应的数据
  57. }
  58. });
  59. }
  60. classClickEventimplementsView.OnClickListener{
  61. @Override
  62. publicvoidonClick(Viewv){
  63. if(v==btnCreateDB){
  64. CreateDB();
  65. }elseif(v==btnInsert){
  66. InsertRecord(16);//插入16条记录
  67. RefreshPage();
  68. }elseif(v==btnClose){
  69. db.close();
  70. }
  71. }
  72. }
  73. /*
  74. *读取指定ID的分页数据
  75. *SQL:Select*FromTABLE_NAMELimit9Offset10;
  76. *表示从TABLE_NAME表获取数据,跳过10行,取9行
  77. */
  78. voidLoadPage(intpageID)
  79. {
  80. Stringsql="select*from"+TABLE_NAME+
  81. "Limit"+String.valueOf(PageSize)+"Offset"+String.valueOf(pageID*PageSize);
  82. Cursorrec=db.rawQuery(sql,null);
  83. setTitle("当前分页的数据总数:"+String.valueOf(rec.getCount()));
  84. //取得字段名称
  85. Stringtitle="";
  86. intcolCount=rec.getColumnCount();
  87. for(inti=0;i<colCount;i++)
  88. title=title+rec.getColumnName(i)+"";
  89. //列举出所有数据
  90. Stringcontent="";
  91. intrecCount=rec.getCount();
  92. for(inti=0;i<recCount;i++){//定位到一条数据
  93. rec.moveToPosition(i);
  94. for(intii=0;ii<colCount;ii++)//定位到一条数据中的每个字段
  95. {
  96. content=content+rec.getString(ii)+"";
  97. }
  98. content=content+"/r/n";
  99. }
  100. edtSQL.setText(title+"/r/n"+content);//显示出来
  101. rec.close();
  102. }
  103. /*
  104. *在内存创建数据库和数据表
  105. */
  106. voidCreateDB(){
  107. //在内存创建数据库
  108. db=SQLiteDatabase.create(null);
  109. Log.e("DBPath",db.getPath());
  110. Stringamount=String.valueOf(databaseList().length);
  111. Log.e("DBamount",amount);
  112. //创建数据表
  113. Stringsql="CREATETABLE"+TABLE_NAME+"("+ID
  114. +"textnotnull,"+NAME+"textnotnull"+");";
  115. try{
  116. db.execSQL("DROPTABLEIFEXISTS"+TABLE_NAME);
  117. db.execSQL(sql);
  118. }catch(SQLExceptione){}
  119. }
  120. /*
  121. *插入N条数据
  122. */
  123. voidInsertRecord(intn){
  124. inttotal=id+n;
  125. for(;id<total;id++){
  126. Stringsql="insertinto"+TABLE_NAME+"("+ID+","+NAME
  127. +")values('"+String.valueOf(id)+"','test');";
  128. try{
  129. db.execSQL(sql);
  130. }catch(SQLExceptione){
  131. }
  132. }
  133. }
  134. /*
  135. *插入之后刷新分页
  136. */
  137. voidRefreshPage()
  138. {
  139. Stringsql="selectcount(*)from"+TABLE_NAME;
  140. Cursorrec=db.rawQuery(sql,null);
  141. rec.moveToLast();
  142. longrecSize=rec.getLong(0);//取得总数
  143. rec.close();
  144. intpageNum=(int)(recSize/PageSize)+1;//取得分页数
  145. lstPageID.clear();
  146. for(inti=0;i<pageNum;i++){
  147. HashMap<String,String>map=newHashMap<String,String>();
  148. map.put("ItemText","No."+String.valueOf(i));
  149. lstPageID.add(map);
  150. }
  151. saPageID.notifyDataSetChanged();
  152. }
  153. }

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. android Intent机制详解
  4. Android(安卓)Intent的详细解析以及用法
  5. Android(安卓)ContentResolver详解
  6. Android(安卓)Design Demo 策略模式 TextView HTML封装 加下划线
  7. Android(安卓)的用户层 uevent处理机制
  8. Android使用SQlite数据库
  9. Android提交数据到服务器的两种方式四种方法

随机推荐

  1. Android配置文件权限一览表
  2. Android事件分发机制研究
  3. 超详细图文讲解android studio导入第三方
  4. 解决 “Error generating final archive:
  5. Android:sqlite3:not found
  6. 【转】anroid中建立sdcard
  7. android studio check for update 更新失
  8. Android异步线程与Bundle消息传递
  9. Android Studio 导出APK
  10. 自定义xml属性attr