
จากบทความเรื่อง การใช้งาน Navicat for SQLite เบื้องต้น
เราได้สร้างไฟล์ฐานข้อมูล SQLite ที่ชื่อ MyDB.db บนเครื่องPC
ด้วยโปรแกรม Navicat for SQLite และได้สร้างตาราง
ชื่อ person แล้วนั้น
บทความนี้ จะอธิบายวิธีการนำฐานข้อมูลดังกล่าว
เข้าไปใช้เป็นฐานข้อมูลบนเครื่อง Android
โดยที่เราไม่จำเป็นสร้าง database ใหม่บน Android
โดยมีขั้นตอนดังนี้
เราได้สร้างไฟล์ฐานข้อมูล SQLite ที่ชื่อ MyDB.db บนเครื่องPC
ด้วยโปรแกรม Navicat for SQLite และได้สร้างตาราง
ชื่อ person แล้วนั้น
บทความนี้ จะอธิบายวิธีการนำฐานข้อมูลดังกล่าว
เข้าไปใช้เป็นฐานข้อมูลบนเครื่อง Android
โดยที่เราไม่จำเป็นสร้าง database ใหม่บน Android
โดยมีขั้นตอนดังนี้
1) ใส่ข้อมูลในตาราง person ตามรูป แล้ว Copy ไฟล์ MyDB.db ไปไว้ที่ SD-Card


2)เปิด Eclipse ไปที่ File – New – Android Project


3)ไฟล์ AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Main"
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>
|
4)ไฟล์ res/layout/main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Use Existing DB" />
<Button
android:id="@+id/btn_import"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Import DB" />
<Button
android:id="@+id/btn_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
|
5)ไฟล์ Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package com.Example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener{
ProgressDialog pDialog;
SQLiteDatabase db= null;
InputStream src = null;
OutputStream desc = null;
Button btn_import,btn_select;
TextView txt_result;
public void initWidget(){
btn_import = (Button)findViewById(R.id.btn_import);
btn_import.setOnClickListener(this);
btn_select = (Button)findViewById(R.id.btn_select);
btn_select.setOnClickListener(this);
txt_result = (TextView)findViewById(R.id.txt_result);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.initWidget();
//สร้างไฟล์ temp.db เพื่อให้ android สร้างไดเรคทอรี database
db = this.openOrCreateDatabase("temp.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.close();
}//////// end onCreate /////////////////////
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.btn_import:
String sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "MyDB.db";
String sdFile = sdDir + File.separator + fileName;
InputStream src = null;
OutputStream desc = null;
String mPackage = getApplicationContext().getPackageName();
try {
Log.d("CopyFile", "Start Import..");
src = new FileInputStream(sdFile);
desc = new FileOutputStream("/data/data/"+mPackage+"/databases/MyDB.db");
copyFile(src,desc);
src.close();
src= null;
desc.flush();
desc.close();
desc= null;
Log.d("CopyFile", "End Import..");
} catch(Exception e) {
Log.d("CopyFile", "Error: "+e.getMessage());
}
break;
case R.id.btn_select:
db = this.openOrCreateDatabase("MyDB.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
txt_result.setText("");
SQLiteCursor cur = (SQLiteCursor)db.rawQuery("select * from person",null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
txt_result.append("id :"+cur.getInt(0)+" ชื่อ :"+cur.getString(1)+" อายุ :"+cur.getInt(2)+"\n");
cur.moveToNext();
}
cur.close();
break;
}
}
//copyFile Method
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}//End copyFile Method
@Override
public void onDestroy(){
super.onDestroy();
if(db.isOpen())db.close();
Log.d(this.getClass().getSimpleName(),"onDestroy");
}
}//////////////// end Main Class ////////////////////
|
ผลการรัน ให้สังเกตุที่ LogCat


จะเห็นได้ว่า หากเราสร้างไฟล์ Database บน PC ไว้ก่อน
ทำให้เราไม่ต้องเขียน code เพื่อสร้าง database บน Android
ซึ่งช่วยลดเวลาในการพัฒนาโปรแกรมได้อีกทางหนึ่ง และหากท่านต้องการส่งออก
ฐานข้อมูล SQLite จากตัวเครื่อง Android มาไว้ที่ SD-Card เพื่อนำไปใช้บนเครื่อง PC
ท่านสามารถใช้ Method ที่ชื่อว่า copyFile
โดย Copy จาก /data/data/”+mPackage+”/databases/MyDB.db
ไปไว้ที่ พาธ ของ SD-card ได้เช่นเดียวกัน
ทำให้เราไม่ต้องเขียน code เพื่อสร้าง database บน Android
ซึ่งช่วยลดเวลาในการพัฒนาโปรแกรมได้อีกทางหนึ่ง และหากท่านต้องการส่งออก
ฐานข้อมูล SQLite จากตัวเครื่อง Android มาไว้ที่ SD-Card เพื่อนำไปใช้บนเครื่อง PC
ท่านสามารถใช้ Method ที่ชื่อว่า copyFile
โดย Copy จาก /data/data/”+mPackage+”/databases/MyDB.db
ไปไว้ที่ พาธ ของ SD-card ได้เช่นเดียวกัน
Credit : http://android4health.wordpress.com/2012/07/06/sqlite_pc_android/