私のアプリは、実行した瞬間にデータベースを作成し、そこにいくつかの基本データを保存します。ただし、デバイスでアプリを実行しようとすると、「データベースを開けません」というエラーが表示されますが、エミュレーターで動作していて完全に機能しています。
私の主な活動は
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity
{
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("sarhad","Initializing data");
DatabaseHelper db = new DatabaseHelper(this);
Cursor cursor;
ListView listSongs;
SimpleCursorAdapter adapter;
final String[] FROM = { db.KEY_SONGNAME, db.KEY_ARTISTNAME, db.KEY_LABELNAME };
final int[] TO = { R.id.textViewSONG, R.id.textViewARTIST, R.id.textViewLABEL };
try
{
db.openDataBase();
} catch (SQLException sqle)
{
throw sqle;
}
listSongs = (ListView) findViewById(R.id.listSongsView);
db.getWritableDatabase();
Log.i("tag","Initializing query");
String queryStr = "SELECT * FROM songs";
Log.i("tag","before : cursor = db.getSongs(queryStr);");
cursor = db.getSongs(queryStr);
Log.i("tag","after : cursor = db.getSongs(queryStr);");
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM,TO);
listSongs.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
私のデータベースヘルパークラスは
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper
{
static final int DATABASE_VERSION = 1;
public static String DB_PATH = "";
// Database Name
static final String DATABASE_NAME = "songsDB";
// Contacts table name
static final String TABLE_SONGS = "songs";
// Contacts Table Columns names
static final String KEY_ID = "_id";
static final String KEY_SONGNAME = "songname";
static final String KEY_ARTISTNAME = "artistname";
static final String KEY_LABELNAME = "labelname";
private SQLiteDatabase db = null;
private final Context myContext = null;
private static DatabaseHelper mDBConnection;
//////////////////////////////////////////////////////////////////////////////////
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
}
//////////////////////////////////////////////////////////////////////////////////
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_SONGS_TABLE = "CREATE TABLE " + TABLE_SONGS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SONGNAME
+ " TEXT," + KEY_ARTISTNAME + " TEXT," + KEY_LABELNAME
+ " TEXT);";
db.execSQL(CREATE_SONGS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
onCreate(db);
}
//////////////////////////////////////////////////////////////////////////////////
void addSong(String song, String singer, String label)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SONGNAME, song);
values.put(KEY_ARTISTNAME, singer);
values.put(KEY_LABELNAME, label);
db.insert(TABLE_SONGS, null, values);
db.close();
}
public Cursor getSongs(String queryStr)
{
return db.rawQuery(queryStr, null);
}
///////////////////////////////////////////////////////////////////////
public SQLiteDatabase openDataBase() throws SQLException
{
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
return db;
}
/**
* Close the database if exist
*/
@Override
public synchronized void close()
{
if (db != null)
db.close();
super.close();
}
}