我想从 SQLITE 数据库中获取数据并将其显示在 LIST 中。
我的数据库名称是 AppDB.db,表名称是 Scrip,其中包含 3 列 _id(主键)和 symbol&company_name,它们是文本。
我只想获取第 2 和第 3 列。我已经在 Assets 文件夹中复制了数据库。
当我执行以下代码时,我强制关闭,但我不知道可能是什么原因。请帮助..
我是 Android 的新手,所以感谢任何帮助...
内容:
1.) DataAttach.java
2.) DbManager.java
3.) 日志
4.) .xml 简介
5.) AVD 问题
6.) 数据库快照
1.) DataAttach.java
public class DataAttach extends Activity {
private DbManager dbM;
private Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
dbM = new DbManager(this);
dbM.createDataBase();
dbM.openDB();
try {
c = dbM.fetchAllData();
} catch (Exception e) {
throw new Error("\n\nERROR Fetchin data\n\n");
}
if (c != null) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
c,
new String[] { c.getString(c.getColumnIndex("_id")),
c.getString(c.getColumnIndex("symbol")),
c.getString(c.getColumnIndex("company_name")) },
new int[] { R.id._id, R.id.symbol, R.id.company_name });
ListView list = (ListView) findViewById(R.id.MainLayout);
list.setAdapter(adapter);
}
}
catch (Exception e) {
throw new Error("\n\nERROR DB failure\n\n");
}
finally {
c.close();
dbM.close();
}
}
}
2.) DbManager.java
public class DbManager extends SQLiteOpenHelper {
private static final String KEY_ROWID = "_id";
private static final String KEY_SYMBOL = "symbol";
private static final String KEY_COMPANY_NAME = "company_name";
private static final String DATABASE_NAME = "AppDB";
private static final String DATABASE_PATH = "/data/data/com.dbexample/databases/";
private static final Integer DATABASE_VERSION = 3;
private static final String DATABASE_TABLE = "Scrip";
// private static final String TAG = "DbManager";
private final Context ctx;
private SQLiteDatabase mDb;
public DbManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.ctx = context;
}
/*
* Creates a empty database on the system and rewrites it with your own
* database.
*/
public void createDataBase() {
if (checkDataBase()) {
// do nothing - database already exist
} else {
/*
* By calling this method and empty database will be created into
* the default system path of your application so we can overwrite
* that database with our database.
*/
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("\n\nERROR copying database\n\n");
}
}
}
/*
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
throw new Error("\n\nERROR database does't exist yet\n\n");
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/*
* Copies your DB from your local assets-folder to the just created empty DB
* in the system folder, from where it can be accessed and handled.
*/
private void copyDataBase() throws IOException {
/* Open your local db as the input stream */
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
/* Path to the just created empty db */
String outFileName = DATABASE_PATH + DATABASE_NAME;
/* Open the empty db as the output stream */
OutputStream myOutput = new FileOutputStream(outFileName);
/* transfer bytes from the inputfile to the outputfile */
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDB() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
mDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public void close() {
mDb.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor fetchAllData() {
return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_SYMBOL,
KEY_COMPANY_NAME }, null, null, null, null, null);
// return mDb.rawQuery("select _id,symbol,company_name from Scrip",
// new String[] { KEY_ROWID,KEY_SYMBOL, KEY_COMPANY_NAME });
}
}
3.) 日志 CAT
sqlite3_open_v2("/data/data/com.dbexample/databases/AppDB", &handle, 1, NULL) failed
Failed to open the database. closing it.
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1017)
4.) Main.xml
它包含 RelativeLayout,在 RelativeLayout 中有 3 个 TextView。
5.) 我总是遇到这个错误
即使在重新打开 eclipse、重新启动系统、重新创建 AVD、尝试 kill_server 和 start_server 之后,我仍然收到此消息。
Failed to install DataAttach.apk on device 'emulator-5554': device not found
com.android.ddmlib.InstallException: device not found
Launch canceled!
6.) 数据库屏幕截图:
已将整个文档编辑为最新代码和 ACC。根据您的建议,但应用仍然无法运行。
最佳答案
我不知道你想用这个函数做什么。您正在尝试读取 .db 文件并说数据库已创建。第一次尝试运行时,数据库路径中不会有 .db 文件。这就是抛出“没有这样的文件或目录”错误的原因。
11-25 12:06:13.398: E/Netd(31): Unable to bind netlink socket: No such file or directory
在 DBManager Constructor 中调用 SQLiteOpenHelper 的 getReadableDatabase() 如果没有数据库,它将创建一个数据库。 SQLiteOpenHelper
public void createNewDatabase() {
InputStream assetsDB = null;
try {
assetsDB = context.getAssets().open(DATABASE_NAME);
OutputStream dbOut = new FileOutputStream(DATABASE_PATH
+ DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = assetsDB.read(buffer)) > 0) {
dbOut.write(buffer, 0, length);
}
dbOut.flush();
dbOut.close();
assetsDB.close();
Log.i(TAG, "New database created...");
} catch (IOException e) {
Log.e(TAG, "Could not create new database...");
}
}
关于android - 无法从 Sqlite 数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268664/