成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android數(shù)據(jù)庫(kù)工作方式解析

移動(dòng)開(kāi)發(fā) Android
Android數(shù)據(jù)庫(kù)在實(shí)際應(yīng)用中占據(jù)著重要的角色。我們會(huì)通過(guò)本文給出的一段代碼示例來(lái)對(duì)此進(jìn)行詳細(xì)的解讀,方便大家學(xué)習(xí)。

在手機(jī)系統(tǒng)領(lǐng)域中,谷歌的Android操作系統(tǒng)算是一個(gè)新起之秀。但是其優(yōu)秀的性能以及開(kāi)源性,使其一經(jīng)推出就伸手廣大用戶(hù)的好評(píng)。在這里我們可以從Android數(shù)據(jù)庫(kù)的相關(guān)操作來(lái)體驗(yàn)這一系統(tǒng)給我們帶來(lái)的好處。#t#

一個(gè)好的習(xí)慣是創(chuàng)建一個(gè)輔助類(lèi)來(lái)簡(jiǎn)化你的數(shù)據(jù)庫(kù)交互。

考慮創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)適配器,來(lái)添加一個(gè)與數(shù)據(jù)庫(kù)交互的包裝層。它應(yīng)該提供直觀(guān)的、強(qiáng)類(lèi)型的方法,如添加、刪除和更新項(xiàng)目。數(shù)據(jù)庫(kù)適配器還應(yīng)該處理查詢(xún)和對(duì)創(chuàng)建、打開(kāi)和關(guān)閉數(shù)據(jù)庫(kù)的包裝。

它還常用靜態(tài)的Android數(shù)據(jù)庫(kù)常量來(lái)定義表的名字、列的名字和列的索引。

下面的代碼片段顯示了一個(gè)標(biāo)準(zhǔn)數(shù)據(jù)庫(kù)適配器類(lèi)的框架。它包括一個(gè)SQLiteOpenHelper類(lèi)的擴(kuò)展類(lèi),用于簡(jiǎn)化打開(kāi)、創(chuàng)建和更新Android數(shù)據(jù)庫(kù)。

  1. import android.content.Context;  
  2. import android.database.*;  
  3. import android.database.sqlite.*;  
  4. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  5. import android.util.Log;  
  6. public class MyDBAdapter   
  7. {  
  8. private static final String DATABASE_NAME = “myDatabase.db”;  
  9. private static final String DATABASE_TABLE = “mainTable”;  
  10. private static final int DATABASE_VERSION = 1;  
  11. // The index (key) column name for use in where clauses.  
  12. public static final String KEY_ID=”_id”;  
  13. // The name and column index of each column in your database.  
  14. public static final String KEY_NAME=”name”;  
  15. public static final int NAME_COLUMN = 1;  
  16. // TODO: Create public field for each column in your table.  
  17. // SQL Statement to create a new database.  
  18. private static final String DATABASE_CREATE = “create table “ +  
  19. DATABASE_TABLE + “ (“ + KEY_ID + “ integer primary key autoincrement, “ +  
  20. KEY_NAME + “ text not null);”;  
  21. // Variable to hold the database instance  
  22. private SQLiteDatabase db;  
  23. // Context of the application using the database.  
  24. private final Context context;  
  25. // Database open/upgrade helpe  
  26. private myDbHelper dbHelper;  
  27. public MyDBAdapter(Context _context) {  
  28. context = _context;  
  29. dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);  
  30. }  
  31. public MyDBAdapter open() throws SQLException {  
  32. db = dbHelper.getWritableDatabase();  
  33. return this;  
  34. }  
  35. public void close() {  
  36. db.close();  
  37. }  
  38. public long insertEntry(MyObject _myObject) {  
  39. ContentValues contentValues = new ContentValues();  
  40. // TODO fill in ContentValues to represent the new row  
  41. return db.insert(DATABASE_TABLE, null, contentValues);  
  42. }  
  43. public boolean removeEntry(long _rowIndex) {  
  44. return db.delete(DATABASE_TABLE, KEY_ID + “=” + _rowIndex, null) > 0;  
  45. }  
  46. public Cursor getAllEntries () {  
  47. return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME},  
  48. null, null, null, null, null);  
  49. }  
  50. public MyObject getEntry(long _rowIndex) {  
  51. MyObject objectInstance = new MyObject();  
  52. // TODO Return a cursor to a row from the database and  
  53. // use the values to populate an instance of MyObject  
  54. return objectInstance;  
  55. }  
  56. public int updateEntry(long _rowIndex, MyObject _myObject) {  
  57. String where = KEY_ID + “=” + _rowIndex;  
  58. ContentValues contentValues = new ContentValues();  
  59. // TODO fill in the ContentValue based on the new object  
  60. return db.update(DATABASE_TABLE, contentValues, where, null);  
  61. }  
  62. private static class myDbHelper extends SQLiteOpenHelper   
  63. {  
  64. public myDbHelper(Context context, String name, CursorFactory factory, 
    int version) {  
  65. super(context, name, factory, version);  
  66. }  
  67. // Called when no database exists in  
  68. // disk and the helper class needs  
  69. // to create a new one.  
  70. @Override  
  71. public void onCreate(SQLiteDatabase _db) {  
  72. _db.execSQL(DATABASE_CREATE);  
  73. }  
  74. // Called when there is a database version mismatch meaning that  
  75. // the version of the database on disk needs to be upgraded to  
  76. // the current version.  
  77. @Override  
  78. public void onUpgrade(SQLiteDatabase _db, int _oldVersion, 
    int _newVersion) {  
  79. // Log the version upgrade.  
  80. Log.w(“TaskDBAdapter”, “Upgrading from version “ +  
  81. _oldVersion + “ to “ + _newVersion +  
  82. “, which will destroy all old data”);  
  83. // Upgrade the existing database to conform to the new version.  
  84. // Multiple previous versions can be handled by comparing  
  85. // _oldVersion and _newVersion values.  
  86. // The simplest case is to drop the old table and create a  
  87. // new one.  
  88. _db.execSQL(“DROP TABLE IF EXISTS “ + DATABASE_TABLE);  
  89. // Create a new one.  
  90. onCreate(_db);  
  91. }  
  92. }  

Android數(shù)據(jù)庫(kù)的相關(guān)操作就為大家介紹到這里。

責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-08-06 10:41:59

Flex命名空間

2009-09-28 13:39:01

Hibernate工作

2009-07-14 12:47:07

WebWork工作方式

2009-06-22 16:42:26

JSF的工作方式

2009-07-10 13:55:48

Swing控件

2010-07-22 09:01:02

SQL Server鏡

2009-08-13 18:36:29

C#數(shù)組工作方式

2011-04-19 10:23:00

路由器網(wǎng)橋

2011-04-19 10:25:44

路由算法路由器

2011-04-19 10:29:57

路由器路由網(wǎng)關(guān)

2016-10-27 17:49:07

群暉群暉科技NAS

2009-02-25 10:52:00

路由器原理工作方式協(xié)議

2023-01-04 07:39:39

2011-07-14 13:20:49

Servlet過(guò)濾器

2011-11-04 09:52:24

Siri云計(jì)算蘋(píng)果

2011-11-04 09:45:43

Siri

2021-12-07 10:18:06

首席信息官技術(shù)發(fā)展企業(yè)管理者

2010-03-25 17:55:12

CentOS界面

2013-03-01 10:23:17

2012-11-27 09:12:39

思杰移動(dòng)云計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 在线免费激情视频 | 久久精品中文 | 国产精品污www一区二区三区 | 久久国产精品99久久久大便 | 精品一区二区三区在线观看国产 | 天天影视网天天综合色在线播放 | aa级毛片毛片免费观看久 | 国产在线观看一区二区 | 综合一区二区三区 | 日韩精品在线免费观看 | 久久久久国产精品午夜一区 | 精品国产91 | 中文字幕在线播放第一页 | 国产精品v| 在线观看免费av网站 | 一级全黄少妇性色生活免费看 | 成人精品毛片国产亚洲av十九禁 | 视频一区二区中文字幕 | 久久成人免费观看 | 欧美中文字幕一区二区三区亚洲 | 天天人人精品 | 亚洲免费网 | 91视频18 | 狠狠操天天干 | 国产伊人久久久 | www.久| 久久性 | 日韩中文一区二区三区 | 国产成人精品一区二区三区网站观看 | 伊人网一区| 久久精品亚洲 | 日韩av成人在线 | 亚洲欧美国产精品一区二区 | 国产有码 | 中文字幕乱码亚洲精品一区 | 免费一二区 | 国产视频久久久久 | 久久草在线视频 | h肉视频| 久久999| 九九九久久国产免费 |