开发者

Reading DB from SDcard on android emulator

I want to refer to SQLite DB file in an android app. I pushed it on SDcard on the emulator from eclipse and then I am running my app which is installed on phone memory. The code is for query based retrieval of data.

I am using following class file.

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?


This code expects DB to be on this location:

mnt\sdcard\SMSconfApp1\SMSconfApp1

First, App Dir is constructed as :

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

This would result in : mnt\sdcard\SMSconfApp1\ , later once DB File Name is added it becomes mnt\sdcard\SMSconfApp1\SMSconfApp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜