在android emulator上从SD卡读取数据库
我想在android应用程序中引用SQLite DB文件。我在eclipse模拟器上的SD卡上推了它,然后运行安装在手机内存中的应用程序。该代码用于基于查询的数据检索 我正在使用下面的类文件在android emulator上从SD卡读取数据库,android,sqlite,Android,Sqlite,我想在android应用程序中引用SQLite DB文件。我在eclipse模拟器上的SD卡上推了它,然后运行安装在手机内存中的应用程序。该代码用于基于查询的数据检索 我正在使用下面的类文件 public class ExternalStorageReadOnlyOpenHelper { private SQLiteDatabase database; private File dbFile; private SQLiteDatabase.CursorFactory factory; publ
public class ExternalStorageReadOnlyOpenHelper {
private SQLiteDatabase database;
private File dbFile;
private SQLiteDatabase.CursorFactory factory;
public static final String KEY_ROWID1= "_id";
public static final String KEY_num1= "num1";
public static final String KEY_words1 = "words1";
private static final String DATABASE_TABLE1 = "dictionary";
public ExternalStorageReadOnlyOpenHelper(
String dbFileName) {
// this.factory = factory;
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
throw new AndroidRuntimeException(
"External storage (SD-Card) not mounted");
}
File appDbDir = new File(
Environment.getExternalStorageDirectory(),
"SMSconfApp1");
if (!appDbDir.exists()) {
appDbDir.mkdirs();
}
this.dbFile = new File(appDbDir, dbFileName);
}
public boolean databaseFileExists() {
return dbFile.exists();
}
private void open() {
if (dbFile.exists()) {
database = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(),
factory,
SQLiteDatabase.OPEN_READONLY);
}
}
public void close() {
if (database != null ) {
database.close();
database = null;
}
}
public SQLiteDatabase getReadableDatabase() {
return getDatabase();
}
private SQLiteDatabase getDatabase() {
if (database==null) {
open();
}
return database;
}
public String getID(String pqr, SQLiteDatabase db) throws SQLException
{
String data="";
Cursor mCursor =
db.query(true, DATABASE_TABLE1, new String[] {
KEY_ROWID1,
KEY_num1,
KEY_words1
},
KEY_words1 + "=?",
new String[]{pqr},
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
if (mCursor.moveToFirst()){
do{
data = mCursor.getString(1);
// do what ever you want here
}while(mCursor.moveToNext());
}
mCursor.close();
}
return data;
}
}
我的活动如下
当我运行应用程序时,它不会显示从数据库检索的任何内容。我不明白代码中出了什么问题。有什么帮助吗
此代码期望DB位于以下位置: mnt\sdcard\SMSconfApp1\SMSconfApp1 首先,App Dir被构造为:
File appDbDir = new File( Environment.getExternalStorageDirectory(),
"SMSconfApp1");
这将导致:mnt\sdcard\SMSconfApp1\,稍后添加DB文件名后,它将变为mnt\sdcard\SMSconfApp1\SMSconfApp此代码期望DB位于以下位置: mnt\sdcard\SMSconfApp1\SMSconfApp1 首先,App Dir被构造为:
File appDbDir = new File( Environment.getExternalStorageDirectory(),
"SMSconfApp1");
这将导致:mnt\sdcard\SMSconfApp1\,稍后添加DB文件名后,它将变成mnt\sdcard\SMSconfApp1\SMSconfApp在DB=obj.getReadableDatabase()上是否看到任何异常@萨米特·维尔玛:很抱歉迟了回复。。在eclipse中调试应用程序时,我仍然不太舒服。但是如果我做到了,我可以判断它是否有问题(obj.databaseFileExists())@Shamit Verma:这个条件正在变假。n控制系统不在回路中。appdbdir=\mnt\sdcard\SMSconfApp1.此位置是否存在数据库?此代码期望数据库位于此位置:mnt\sdcard\SMSconfApp1\SMSconfApp1首先,App Dir被构造为:File appdbdir=新文件(Environment.getExternalStorageDirectory(),“SMSconfApp1”);这将导致:mnt\sdcard\SMSconfApp1\,之后,一旦添加了DB文件名,它将变成mnt\sdcard\SMSconfApp1\SMSconfApp1。在DB=obj.getReadableDatabase()上是否看到任何异常@萨米特·维尔玛:很抱歉迟了回复。。在eclipse中调试应用程序时,我仍然不太舒服。但是如果我做到了,我可以判断它是否有问题(obj.databaseFileExists())@Shamit Verma:这个条件正在变假。n控制系统不在回路中。appdbdir=\mnt\sdcard\SMSconfApp1.此位置是否存在数据库?此代码期望数据库位于此位置:mnt\sdcard\SMSconfApp1\SMSconfApp1首先,App Dir被构造为:File appdbdir=新文件(Environment.getExternalStorageDirectory(),“SMSconfApp1”);这将导致:mnt\sdcard\SMSconfApp1\,稍后添加DB文件名后,它将成为mnt\sdcard\SMSconfApp1\SMSconfApp1