Androidでは、アプリケーション毎に専用のディレクトリが設けられています。
アプリケーション毎に「/data/data/パッケージ名」となっていて、
更にそれ以下に以下のように領域が分かれています。
| databases | データベースの保存 |
|---|---|
| shared_prefs | 設定ファイルの保存 |
| files | ファイルの保存 |
今回はファイルの入出力なので、「files」ディレクトリを利用します。
アクセスする際はファイルまでのパスは不要です。
データベースが簡単に利用できるSQLiteOpenHelperクラスを使用します。
プラットフォームは1.6で作成。
【HelloSqliteActivity.java】
package android.sample.sqlite;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class HelloSqliteActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySqlite helper = new MySqlite(this);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query("TABLE_NAME",
new String[] { "number", "latitude", "longitude" },
null, null, null, null, null);
startManagingCursor(c); //リソースの扱いをActivityに委ねます
boolean isEof = c.moveToFirst();
TextView textView1 = (TextView)findViewById(R.id.textView1);
String text="";
while (isEof) {
text += String.format("物件番号 : %s\r\n緯度 : %.6f\r\n経度 : %.6f\r\n\r\n",
c.getString(0), c.getDouble(1), c.getDouble(2));
isEof = c.moveToNext();
}
textView1.setText(text);
c.close();
db.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}