这适用于模拟器但不适用于我的手机。至少看起来是这样,因为当我在手机上查询时,它找不到表格。
这是我继承自SQLiteOpenHelper
的类public class DBHelper extends SQLiteOpenHelper {
private final static String DB_NAME = "klb_db.sqlite";
private final static int DB_VERSION = 1;
private String dbPath;
private Context context;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
dbPath = "/" + Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/";
}
public void initializeDatabase() {
try {
File file = new File(dbPath + DB_NAME);
if (!file.exists()) {
InputStream inputStream = context.getAssets().open(
"klb_db.sqlite");
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在dbadapter中,打开方法:
public DBAdapter open() {
dbHelper.initializeDatabase();
db = dbHelper.getWritableDatabase();
return this;
}
在使用它的活动中:
@Override
protected void onStart() {
super.onStart();
QuizDAO quizDAO = new QuizDAO(this);
quizDAO.open();
Cursor cursor = quizDAO.getQuestion(10);
Toast.makeText(this, cursor.getString(1), Toast.LENGTH_LONG).show();
}
答案 0 :(得分:2)
尝试增加你的字节数
byte[] buffer = new byte[1024];
到
byte[] buffer = new byte[2048];
<强> EDITED 强>
@Override
protected void onStart() {
super.onStart();
QuizDAO quizDAO = new QuizDAO(this);
quizDAO.open();
Cursor cursor = quizDAO.getQuestion(10);
cursor.moveToFirst()
Toast.makeText(this, cursor.getString(1), Toast.LENGTH_LONG).show();
}