データベースが簡単に利用できる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();
}
}
続きを読む…»