Reputation: 927
My app creates a database the moment it runs, and stores some basic data in it. However, when I try to run the app on the device, I am getting "Unable to open database" error, while it is working and is fully functional on the emulator.
my main activity is
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;
}
}
and my databasehelper class is
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();
}
}
Upvotes: 0
Views: 1545
Reputation: 8528
I think you messed up the path and the real device won't let you put a file in that location, while the emulator will. Try removing the path entirely and letting the system figure out where to put the database. Once you know it works like that, then you can go back and modify the path if you need to.
Upvotes: 2