[英]Database works fine in emulator, but gives an error on device
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 我的databasehelper类是
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();
}
} }
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.
一旦你知道它就像那样工作,那么你可以回去修改路径,如果你需要的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.