android - 在 Android 模拟器上从 SD 卡读取数据库

标签 android sqlite

我想在 Android 应用程序中引用 SQLite 数据库文件。我从 eclipse 将它推送到模拟器上的 SD 卡上,然后我运行我的应用程序,它安装在手机内存上。该代码用于基于查询的数据检索。

我正在使用以下类文件。

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;
}
}

My activity is as follows

public class SDcardDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv= new TextView(this);
    setContentView(R.layout.main);
    SQLiteDatabase db;
    String DBFile="SMSconfApp1";
    String number=null;
   // ExternalStorageReadOnlyOpenHelper.open();
    ExternalStorageReadOnlyOpenHelper obj= new ExternalStorageReadOnlyOpenHelper(DBFile);
    if(obj.databaseFileExists())
    {
        db=obj.getReadableDatabase();
        number=obj.getID("hi", db);
        obj.close();
    }
        tv.setText(number);
        setContentView(tv);

}
}

When I run the app it is not displaying any retrieval from the DB. I am not getting what is going wrong in the code. Any help?

最佳答案

此代码期望数据库位于此位置:

mnt\sdcard\SMSconfApp1\SMSconfApp1

首先,App Dir 构造为:

File appDbDir = new File( Environment.getExternalStorageDirectory(),
    "SMSconfApp1");

这将导致: mnt\sdcard\SMSconfApp1\,稍后一旦添加了数据库文件名,它就会变成 mnt\sdcard\SMSconfApp1\SMSconfApp

关于android - 在 Android 模拟器上从 SD 卡读取数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5387695/

相关文章:

android - ScrollView 上方的线性布局

android - 使用 Retrofit 处理变量 JSON

android - 不幸的是应用程序已停止,doinbackground() 出错

c# - 未找到方法 'SQLiteConnection.InsertAll'

python - 在python代码中访问在django中创建的表时出错

android - 从android设备读取数据库文件

sqlite - 对 Sqlite 数据库管理器应用程序的推荐?

ruby-on-rails - Rails 3 SQLite3 bool 值 false

android - 我必须使用 _ID 作为 SQlite 主键吗?它必须是 INT 吗? (安卓开发)

c# - 从 SQLite 数据库 C# WPF 中提取 DateTime 的问题