91
Android 資資資資資 資資資資資資 資資資 資資資 2011/1

Android 資料庫處理

  • Upload
    valiant

  • View
    98

  • Download
    0

Embed Size (px)

DESCRIPTION

Android 資料庫處理. 建國科技大學 資管系 饒瑞佶 2011/1. Android 資料庫- SQLite. 資料庫 SQLite 檔案式資料庫 適合嵌入式系統,不需要額外安裝系統 OPEN SOURCE 資料庫 SQL 指令與一般 DBMS 大同小異,但有些微差異. SQLite 管理. adb shell + sqlite3. SQLite 操作- adb shell. 沒有 AVD 或是手機. 沒有連線的 device. SQLite 操作- adb shell. 先用 android list avd 看可以用的 AVD. - PowerPoint PPT Presentation

Citation preview

Page 2: Android 資料庫處理

Android 資料庫- SQLite

• 資料庫– SQLite– 檔案式資料庫– 適合嵌入式系統,不需要額外安裝系統– OPEN SOURCE 資料庫– SQL 指令與一般 DBMS 大同小異,但有些微差異

Page 3: Android 資料庫處理

SQLite 管理adb shell + sqlite3

Page 4: Android 資料庫處理

SQLite 操作- adb shell

• 沒有 AVD 或是手機

沒有連線的 device

Page 5: Android 資料庫處理

SQLite 操作- adb shell

• 先用 android list avd 看可以用的 AVD

可用的 AVD

Page 6: Android 資料庫處理

SQLite 操作- adb shell

• 再使用 emulator –avd <AVD 名稱 > 啟動一個AVD

• 例如: emulator –avd my_avd

啟動中的 my_avd

Page 7: Android 資料庫處理

SQLite 操作- adb shell• my_avd 模擬器

Page 8: Android 資料庫處理

SQLite 操作- adb shell

• 再次執行 adb shell ,就可以看到 # 提示符號# 提示符號

Page 9: Android 資料庫處理

SQLite 操作- adb shell

• 建議進入 sdcard 內建立目錄存放資料庫進入 sdcard

在 sdcard 內建立 mydb 目錄

進入 mydb 目錄

Page 10: Android 資料庫處理

SQLite 操作- adb shell• 請在 # 提示符號後輸入 sqlite3 < 資料庫名稱 >• 例如 sqlite3 db1• 如果資料庫不存在會自動建立,可以看到

sqlite> 提示符號

sqlite> 提示符號

Page 11: Android 資料庫處理

SQLite 操作- adb shell

• 離開是 .quit ( 注意有一個 .)• 現在可以開始操作 db1 資料庫• 使用 SQL 指令進行操作,包括:

建立欄位與資料表 ( 結構部分 )建立資料 ( 資料部分 )維護資料 ( 資料部分 )

Page 12: Android 資料庫處理

SQLite 結構相關 SQL 指令不分大小寫

Page 13: Android 資料庫處理

SQL 指令-結構操作• create table :建立資料表• 藍色是要填資料的部分,其它是固定語法• 所有 SQL 指令都要以 ; 結束

create table 資料表名稱 ( 欄位 1 資料型態 , 欄位 2 資料型態 , ….);

create table member( id char(20) not null, name char(20) not null, pwd char(20) not null, age int, PRIMARY KEY(id));

Page 14: Android 資料庫處理

SQL 指令-結構操作• .tables :可以查看目前資料庫內存在的資料表• .help :可以看 sqlite3 所有指令

沒有任何資料表

Page 15: Android 資料庫處理

SQL 指令-結構操作• create table :建立資料表

member 資料表已經被建立

Page 16: Android 資料庫處理

SQL 指令-結構操作• .schema :查看資料表結構

member 資料表結構

Page 17: Android 資料庫處理

SQL 指令-結構操作• alter table :更改資料表結構

alter table 資料表名稱 add 欄位 1 資料型態 ;

增加欄位

alter table 資料表名稱 rename to 新資料表名稱 ;

更改資料表名稱

• drop table :刪除資料表drop table 資料表名稱 ;

Page 18: Android 資料庫處理

SQLite 資料相關 SQL 指令

Page 19: Android 資料庫處理

SQL 指令-資料操作• insert :新增資料所有資料都要加上單引號

insert into 資料表名稱 ( 欄位 1, 欄位 2…) values (‘ 資料 1’,’ 資料 2’…);

已在 member 資料表中新增一筆資料

Page 20: Android 資料庫處理

SQL 指令-資料操作• Select :篩選資料

select 欄位 s from 資料表名稱 where 條件 ;

加入 where 條件

Page 21: Android 資料庫處理

SQL 指令-資料操作• update :更新資料

update 資料表名稱 set 欄位 =‘ 新值’… where 條件 ;

姓名改成大寶

Page 22: Android 資料庫處理

SQL 指令-資料操作• delete :刪除資料

Delete from 資料表名稱 where 條件 ;

先新增一筆資料

刪除剛新增的資料

Page 23: Android 資料庫處理

SQL 指令-結構操作• SQLite 沒有刪除欄位的指令,例如 ALTER TABLE DROP COLUMN

• 那怎麼辦?

Page 24: Android 資料庫處理

SQL 指令-結構操作新增一個備份資料庫 member_backup

將資料從 member 搬到 member_backup刪除 member

再新增一個 member ,此時不要含要刪掉的欄位再把資料從 member_backup 搬到member刪除 member_backup

完成

Page 25: Android 資料庫處理

更多 SQLite SQL 指令• http://www.newobjects.com/pages/ndl/

SQLite3/sql.htm#ALTER TABLE

Page 26: Android 資料庫處理

SQLite+Android

Page 27: Android 資料庫處理

SQLite+Android

• 建立新專案- HelloDB

Page 28: Android 資料庫處理

SQLite+Android• 首先,在主程式中加入 SQLiteOpenHelper 結構

資料庫第一次建立時會執行 onCreate

資料庫異動版本時會執行 onUpgrade

必要的 class

建立 db2.db 資料庫

Page 29: Android 資料庫處理

SQLite+Android• SQLiteOpenHelper 結構

// SQLite 資料庫使用必要 class ------------------------ String DATABASE_TABLE = "member"; String DATABASE_CREATE_TABLE = "create table " + DATABASE_TABLE + "(_id char(20), name, pwd, age, primary key(_id));"; class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, "/sdcard/db2.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE_TABLE); // 新增資料表 } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { }}

Page 30: Android 資料庫處理

SQLite+Android• 從 class HelloDB 呼叫 DatabaseHelper 建立資料庫 建立 DatabaseHelper 物件

呼叫建立 db2.db 資料庫

Page 31: Android 資料庫處理

SQLite+Android

• 資料庫被建立在 data/data/com.android.hellodb

Page 32: Android 資料庫處理

SQLite+Android• 資料庫被建立在 data/data/com.android.hellodb

Page 33: Android 資料庫處理

SQLite+Android• 有資料庫後,下一步要建立資料表與資料

新增資料

建立資料表

定義資料表結構

Page 34: Android 資料庫處理

String DATABASE_TABLE = "member"; String DATABASE_CREATE_TABLE = "create table " + DATABASE_TABLE + "(_id char(20), name, pwd, age, primary key(_id));";

class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, "/sdcard/db2.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE_TABLE); // 新增資料表 } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { }}

SQLite+Android• 建立資料表 schema

建立資料表結構

Page 35: Android 資料庫處理

SQLite+Android

• 為何會錯誤?

跟執行流程有關!

Page 36: Android 資料庫處理

SQLite+Android• 幾點注意!1. 下面這段只有在資料庫第一次被建立時才會進行,所以如果資料庫已經存在,那就不會建立資料表,當然也就不能寫入資料

上張投影片的錯誤就是因為我們已經先建立 db2.db ,因此再次執行程式時,並不會建立資料表,導致下面程式出錯

Page 37: Android 資料庫處理

SQLite+Android• 正常執行結果

資料庫

資料表資料

中文資料有亂碼

Page 38: Android 資料庫處理

SQLite+Android

• 最後,可以進行資料查詢再新增一筆資料 ( 小黑 )

SQL 查詢指令資料移動到第一筆

顯示資料

Page 39: Android 資料庫處理

SQLite+Android• 實際顯示沒有亂碼

Page 40: Android 資料庫處理

小練習• 使用上述課程 member 資料表• 設計一個登入畫面,可以讓使用者輸入帳號與密碼• 輸入完成後按下「確認」按鈕後,連回資料庫內的 member 資料表進行確認 (id 與

pwd 欄位 )• 輸入正確者跳至下一個頁面 ( 自訂 ) ,錯誤者請跳出訊息提示框

Page 41: Android 資料庫處理

透過 ListView 顯示資料庫資料

Page 42: Android 資料庫處理

透過 ListView 顯示資料整個版面都用 ListActivity

改成 ListActivity

刪除

Page 43: Android 資料庫處理

透過 ListView 顯示資料

陣列資料

ArrayAdapter

Page 44: Android 資料庫處理

透過 ListView 顯示資料陣列資料

private static final String[] mStrings = new String[] { " 大餅包小餅 ", " 蚵仔煎 ", " 東山鴨頭 ", " 臭豆腐 ", " 潤餅 ", " 豆花 ", " 青蛙下蛋 "," 豬血糕 ", " 大腸包小腸 ", " 鹹水雞 ", " 烤香腸 "," 車輪餅 "," 珍珠奶茶 "," 鹹酥雞 "," 大熱狗 ", " 炸雞排 "," 山豬肉 "," 花生冰 "," 剉冰 "," 水果冰 ", " 包心粉圓 "," 排骨酥 "," 沙茶魷魚 "," 章魚燒 "," 度小月 ", "aaa","abc","bbb","bcd","123" };

Page 45: Android 資料庫處理

透過 ListView 顯示資料

選了怎麼不會勾選 ?

Page 46: Android 資料庫處理

透過 ListView 顯示資料

ListView 勾選效果

加入:ListView lv = this.getListView();lv.setChoiceMode( ListView.CHOICE_MODE_SINGLE );

修改: android.R.layout.simple_list_item_checked

Page 47: Android 資料庫處理

透過 ListView 顯示資料

偵測選擇資料

Page 48: Android 資料庫處理

透過 ListView 顯示資資料

Page 49: Android 資料庫處理

透過 ListView 顯示資料• 假設是在 Activity 中除了 ListView之外,還要放入其他的組件時,這時候就需要在 Activity 中加入一個

ListView 物件,利用這個組件的setAdapter來連接 Adapter

Page 50: Android 資料庫處理

透過 ListView 顯示資料

ListView 物件的 id

main.xml 版面

Page 51: Android 資料庫處理

透過 ListView 顯示資料

陣列資料

不改

Page 52: Android 資料庫處理

透過 ListView 顯示資料

偵測選擇資料

Page 53: Android 資料庫處理

透過 ListView 顯示資料• ArrayAdapter 中有一個 android 定義好的內建 list樣式 -

“android.R.layout.simple_list_item_1” ,其它樣式如下所列:– android.R.layout.simple_list_item_1 :一行 text– android.R.layout.simple_list_item_2 :一行 text較大,一行 text較小– android.R.layout.simple_list_item_single_choice :單選– android.R.layout.simple_list_item_multiple_choice :多選按鈕– android.R.layout.simple_list_item_checked :勾選盒

• 第 1 個剛剛有用了,事實上第 3,4,5 個也是直接換上去就可以看到了。• 第 2 個 android.R.layout.simple_list_item_2 就比較麻煩,原因是 ArrayAdapter 並不支援傳入兩個字串參數值,所以要改用

SimpleAdapter ,而且傳入的數值型態要改為 ArrayList 才可以

Page 54: Android 資料庫處理

透過 ListView 顯示資料- SimpleAdapterArrayList

SimpleAdapter

Page 55: Android 資料庫處理

透過 ListView 顯示資料- SimpleAdapter

陣列資料

Page 56: Android 資料庫處理

透過 ListView 顯示資料- SimpleAdapter

陣列資料 private static final String[] mPlaces = new String[] { " 台北市 ", " 新北市 ", " 台南市 ", " 高雄市 ", " 苗粟縣 ", " 台北市 ", " 新北市 ", " 台南市 ", " 高雄市 ", " 苗粟縣 ", " 台北市 ", " 新北市 ", " 台南市 ", " 高雄市 ", " 苗粟縣 ", " 台北市 ", " 新北市 ", " 台南市 ", " 高雄市 ", " 苗粟縣 ", " 台北市 ", " 新北市 ", " 台南市 ", " 高雄市 ", " 苗粟縣 ", " 台北市 ", " 新北市 ", "789", "cde", "abc" }; private static final String[] mFoods = new String[] { " 大餅包小餅 ", " 蚵仔煎 ", " 東山鴨頭 ", " 臭豆腐 ", " 潤餅 ", " 豆花 ", " 青蛙下蛋 "," 豬血糕 ", " 大腸包小腸 ", " 鹹水雞 ", " 烤香腸 "," 車輪餅 "," 珍珠奶茶 "," 鹹酥雞 "," 大熱狗 ", " 炸雞排 "," 山豬肉 "," 花生冰 "," 剉冰 "," 水果冰 ", " 包心粉圓 "," 排骨酥 "," 沙茶魷魚 "," 章魚燒 "," 度小月 ", "aaa","abc","bbb","bcd","123" };

Page 57: Android 資料庫處理

透過 ListView 顯示資料- SimpleAdapter

Page 58: Android 資料庫處理

透過 ListView 顯示資料- SimpleAdapter

偵測選擇資料偵測選擇資料

Page 59: Android 資料庫處理

透過 ListView 顯示資料-自訂版面• 如果不要用 android 內建的

simple_list_item_2 ,改用自己定義的樣式,要怎麼作呢?像上面的範例,再加上一個評分的字串在地點的旁邊。• 首先先製作一個專用的 layout ,取名為

mylistview.xml。

Page 60: Android 資料庫處理

透過 ListView 顯示資料-自訂版面<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="fill_parent" android:orientation="vertical"> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:layout_marginTop="6dip" android:textAppearance="?android:attr/textAppearanceLarge"> </TextView> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal"> <TextView android:id=“@+id/textView2” android:text=“TextView” android:layout_height=“wrap_content” android:layout_width=“wrap_content” android:textAppearance=“?android:attr/textAppearanceSmall”> </TextView> <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_marginLeft="6dip"> </TextView> </LinearLayout></LinearLayout>

mylistview.xml

Page 61: Android 資料庫處理

透過 ListView 顯示資料-自訂版面

第二個 LinearLayout 要設定成 horizontal

Page 62: Android 資料庫處理

透過 ListView 顯示資料-自訂版面

三個改變的地方

Page 63: Android 資料庫處理

透過 ListView 顯示資料-自訂版面

private static final String[] mFoods = new String[] { " 大餅包小餅 ", " 蚵仔煎 ", " 東山鴨頭 ", " 臭豆腐 ", " 潤餅 ", " 豆花 ", " 青蛙下蛋 "," 豬血糕 ", " 大腸包小腸 ", " 鹹水雞 ", " 烤香腸 "," 車輪餅 "," 珍珠奶茶 "," 鹹酥雞 "," 大熱狗 ", " 炸雞排 "," 山豬肉 "," 花生冰 "," 剉冰 "," 水果冰 ", " 包心粉圓 "," 排骨酥 "," 沙茶魷魚 "," 章魚燒 "," 度小月 ", "aaa","abc","bbb","bcd","123" };

陣列資料

Page 64: Android 資料庫處理

透過 ListView 顯示資料-自訂版面

Page 65: Android 資料庫處理

透過 ListView 顯示資料-加入圖片• 最後加入圖 片到 ListView 吧• 圖片需要先放到 res/drawable-xxxx 目錄中(這裡放到 res/drawable-hdpi 中)• 因為 HashMap 的 value 部份需要用到圖片,是一個 int 的型態,所以 HashMap 的 value 部份需要改為 Object ,才能容得下 int 和 string的類型。• 修改 mylistview.xml ,加上圖片在標題的左邊

Page 66: Android 資料庫處理

透過 ListView 顯示資料-加入圖片

再加入 LinearLayout 與 ImageView

LinearLayout 要設定成 horizontal

Page 67: Android 資料庫處理

透過 ListView 顯示資料-加入圖片三個修改的地方

Page 68: Android 資料庫處理

透過 ListView 顯示資料-加入圖片

private static final int[] mPics=new int[]{ R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5 };

陣列資料

Page 69: Android 資料庫處理

透過 ListView 顯示資料-加入圖片陣列資料

Page 70: Android 資料庫處理

ListView + 資料庫

Page 71: Android 資料庫處理

透過 ListView 顯示資料庫資料原程式,用 Toast 顯示資料

用 ListView 取代這裡

Page 72: Android 資料庫處理

透過 ListView 顯示資料庫資料• 注意資料表內需要有個 _id 的主鍵欄位• 注意使用 ListView 顯示資料 , 需要修改 extends Activity變成 ListActivity• 需刪掉 setContentView(R.layout.main);

Page 73: Android 資料庫處理

透過 ListView 顯示資料庫資料 String[] from = new String[]{"name","pwd","_id"}; //一定要有 _id才行 int[] to = new int[]{android.R.id.text1};

Cursor c1 = db.query("member", from, null, null, null, null, "_id ASC");startManagingCursor(c1);// 多資料欄位ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,c1,new String[] {"name","pwd"},new int[] {android.R.id.text1, android.R.id.text2 }); setListAdapter(adapter);

Page 74: Android 資料庫處理

透過 ListView 顯示資料庫資料

Page 75: Android 資料庫處理

透過 ListView 顯示資料庫資料• 兩列字體大小不同

改這個屬性

Page 76: Android 資料庫處理

透過 ListView 顯示資料庫資料

Page 77: Android 資料庫處理

透過 ListView 顯示資料庫資料

偵測選擇資料直接顯示資料

Page 78: Android 資料庫處理

@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);// Get the item that was clicked// 直接顯示資料Cursor cursor = (Cursor)this.getListAdapter().getItem(position);String keyword =cursor.getString(cursor.getColumnIndex("_id"));Toast.makeText(this, " 您選擇了 : " + keyword, Toast.LENGTH_LONG).show();}

透過 ListView 顯示資料庫資料偵測選擇資料

Page 79: Android 資料庫處理

透過 ListView 顯示資料庫資料

傳送資料

透過另一頁 (DataDetails) 顯示資料注意要加入 <activity android:name="DataDetails"></activity>

Page 80: Android 資料庫處理

透過 ListView 顯示資料庫資料透過另一頁 (DataDetails) 顯示資料注意要加入 <activity android:name="DataDetails"></activity>

@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);// Get the item that was clicked//* 跳頁顯示資料 Intent intent = new Intent(this, Page2.class); Cursor cursor = (Cursor)this.getListAdapter().getItem(position); intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id"))); startActivity(intent); }

Page 81: Android 資料庫處理

透過 ListView 顯示資料庫資料

接收資料

DataDetails.java

Page 82: Android 資料庫處理

小練習• 在 DataDetails 上設計四個 TextView 物件• 透過傳進來的 EMPLOYEE_ID ,再到資料庫查詢詳細資料,將其顯示到上面四個

TextView 中

Page 83: Android 資料庫處理

連結外部 DB

Page 84: Android 資料庫處理

連結外部 DB• 不直接接觸 DB• 透過外部 php 或 ASP.NET 程式連結資料庫• Android 再呼叫外部 php 或 ASP.NET 程式• 可以傳遞參數

Page 85: Android 資料庫處理

連結外部 DB• WebLogin 專案

Page 86: Android 資料庫處理

連結外部 DB• WebLogin 專案

Page 87: Android 資料庫處理

透過WebService連結外部 DB

Page 88: Android 資料庫處理

連結 Web Service• 不直接接觸 DB• 透過Web Service 連結資料庫• Android 再呼叫外部 Web Service• 可以傳遞參數

Page 90: Android 資料庫處理

連結 Web Service• 使用 ksoap2-android– 下載後如果副檔名是 zip ,請更改成 jar– 請在 project 中加入本 jar

12

3

Page 91: Android 資料庫處理

連結 Web Service

• 注意 DNS問題–若執行出現 host is unresolved 的問題

• 解決方法adb shell #getprop << 查看屬性設定 [net.dns1]: [192.168.2.1] <<某一筆是 net.dns1 就是目前的設定 #setprop net.dns1 168.95.1.1 << 設定成可以用的 DNS