この記事ではSQLiteのデータベースファイルはファイルに保存されません。アプリケーションを終了するとデータベースに蓄積したレコードはなくなります。データベースファイルをファイルに保存するコードについてはこちらの記事を参照してください。
package com.iPentec.simplesqlite;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDBHelper helper = new MyDBHelper(this);
SQLiteDatabase db = helper.getReadableDatabase();
// データ挿入
db.execSQL("insert into products(name,price) values ('Pig', 250);");
db.execSQL("insert into products(name,price) values ('Penguin', 120);");
db.execSQL("insert into products(name,price) values ('Camel', 520);");
db.execSQL("insert into products(name,price) values ('Hawk', 60);");
// データ取り出し
String text = "";
Cursor c = db.query("products", new String[] { "name", "price" }, null,
null, null, null, null);
boolean isEof = c.moveToFirst();
while (isEof) {
text += String.format("%s : %d円\r\n", c.getString(0), c.getInt(1));
isEof = c.moveToNext();
}
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.iPentec.simplesqlite;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(Context context) {
super(context, null, null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// table create
db.execSQL("create table products(" + " name text not null,"
+ " price text" + ");");
}
}
package iPentec.SimpleSqlite;
import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
public class SimpleSQLite extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyDBHelper helper = new MyDBHelper(this);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query("products", new String[] { "name", "price" },
null, null, null, null, null);
boolean isEof = c.moveToFirst();
TextView textView1 = (TextView)findViewById(R.id.textView1);
String text="";
while (isEof) {
text += String.format("%s : %d円\r\n", c.getString(0), c.getInt(1));
isEof = c.moveToNext();
//layout.addView(tv);
}
textView1.setText(text);
c.close();
db.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(Context context) {
super(context, null, null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// table create
db.execSQL(
"create table products("+
" name text not null,"+
" price text"+
");"
);
// table row insert
db.execSQL("insert into products(name,price) values ('Cookie', 120);");
db.execSQL("insert into products(name,price) values ('Candy', 85);");
db.execSQL("insert into products(name,price) values ('Cake', 285);");
}
}
}
db.execSQL(
"create table products("+
" name text not null,"+
" price text"+
");"
);
db.execSQL("insert into products(name,price) values ('Cookie', 120);");
db.execSQL("insert into products(name,price) values ('Candy', 85);");
db.execSQL("insert into products(name,price) values ('Cake', 285);");
MyDBHelper helper = new MyDBHelper(this);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query("products", new String[] { "name", "price" }, null, null, null, null, null);
boolean isEof = c.moveToFirst();
TextView textView1 = (TextView)findViewById(R.id.textView1);
String text="";
while (isEof) {
text += String.format("%s : %d円\r\n", c.getString(0), c.getInt(1));
isEof = c.moveToNext();
}
textView1.setText(text);
c.close();
db.close();
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>