私は自分のテーブルを読んでいるだけですが、これらのエラーが発生するため、アプリを起動できません:
dbclass に接続するためのコードを次に示します。DBclass の id メソッドを呼び出してから、db を閉じます。
public int return_id_phone(Context contxt) {
Database db = new Database(contxt);
db.open();
int id_phone = db.id(2);
db.close();
return id_phone;
}
抽出するコードは次のとおりです。単純な数値のみを返すだけで、クラッシュする理由がわかりません。以下のエラーには、カーソルを閉じておらず、データベースもロックされていることが示されています。
これが私のデータベースクラスです。昨日は完璧に機能していましたが、今日は機能していません...
パッケージcom.background;
公開クラス データベース {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "sync";
public static final String ID_ROW = "_ids";
public static final String VALUE_ROW = "_value";
public static final String ID = "id";
public static final String STATE = "state";
public static final String ID_CONTACTS = "idcontacts";
public static final String NAME = "name";
public static final String NUMBER = "phone";
private static final String DATABASE_NAME = "faraway";
private static final String DATABASE_TABLE = "sync";
private static final String DATABASE_TABLE_2 = "user";
private static final String DATABASE_TABLE_3 = "service";
private static final String DATABASE_TABLE_4 = "contacts";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
ArrayList<String> list = new ArrayList<String>();
String proba = "";
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " INTEGER NOT NULL);");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_2 + " (" + ID_ROW
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + VALUE_ROW
+ " INTEGER NOT NULL);");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_3 + " (" + ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + STATE
+ " INTEGER NOT NULL);");
db.execSQL("CREATE TABLE " + DATABASE_TABLE_4 + " (" + ID_CONTACTS
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
+ " TEXT NOT NULL, " + NUMBER + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public Database(Context c) {
ourContext = c;
}
public Database open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long primerCop() {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, 1);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public long updateSync(int ids, int value) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, value);
return ourDatabase.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + ids,
null);
}
public long zeroAll(int ids) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, 0);
return ourDatabase.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + ids,
null);
}
public List<String> check() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=1",
null, null, null, null);
if (c != null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
list.add(c.getString(0));
}
return list;
} else {
return null;
}
}
public long personaldata(Integer id) {
ContentValues cv = new ContentValues();
cv.put(VALUE_ROW, id);
return ourDatabase.insert(DATABASE_TABLE_2, null, cv);
}
public int id(int row) {
String[] columns = new String[] { ID_ROW, VALUE_ROW };
Cursor c = ourDatabase.query(DATABASE_TABLE_2, columns, ID_ROW + "='"
+ row+"'", null, null, null, null);
if (c != null) {
c.moveToFirst();
int id= c.getInt(1);
c.close();
return id;
} else {
return 0;
}
}
public long prepare_service() {
ContentValues cv = new ContentValues();
cv.put(STATE, 0);
return ourDatabase.insert(DATABASE_TABLE_3, null, cv);
}
public int chech_state(int id) {
String[] columns = new String[] { ID, STATE };
Cursor c = ourDatabase.query(DATABASE_TABLE_3, columns, ID + "=" + id,
null, null, null, null);
if (c != null) {
c.moveToFirst();
int a = c.getInt(1);
c.close();
return a;
} else {
return 2;
}
}
public long update_s(int value, int id) {
ContentValues cv = new ContentValues();
cv.put(STATE, value);
return ourDatabase.update(DATABASE_TABLE_3, cv, KEY_ROWID + "=" + id,
null);
}
public boolean Preferences(int id) {
String[] columns = new String[] { KEY_ROWID, KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
+ id, null, null, null, null);
if (c != null) {
c.moveToFirst();
int number = c.getInt(1);
if (number == 1) {
c.close();
return true;
} else {
c.close();
return false;
}
} else {
c.close();
return false;
}
}
public long contact_table(String name, String number) {
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(NUMBER, number);
return ourDatabase.insert(DATABASE_TABLE_4, null, cv);
}
public long update_name(String name, String number) {
ContentValues cv = new ContentValues();
cv.put(NAME, name);
return ourDatabase.update(DATABASE_TABLE_4, cv, NUMBER + "='" + number
+ "'", null);
}
public long update_phone(String name, String number) {
ContentValues cv = new ContentValues();
cv.put(NUMBER, number);
return ourDatabase.update(DATABASE_TABLE_4, cv, NAME + "='" + name
+ "'", null);
}
public JSONObject select_contact(String name, String number) {
String queryf = "select * from " + DATABASE_TABLE_4 + " where " + NAME
+ "='" + name + "' or " + NUMBER + "='" + number + "'";
Cursor c = ourDatabase.rawQuery(queryf, null);
JSONObject json = new JSONObject();
if (c.getCount() == 0) {
// No hay usuario y lo insertamos en la db
c.close();
try {
json.put("type", "upload");
json.put("name", name);
json.put("phone", number);
} catch (Exception ex) {
ex.printStackTrace();
}
return json;
} else {
c.close();
String queryf2 = "select * from " + DATABASE_TABLE_4 + " where "
+ NAME + "='" + name + "' and " + NUMBER + "='" + number
+ "'";
Cursor c2 = ourDatabase.rawQuery(queryf2, null);
if (c2.getCount() != 0) {
c2.close();
try {
json.put("type", "exists");
} catch (Exception ex) {
ex.printStackTrace();
}
return json;
} else {
c2.close();
String queryf3 = "select * from " + DATABASE_TABLE_4
+ " where " + NAME + "='" + name + "'";
Cursor c3 = ourDatabase.rawQuery(queryf3, null);
if (c3.getCount() != 0) {
c3.close();
try {
json.put("type", "change_phone");
} catch (Exception ex) {
ex.printStackTrace();
}
return json;
} else {
String queryf4 = "select * from " + DATABASE_TABLE_4
+ " where " + NUMBER + "='" + number + "'";
Cursor c4 = ourDatabase.rawQuery(queryf4, null);
if (c4.getCount() != 0) {
c4.close();
try {
json.put("type", "change_name");
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
}
}
return null;
}
}
}
何が間違っているのかわかりません...
> 07-09 11:54:05.460: E/SQLiteDatabase(6938): close() was never explicitly called on database '/data/data/com.anonyme.app/databases/faraway'
07-09 11:54:05.460: E/SQLiteDatabase(6938): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2069)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1123)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1081)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1167)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:833)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.background.Database.open(Database.java:89)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.extract.Contacts.phoneandname(Contacts.java:124)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.anonyme.app.IntroActivity.uploadall(IntroActivity.java:85)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.anonyme.app.IntroActivity.onCreate(IntroActivity.java:76)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.Activity.performCreate(Activity.java:4465)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.os.Looper.loop(Looper.java:137)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 11:54:05.460: E/SQLiteDatabase(6938): at dalvik.system.NativeStart.main(Native Method)
07-09 11:54:05.460: E/System(6938): Uncaught exception thrown by finalizer
07-09 11:54:05.465: E/System(6938): java.lang.IllegalStateException: Don't have database lock!
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2227)
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2319)
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2315)
07-09 11:54:05.465: E/System(6938): at android.util.LruCache.trimToSize(LruCache.java:197)
07-09 11:54:05.465: E/System(6938): at android.util.LruCache.evictAll(LruCache.java:285)
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2280)
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1252)
07-09 11:54:05.465: E/System(6938): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:2040)
07-09 11:54:05.465: E/System(6938): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
07-09 11:54:05.465: E/System(6938): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
07-09 11:54:05.465: E/System(6938): at java.lang.Thread.run(Thread.java:856)