我在 stackoverflow 上看了很多关于这个错误的帖子,看起来解决方法是重写 onDestroy() 方法并关闭数据库。但这对我来说似乎不起作用。当 Eclipse 最初在我的手机上运行该应用程序时,我实际上没有收到任何错误,但是当我关闭该应用程序并在我的手机上重新启动它时 - 那是 close() 从未在数据库上显式调用出现在 LogCat 中。我正在调用 Assets 文件夹中的外部数据库,不确定这是否与它有任何关系。
数据库适配器:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_HDATE = "date";
public static final String KEY_MONTHDAY = "monthday";
public static final String KEY_YEAR = "year";
public static final String KEY_HD = "happened";
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.vdm.whtt/databases/";
private static String DB_NAME = "wht.db";
private static final String DB_TABLE = "historydaytable";
private SQLiteDatabase db;
private final Context ourContext;
public DBAdapter(Context context) {
super(context, DB_NAME, null, 1);
this.ourContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// 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 are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = ourContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
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 open() throws SQLException {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(db);
}
public String[] getTH(long aLong) {
String[] columns = new String[] { KEY_YEAR, KEY_MONTHDAY, KEY_HD };
Cursor cursor = db.query(DB_TABLE, columns, KEY_HDATE + "=" + aLong,
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
String hpToday[] = new String[3];
hapToday[0] = cursor.getString(0);
hapToday[1] = cursor.getString(1);
hapToday[2] = cursor.getString(2);
cursor.close();
this.db.close();
return hpToday;
}
return null;
}
public String[] getFirstRandomAnswer(int randomNum1) {
String[] columns = new String[] { KEY_YEAR, KEY_MONTHDAY, KEY_HD };
Cursor cursor = db.query(DB_TABLE, columns, null,
null, null, null, null);
if (cursor != null) {
cursor.moveToPosition(randomNum1);
String r1Str[] = new String[3];
r1Str[0] = cursor.getString(0);
r1Str[1] = cursor.getString(1);
r1Str[2] = cursor.getString(2);
cursor.close();
this.db.close();
return r1Str;
}
return null;
}
public String[] getSecondRandomAnswer(int randomNum2) {
String[] columns = new String[] { KEY_YEAR, KEY_MONTHDAY, KEY_HD };
Cursor cursor = db.query(DB_TABLE, columns, null,
null, null, null, null);
if (cursor != null) {
cursor.moveToPosition(randomNum2);
String r2Str[] = new String[3];
r2Str[0] = cursor.getString(0);
r2Str[1] = cursor.getString(1);
r2Str[2] = cursor.getString(2);
cursor.close();
this.db.close();
return r2Str;
}
return null;
}
}
主要 Activity :
import java.io.IOException;
import java.util.Locale;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import android.app.Activity;
import android.database.SQLException;
public class MainActivity extends Activity {
private DBAdapter dba;
TextView hiddenDBdatetv, todayIstv, optionAtv;
String MMdStr;
String[] todayArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hiddenDBdatetv = (TextView) findViewById(R.id.hiddenDBdate_TV);
todayIstv = (TextView) findViewById(R.id.todayIs_TV);
optionAtv = (TextView) findViewById(R.id.optionA_TV);
dba = new DBAdapter(this);
try {
dba.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dba.open();
} catch (SQLException sqle) {
throw sqle;
}
getMMdDate();
getTH();
}
private void getMMdDate() {
Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MMd", Locale.US);
MMdStr = formatter.format(anotherCurDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMd");
Date myDate = null;
try {
myDate = dateFormat.parse(MMdStr);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("MMMMMMMMMM d");
String finalDate = timeFormat.format(myDate);
}
private void getTH() {
long aLong = Long.parseLong(MMdStr);
dba.open();
todayArray = dba.getTH(aLong);
dba.close();
optionAtv.setText(todayArray[2]);
}
@Override
protected void onDestroy() {
dba.close();
super.onDestroy();
}
}
最佳答案
您将在 createDatabase() 中打开一个数据库(当 dbExist == false 时),并且您将在 onCreate( ),然后再次在 getTH()...
避免此错误的技巧是跟踪您的数据库何时打开,并确保在您使用完后关闭它。考虑检查 isOpen()如果您发现自己多次打开数据库,请在尝试打开数据库之前。
关于android - close() 从未在 Android 和 SQlite 中显式调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615348/
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
如何找到调用此方法的位置?defto_xml(options={})binding.pryoptions=options.to_hifoptions&&options.respond_to?(:to_h)serializable_hash(options).to_xml(options)end 最佳答案 键入caller。这将返回当前调用堆栈。文档:Kernel#caller.例子[0]%rspecspec10/16|===================================================62=====
Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"
我正在写一篇关于在Ruby中几乎一切都是对象的博客文章,我试图通过以下示例来展示这一点:classCoolBeansattr_accessor:beansdefinitialize@bean=[]enddefcount_beans@beans.countendend所以从类中我们可以看出它有4个方法(当然,除非我错了):它可以在创建新实例时初始化一个默认的空bean数组它可以计算它有多少个bean它可以读取它有多少个bean(通过attr_accessor)它可以向空数组写入(或添加)更多bean(也通过attr_accessor)但是,当我询问类本身它有哪些实例方法时,我没有看到默认