在android模拟器上从SD卡读取DB

发布于 2024-10-25 00:19:55 字数 3201 浏览 1 评论 0原文

我想在 Android 应用程序中引用 SQLite DB 文件。我从 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;
}
}

我的活动如下

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

}
}

当我运行该应用程序时,它没有显示任何来自数据库的检索。我不明白代码中出了什么问题。有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-11-01 00:19:55

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

mnt\sdcard\SMSconfApp1\SMSconfApp1

首先,应用程序目录构造为:

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

这将导致: mnt\sdcard\SMSconfApp1\ ,稍后添加数据库文件名后,它将变为 mnt\sdcard\ SMSconfApp1\SMSconfApp

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文