TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

[Android智慧型手機設計_Ch8_2_SQLite瀏覽功能]

2013-7-10 19:54| 發佈者: woff| 查看: 3433| 評論: 0|原作者: woff

摘要: 存取SQLite資料庫的說明如下: SQLiteDatabase類別提供2種查詢功能的方法 1.rawQuery():僅接受單純的SQL查詢語法 rawQuery(String sql, String selectionArgs) 2.query():將SQL查詢語法依照query()的參數來切割 q ...
 

1.jpg


存取SQLite資料庫的說明如下:



SQLiteDatabase類別提供2種查詢功能的方法


1.rawQuery():僅接受單純的SQL查詢語法


rawQuery(String sql, String[] selectionArgs)


2.query():將SQL查詢語法依照query()的參數來切割
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)


1.設計Ch8_2_BrowseData.java
  1. package com.example.ch8_2_browsedata;

  2. import java.util.ArrayList;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;

  11. public class Ch8_2_BrowseData extends Activity {
  12. EditText et1,et2,et3,et4;
  13. Button btn1,btn2;
  14. TextView tv2;
  15. DBHP dbHlp;
  16. ArrayList sites;
  17. int index=0;
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. connectDB();
  22. findViews();
  23. showSites(index);
  24. }
  25. private void connectDB() {
  26. if(dbHlp == null)
  27. dbHlp = new DBHP(this);
  28. dbHlp.fillDB();
  29. sites=dbHlp.getAllSites();
  30. }
  31. void findViews(){
  32. et1=(EditText)findViewById(R.id.editText1);
  33. et2=(EditText)findViewById(R.id.editText2);
  34. et3=(EditText)findViewById(R.id.editText3);
  35. et4=(EditText)findViewById(R.id.editText4);
  36. btn1=(Button)findViewById(R.id.button1);
  37. btn2=(Button)findViewById(R.id.button2);
  38. tv2=(TextView)findViewById(R.id.textView2);
  39. btn1.setOnClickListener(new OnClickListener(){

  40. @Override
  41. public void onClick(View v) {
  42. index=index+1;
  43. if(index>=sites.size())
  44. index=0;
  45. showSites(index);
  46. }} );
  47. btn2.setOnClickListener(new OnClickListener(){

  48. @Override
  49. public void onClick(View v) {
  50. index=index-1;
  51. if(index<0)
  52. index=sites.size()-1;
  53. showSites(index);
  54. }} );
  55. }
  56. void showSites(int index){
  57. if(sites.size()>0){
  58. tv2.setText((index+1)+"/"+sites.size()+getString(R.string.count));
  59. et1.setText(sites.get(index).getId());
  60. et2.setText(sites.get(index).getName());
  61. et3.setText(sites.get(index).getPhoneNo());
  62. et4.setText(sites.get(index).getAddress());
  63. }else{
  64. tv2.setText("0/0"+getString(R.string.count));
  65. et1.setText("");
  66. et2.setText("");
  67. et3.setText("");
  68. et4.setText("");
  69. }
  70. }
  71. }
復制代碼



2.設計DBHP.java
  1. package com.example.ch8_2_browsedata;

  2. import java.util.ArrayList;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  8. import android.database.sqlite.SQLiteOpenHelper;

  9. public class DBHP extends SQLiteOpenHelper {
  10. private static final String DATABASE_NAME = "sites";
  11. private static final int DATABASE_VERSION = 1;
  12. private static final String TABLE_NAME = "sitesInfo";
  13. private static final String TABLE_CREATE =
  14. "CREATE TABLE " + TABLE_NAME + " (id TEXT NOT NULL,name TEXT NOT NULL,phoneNo TEXT, address TEXT, PRIMARY KEY (id)); ";
  15. public DBHP(Context context) {
  16. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  17. }
  18. @Override
  19. public void onCreate(SQLiteDatabase db) {
  20. db.execSQL(TABLE_CREATE);
  21. }
  22. @Override
  23. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  24. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  25. onCreate(db);
  26. }
  27. public void fillDB() {
  28. SQLiteDatabase db = getWritableDatabase();
  29. ContentValues[] values = new ContentValues[3];
  30. for(int i=0; i
  31. values[i] = new ContentValues();

  32. values[0].put("id", "yangmingshan");
  33. values[0].put("name", "陽明山國家公園管理處");
  34. values[0].put("phoneNo", "02-28613601");
  35. values[0].put("address", "台北市北投區竹子湖路1之20號");

  36. values[1].put("id", "yushan");
  37. values[1].put("name", "玉山國家公園管理處");
  38. values[1].put("phoneNo", "049-2773121");
  39. values[1].put("address", "南投縣水里鄉中山路一段300號");

  40. values[2].put("id", "taroko");
  41. values[2].put("name", "太魯閣國家公園管理處");
  42. values[2].put("phoneNo", "03-8621100");
  43. values[2].put("address", "花蓮縣秀林鄉258號");

  44. for(ContentValues row : values){
  45. db.insert(TABLE_NAME, null, row);
  46. }
  47. db.close();
  48. }
  49. public ArrayList getAllSites()
  50. {
  51. SQLiteDatabase db = getReadableDatabase();
  52. String columns[]={"id","name","phoneNo","address"};
  53. Cursor cursor =db.query(TABLE_NAME, columns, null, null, null, null, null);
  54. ArrayList sites = new ArrayList();
  55. while(cursor.moveToNext()){
  56. String id=cursor.getString(0);
  57. String name=cursor.getString(1);
  58. String phoneNo=cursor.getString(2);
  59. String address=cursor.getString(3);
  60. Site site=new Site(id,name,phoneNo,address);
  61. sites.add(site);
  62. }
  63. cursor.close();
  64. db.close();
  65. return sites;
  66. }
  67. }
復制代碼



3.設計Site.java
  1. package com.example.ch8_2_browsedata;

  2. public class Site {
  3. String id,name,phoneNo,address;
  4. Site(String id,String name,String phoneNo,String address)
  5. {
  6. this.id=id;
  7. this.name=name;
  8. this.phoneNo=phoneNo;
  9. this.address=address;
  10. }
  11. String getId(){
  12. return id;
  13. }
  14. String getName(){
  15. return name;
  16. }
  17. String getPhoneNo(){
  18. return phoneNo;
  19. }
  20. String getAddress(){
  21. return address;
  22. }
  23. }
復制代碼


最新評論



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-10-5 07:51 , Processed in 0.106458 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部