Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
嵌入式實驗操作手冊
AD-06 -資料庫的應用(一)
一、 實驗目的
在開發板上,透過 Android 系統,使用 Android 所提供的 Portable SQLite(可攜
式資料庫),設計一個簡單的資料庫來放置行事曆。
二、 實驗儀器
PAC Duo SOC 嵌入式開發板
軟體開發工具 Eclipse
三、 實驗原理
實作 onCreate( )與 onUpgrade( )來達到資料庫的新增與更動。
四、 實驗內容
預計功能:
可進行資料的新增(Insert)、修改(Update)和刪除(Delete)。
五、 實驗步驟
1. 如圖一所示,用電源線、RS-232(交叉線),將 PC 與 PAC-Duo SOC
做好連接。
嵌入式實驗操作手冊
圖一、PAC Duo SOC 安裝圖
2. 進入 Eclipse,進行程式碼編譯。
主程式 Test2.java
package test2.arnor.program;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Test2 extends Activity
{
private ToDoDB myToDoDB;
private Cursor myCursor;
private ListView myListView;
private EditText myEditText;
private int _id;
protected final static int MENU_ADD = Menu.FIRST;
嵌入式實驗操作手冊
protected final static int MENU_EDIT = Menu.FIRST + 1;
protected final static int MENU_DELETE = Menu.FIRST + 2;
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item); //提供選擇類別
switch (item.getItemId())
{
case MENU_ADD: //新增資料事件
this.addTodo();
break;
case MENU_EDIT: //編輯資料事件
this.editTodo();
break;
case MENU_DELETE: //刪除資料事件
this.deleteTodo();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
/* 新增三個 MENU */
menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton);
menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton);
menu.add(Menu.NONE, MENU_DELETE, 0,
R.string.strDeleteButton);
return true;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView = (ListView) this.findViewById(R.id.myListView);
myEditText = (EditText) this.findViewById(R.id.myEditText);
myToDoDB = new ToDoDB(this);
/* 取得 DataBase 裡的資料 */
myCursor = myToDoDB.select();
嵌入式實驗操作手冊
/* new SimpleCursorAdapter 並將 myCursor 傳入,顯示資料的欄位
為 todo_text */
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list,
myCursor, new String[]
{ ToDoDB.FIELD_TEXT }, new int[]
{ R.id.listTextView1 });
myListView.setAdapter(adapter);
/* 將 myListView 加入 OnItemClickListener */
myListView.setOnItemClickListener(new
AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int
arg2,
long arg3)
{
/* 將 myCursor 移到所點選的值 */
myCursor.moveToPosition(arg2);
/* 取得欄位_id 的值 */
_id = myCursor.getInt(0);
/* 取得欄位 todo_text 的值 */
myEditText.setText(myCursor.getString(1));
}
});
myListView
.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2,
long arg3)
{
/* getSelectedItem 所取得的是 SQLiteCursor */
SQLiteCursor sc = (SQLiteCursor) arg0.getSelectedItem();
_id = sc.getInt(0);
myEditText.setText(sc.getString(1));
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
嵌入式實驗操作手冊
});
}
private void addTodo()
{
if (myEditText.getText().toString().equals(""))
return;
/* 新增資料到資料庫 */
myToDoDB.insert(myEditText.getText().toString());
/* 重新查詢 */
myCursor.requery();
/* 重新整理 myListView */
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
private void editTodo()
{
if (myEditText.getText().toString().equals(""))
return;
/* 修改資料 */
myToDoDB.update(_id, myEditText.getText().toString());
myCursor.requery();
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
private void deleteTodo()
{
if (_id == 0)
return;
/* 刪除資料 */
myToDoDB.delete(_id);
myCursor.requery();
myListView.invalidateViews();
myEditText.setText("");
_id = 0;
}
}
資料庫程式 ToDoDB.java
package test2.arnor.program;
import android.content.ContentValues;
嵌入式實驗操作手冊
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ToDoDB extends SQLiteOpenHelper
{
private final static String DATABASE_NAME = "todo_db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "todo_table";
public final static String FIELD_id = "_id";
public final static String FIELD_TEXT = "todo_text";
public ToDoDB(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
/* 建立 table */
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
FIELD_id
+ " INTEGER primary key autoincrement, " + " " +
FIELD_TEXT + " text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion)
{
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null,
null);
return cursor;
}
public long insert(String text)
{
SQLiteDatabase db = this.getWritableDatabase();
嵌入式實驗操作手冊
/* 將新增的值放入 ContentValues */
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
long row = db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id, String text)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue =
{ Integer.toString(id) };
/* 將修改的值放入 ContentValues */
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@drawable/black"
>
嵌入式實驗操作手冊
</TextView>
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/black"
>
</EditText>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/red"
>
</ListView>
</LinearLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckedTextView android:id="@+id/listTextView1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/black"
>
</CheckedTextView>
</LinearLayout>
Strings.xml
嵌入式實驗操作手冊
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">按MENU可新增、修改、刪除</string>
<string name="app_name">Test2</string>
<string name="strEditButton">修改</string>
<string name="strMenu1">Delete</string>
<string name="strAddButton">新增</string>
<string name="strDeleteButton">刪除</string>
</resources>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="black">#000000</drawable>
<drawable name="white">#FFFFFFFF</drawable>
<drawable name="red">#FF0000</drawable>
</resources>
AndroidManifest.xmll
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test2.arnor.program"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Test2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
嵌入式實驗操作手冊
</manifest>
3. 開啟終端機程式,即 Tera Term。
(XP 作業環境下請選擇超級終端機)
設定如下:
Baud rate:115200
Data:8 bit
Parity:none
Stop:1 bit
Flow control:none
4. 將 USB 裝置連接至開發板,如圖二所示。
圖二、將 USB 裝置連結至開發板
5. 輸入掛載指令,如圖三、圖四、圖五所示:
busybox mknod /dev/sda1 b 8 1 (先建立一個掛載點)。
busybox mount –t vfat /dev/sda1 /mnt (參數 –t 是 usb 所要掛載的檔案
類型,將/dev 底下的 sda1 掛載到/mnt 底下)
嵌入式實驗操作手冊
圖三、輸入掛載指令
利用 cd、ls 指令查看程式所在路徑
圖四、查詢程式所在路徑
嵌入式實驗操作手冊
利用 cp 指令將執行檔複製至根目錄/data/app 資料夾下
圖五、將執行檔複製至根目錄/data/app 資料夾下
6. 操作開發板執行程式,畫面如圖六、圖七、圖八所示。
嵌入式實驗操作手冊
圖六、Test2.apk 程式執行畫面
圖七、新增行事曆資料
圖八、資料的新增、修改與刪除功能
嵌入式實驗操作手冊
備註:
模擬器設定方式請參考先前的教學文件。
由於 1.5 版的 compiler 對 method 的 override 支援上有問題,
故請先將 compiler 的改為 1.6 版。
六、 問題與思考題
1. 請將程式碼匯入 eclipse 中,用 Android 模擬器 compiler 完成並執行。
2. 請將程式 porting 到 PAC-Duo EVS 上,並正常執行。
3. 請完成行事曆內容的資料建置,並測試新增、刪除與修改功能。
4. Busybox 是什麼?