2011-03-22 52 views
1

我想在Android應用程序中引用SQLite數據庫文件。我將它從eclipse推到模擬器上的SDcard中,然後運行安裝在手機內存中的應用程序。該代碼用於基於查詢的數據檢索。在Android模擬器上從SDcard讀數據庫

我正在使用下面的類文件。

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

我的活動如下:

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

} 
} 

當我運行它不會顯示從數據庫檢索的任何應用程序。我沒有看到代碼中出了什麼問題。任何幫助?

+1

您是否看到任何異常在 db = obj.getReadableDatabase(); ? – 2011-03-22 06:50:23

+0

@Shamit Verma:抱歉,遲到的答覆.. M仍然不是很適合eclipse中的調試應用程序。但是,我做了它,我可以得到它的問題,如果(obj.databaseFileExists()) – 2011-03-22 17:43:34

+0

@Shamit Verma:這種情況變得錯誤。控制cuming出循環。 appdbdir = \ MNT \ SD卡\ SMSconfApp1。 – 2011-03-22 17:46:59

回答

1

此代碼期望DB能在這個位置:

MNT \ SD卡\ SMSconfApp1 \ SMSconfApp1

首先,應用風向被構造爲:

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

這將導致:mnt \ sdcard \ SMSconfApp1 \,稍後一旦添加數據庫文件名稱就會變成mnt \ sdcard \ SMSconfApp1 \ SMSconfApp

相關問題