gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:44:10 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com