当前位置:首页 > 编程开发

Android数据库相关代码解读(1)

webgou16年前 (2010-08-08)编程开发105

昨天进行了GUI界面设计,感受了一下android初次设计的愉悦,今天接着学习其SQLite数据库试用,将昨天的例子中数据存到数库中,并读取查看一下。 具体看代码(原写的有点问题,再改写如下):

1) Android数据库之库操作类:

  1. package com.topsun;   
  2. import android.content.Context;   
  3. import android.database.Cursor;   
  4. import android.database.sqlite.SQLiteDatabase;   
  5. import android.util.Log;   
  6. public class DBHelper {   
  7. private static final String TAG = "UserDB_DBHelper.java";   
  8. private static final String DataBaseName = "UserDB";   
  9. SQLiteDatabase db;   
  10. Context context;   
  11. public DBHelper(Context context) {   
  12. this.open(context);   
  13. }   
  14. private void createTabel() {   
  15. // TODO Auto-generated method stub   
  16. String sql = "";   
  17. try {   
  18. sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER 
    PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)"
    ;   
  19. this.db.execSQL(sql);   
  20. Log.v(TAG, "Create Table TestUser ok");   
  21. } catch (Exception e) {   
  22. Log.v(TAG, "Create Table TestUser fail");   
  23. } finally {   
  24. //this.db.close();   
  25. Log.v(TAG, "Create Table TestUser ");   
  26. }   
  27. }   
  28. public boolean save(String name, String sex, Integer ages) {   
  29. String sql = "insert into TestUser values
    (null,'"
     + name + "','" + sex   
  30. + "'," + ages + ")";   
  31. try {   
  32. this.db.execSQL(sql);   
  33. Log.v(TAG, "insert Table TestUser 1 record ok");   
  34. return true;   
  35. } catch (Exception e) {   
  36. Log.v(TAG, "insert Table TestUser 1 record fail");   
  37. return false;   
  38. } finally {   
  39. //this.db.close();   
  40. Log.v(TAG, "insert Table TestUser ");   
  41. }   
  42. }   
  43. public Cursor loadAll() {   
  44. Cursor cur = db.query("TestUser", new String[] 
    { "ID", "NAME","SEX","AGES"}, null,   
  45. null, null, null, null);   
  46. return cur;   
  47. }   
  48. public void open(Context context){   
  49. if (null == db || !this.db.isOpen()){   
  50. this.context = context;   
  51. this.db = context.openOrCreateDatabase(this.DataBaseName,   
  52. context.MODE_PRIVATE, null);   
  53. createTabel();   
  54. Log.v(this.TAG, "create or Open DataBase。。。");   
  55. }   
  56. }   
  57. public void close() {   
  58. db.close();   
  59. }   
  60. }   
  61. package com.topsun;  
  62. import android.content.Context;  
  63. import android.database.Cursor;  
  64. import android.database.sqlite.SQLiteDatabase;  
  65. import android.util.Log;  
  66. public class DBHelper {  
  67. private static final String TAG = "UserDB_DBHelper.java";  
  68. private static final String DataBaseName = "UserDB";  
  69. SQLiteDatabase db;  
  70. Context context;  
  71. public DBHelper(Context context) {  
  72. this.open(context);  
  73. }  
  74. private void createTabel() {  
  75. // TODO Auto-generated method stub  
  76. String sql = "";  
  77. try {  
  78. sql = "CREATE TABLE IF NOT EXISTS TestUser 
    (ID INTEGER PRIMARY KEY autoincrement, 
    NAME TEXT, SEX TEXT, AGES INTEGER)"
    ;  
  79. this.db.execSQL(sql);  
  80. Log.v(TAG, "Create Table TestUser ok");  
  81. } catch (Exception e) {  
  82. Log.v(TAG, "Create Table TestUser fail");  
  83. } finally {  
  84. //this.db.close();  
  85. Log.v(TAG, "Create Table TestUser ");  
  86. }  
  87. }  
  88. public boolean save(String name, String sex, Integer ages) {  
  89. String sql = "insert into TestUser values
    (null,'"
     + name + "','" + sex  
  90. + "'," + ages + ")";  
  91. try {  
  92. this.db.execSQL(sql);  
  93. Log.v(TAG, "insert Table TestUser 1 record ok");  
  94. return true;  
  95. } catch (Exception e) {  
  96. Log.v(TAG, "insert Table TestUser 1 record fail");  
  97. return false;  
  98. } finally {  
  99. //this.db.close();  
  100. Log.v(TAG, "insert Table TestUser ");  
  101. }  
  102. }  
  103. public Cursor loadAll() {  
  104. Cursor cur = db.query("TestUser", new String[] 
    { "ID", "NAME","SEX","AGES"}, null,  
  105. null, null, null, null);  
  106. return cur;  
  107. }  
  108. public void open(Context context){  
  109. if (null == db || !this.db.isOpen()){  
  110. this.context = context;  
  111. this.db = context.openOrCreateDatabase(this.DataBaseName,  
  112. context.MODE_PRIVATE, null);  
  113. createTabel();  
  114. Log.v(this.TAG, "create or Open DataBase。。。");  
  115. }  
  116. }  
  117. public void close() {  
  118. db.close();  
  119. }  
  120. }

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=245

标签: androidSDK
分享给朋友:

“Android数据库相关代码解读(1)” 的相关文章

CentOS7安装Docker与使用篇

 一、在CentOS7上安装docker篇1. 查看系统版本: ...…

sandy 摄像机

SandyCam Help.The ViewFinderThis is the camera view of the Sandy world.The ViewFinder has a crosshair, that can be turned on or off, using the check b…

总结:用OleObject嵌入flash ocx

用OleObject嵌入flash ocx总结 用了好几天的时间,从从未接触com到知道用oleobject嵌入 flash ocx的过程。应该要注意下面几点: 1.实现ole 接口 2.用#import 加载 flash ocx ...…

devenv.exe的命令行参数

devenv.exe的命令行参数…

黑莓C网烧号写号的原理

中国大陆:插卡 1. 传统的方式 Sim卡 插卡 电信将UIM卡 提交给你的时候,需要将UIM卡 插在一个设备上,然后将你号码在服务器上的相关信息写进 UIM 卡中 2. Sim 卡插进手机 手机读取 Sim卡上(服务器上的)信息,开机,与电信服务器交互(电话,短信,上网) 备注: 服务器的信息--…

Android手机WiFi调试,查看logcat

在项目开发过程中,遇到这样的场景:有写特殊Android设备只有一个USB口,当挂载U盘之后就无法连接USB实时进行调试了。这儿时候如果设备可以开启WiFi,那就可以用WiFi进行调试;要开启网络调试,执行下面指令即可:"setprop service.adb.t…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。