尝试在我的 Android 应用程序中加载然后查询和 SQLite 数据库 - 严重难倒
基本上我想做的是从我的资产文件夹加载我的数据库(romskillDB.db),然后查询我想要的信息。我尝试了几乎在网上可以找到的所有数据库帮助程序类,我尝试研究了所述帮助程序类调用的每个方法,我什至采用了示例代码,复制了它,然后尝试使其适应我的需要。一切都无济于事。因此,这是我在放弃并将数据硬编码到 .xml 文件中的广泛列表中之前获得所需答案的最后努力。请查看下面的代码,非常感谢您花时间提供帮助。
这是显示结果的类。
package com.rom.testdb;
import java.io.IOException;
import com.rom.testdb.DbUtils;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class testDB extends ListActivity {
SQLiteDatabase db;
DbUtils dbUtil;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbUtil = new DbUtils();
try {
DbUtils.createDatabaseIfNotExists(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SQLiteDatabase db = DbUtils.getStaticDb();
String table = "magegeneral";
String[] columns = {"_id", "name"};
Cursor cursor = db.query(table, columns, null, null, null, null, null);
startManagingCursor(cursor);
cursor.moveToFirst();
int thisId = cursor.getInt(0);
String thisName = cursor.getString(1);
cursor.close();
TextView textview = new TextView(this);
textview.setText(thisId + " " + thisName);
setContentView(textview);
}
}
这是帮助程序代码。
package com.rom.testdb;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class DbUtils {
private static final String DB_PATH = "/data/data/com.rom.testdb/databases/";
private static final String DB_NAME = "romskillDB.db";
public static void createDatabaseIfNotExists(Context context) throws IOException {
boolean createDb = false;
File dbDir = new File(DB_PATH);
File dbFile = new File(DB_PATH + DB_NAME);
if (!dbDir.exists()) {
dbDir.mkdir();
createDb = true;
}
else if (!dbFile.exists()) {
createDb = true;
}
else {
// Check that we have the latest version of the db
boolean doUpgrade = false;
// Insert your own logic here on whether to upgrade the db; I personally
// just store the db version # in a text file, but you can do whatever
// you want. I've tried MD5 hashing the db before, but that takes a while.
// If we are doing an upgrade, basically we just delete the db then
// flip the switch to create a new one
if (doUpgrade) {
dbFile.delete();
createDb = true;
}
}
if (createDb) {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(dbFile);
// 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);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
}
public static SQLiteDatabase getStaticDb() {
return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
}
}
再次感谢您的宝贵时间!
Basically what I am trying to do is load my database (romskillDB.db) from my assets folder and then query for the information I want. I have tried almost every db helper class I could find on the net, I've tried researching every single method called by said helper classes, and I've even taken example code, copied it, and then tried to adapt it to my needs. All to no avail. So this is my last ditch effort to get the answer I need before I give up and hardcode the data into an extensive list in .xml files. Please review my code below, and thank you so very much for taking the time to help.
This is the Class that displays the results.
package com.rom.testdb;
import java.io.IOException;
import com.rom.testdb.DbUtils;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class testDB extends ListActivity {
SQLiteDatabase db;
DbUtils dbUtil;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbUtil = new DbUtils();
try {
DbUtils.createDatabaseIfNotExists(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SQLiteDatabase db = DbUtils.getStaticDb();
String table = "magegeneral";
String[] columns = {"_id", "name"};
Cursor cursor = db.query(table, columns, null, null, null, null, null);
startManagingCursor(cursor);
cursor.moveToFirst();
int thisId = cursor.getInt(0);
String thisName = cursor.getString(1);
cursor.close();
TextView textview = new TextView(this);
textview.setText(thisId + " " + thisName);
setContentView(textview);
}
}
This is the helper code.
package com.rom.testdb;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class DbUtils {
private static final String DB_PATH = "/data/data/com.rom.testdb/databases/";
private static final String DB_NAME = "romskillDB.db";
public static void createDatabaseIfNotExists(Context context) throws IOException {
boolean createDb = false;
File dbDir = new File(DB_PATH);
File dbFile = new File(DB_PATH + DB_NAME);
if (!dbDir.exists()) {
dbDir.mkdir();
createDb = true;
}
else if (!dbFile.exists()) {
createDb = true;
}
else {
// Check that we have the latest version of the db
boolean doUpgrade = false;
// Insert your own logic here on whether to upgrade the db; I personally
// just store the db version # in a text file, but you can do whatever
// you want. I've tried MD5 hashing the db before, but that takes a while.
// If we are doing an upgrade, basically we just delete the db then
// flip the switch to create a new one
if (doUpgrade) {
dbFile.delete();
createDb = true;
}
}
if (createDb) {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(dbFile);
// 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);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
}
public static SQLiteDatabase getStaticDb() {
return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
}
}
And again, thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个问题可能会导致这种迷恋——
一个。这是您在内部运行的列表活动,尝试使用 getApplicationContext() 启动数据库(或者类似的东西,我忘记了确切的术语...)
b.你不能从UI线程执行逻辑操作,使用asynctask在后台处理数据库查询..
至于第二个我确信95%,因为我曾经遇到过类似的问题并且(使用asynctask)解决了我的问题
There are two problems that might cause this crush -
a. your this is the list activity you run inside try to initiate the db using getApplicationContext() (Or somethng like that I forgot the exact term...)
b. you cant perform logical operations from UI thread, use asynctask to handle db queries on the background..
as for the second one I am sure on 95% since I had a similar problem once and that (using asynctask) solved my issue