assetsフォルダにdatabase.dbがある場合
assetsフォルダのデータベースをdatabaseフォルダにコピーして使う
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class RopouActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String DB_PATH = "/data/data/com.yamato.ropou/databases/";
String DB_NAME = "database.db";
try {
InputStream myInput = getResources().getAssets()
.open("database.db");
//InputStream myInput = this.getResources().openRawResource(R.raw.database);//res/rawフォルダにデータベースがある場合
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
//データベースを開く
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 0);
Cursor c = db.query("myDatabaseTable", new String[] { "name", "age" },
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();
}
}
動かない
package com.android.word.net;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AndroidWordNetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager as = getResources().getAssets();
Log.d("タグ",as.toString() );
//ヘルパークラスのインスタンスを作成します。
MyDBHelper helper = new MyDBHelper(this,as.toString()+"database.db");
SQLiteDatabase db = helper.getWritableDatabase();
Cursor c = db.query("myDatabaseTable", new String[] { "name", "age" },
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歳\n", c.getString(0), c.getInt(1));
isEof = c.moveToNext();
}
textView1.setText(text);
c.close();
db.close();
}
public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(Context context, String string) {
super(context, string, null, 1);
}
@Override
//ここでデータベース作成(コンストラクタに渡されたDBファイル名が存在しない場合に呼ばれる)
public void onCreate(SQLiteDatabase db) {
// テーブルを作成
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO 自動生成されたメソッド・スタブ
}
}
}




















