0

私はsqliteデータベースにアクセスしている場所からアンドロイドにSQLiteHelperクラスを持っています.テーブルに生を挿入したいのですが、生がすでに存在する場合はそれを更新したいです.ここに私の実装があります:

public void addShares(ArrayList<SharePrices> shares) {
    SQLiteDatabase database = this.getWritableDatabase();
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues values = new ContentValues();
    String share_name;
    double share_price;
    double yesterday_price;
    for (int i = 0; i < shares.size(); i++) {
        try {
            Cursor cursor = null;
            share_name = shares.get(i).getName();
            share_price = Double.parseDouble(shares.get(i).getPrice());
            yesterday_price = Double.parseDouble(shares.get(i)
                    .getYesterdays());
            values.put(KEY_NAME, share_name);
            values.put(KEY_PRICE, share_price);
            values.put(KEY_YESTERDAY, yesterday_price);
            cursor = db.query(TABLE_SHARES, new String[] { KEY_NAME },
                    KEY_NAME + "=?", new String[] { share_name }, null,
                    null, null, null);

            if (cursor.getCount() == 1) {
                db.close();
                database.update(TABLE_SHARES, values, KEY_NAME + "=?",
                        new String[] { share_name });
                database.close();
                Log.i("db", "updated");
            } else {
                db.close();
                database.insert(TABLE_SHARES, null, values);
                database.close();
                Log.i("db", "inserted");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ただし、例外が発生します。データベースのロックと関係があります。ここに私のlogcatデータがあります

09-12 14:31:51.020: W/System.err(25491): java.lang.NullPointerException
09-12 14:31:51.120: W/System.err(25491):    at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
09-12 14:31:51.190: W/System.err(25491):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
09-12 14:31:51.190: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1810)
09-12 14:31:51.210: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1761)
09-12 14:31:51.230: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:103)
09-12 14:31:51.270: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:51.270: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:51.300: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:51.310: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:51.320: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:51.371: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:51.371: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:51.401: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:51.441: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:51.491: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:51.521: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:51.651: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:51.651: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:51.661: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:51.661: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:51.661: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:51.661: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:51.661: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:51.661: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:51.661: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:51.671: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:51.671: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:51.671: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:51.681: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:51.681: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:51.691: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:51.711: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:51.721: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:51.731: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:51.731: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:51.731: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:51.741: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:51.741: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:51.741: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:51.741: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:51.741: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:51.751: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:51.761: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:51.771: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:51.781: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:51.801: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:51.801: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:51.801: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:51.811: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:51.821: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:51.831: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:51.861: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:51.861: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:51.871: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:51.871: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:51.871: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:51.871: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:51.871: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:51.881: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:51.881: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:51.881: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:51.881: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:51.881: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:51.891: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:51.891: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:51.911: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:51.921: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:51.931: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:51.931: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:51.941: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:51.941: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:51.941: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:51.941: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:51.941: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:51.941: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:51.941: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:51.951: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:51.951: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:51.951: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:51.951: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:51.980: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:51.990: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:51.990: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:51.990: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:51.990: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:52.001: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:52.010: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:52.020: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:52.030: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:52.060: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:52.060: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:52.060: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:52.060: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:52.070: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:52.070: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:52.070: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:52.081: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:52.100: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:52.100: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:52.120: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:52.120: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:52.120: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:52.130: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:52.130: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:52.130: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:52.130: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:52.130: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:52.150: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:52.160: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:52.170: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:52.180: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:52.180: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:52.190: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:52.190: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:52.190: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:52.190: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:52.200: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:52.200: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:52.200: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:52.200: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:52.200: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:52.220: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:52.230: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:52.240: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:52.240: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:52.250: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:52.250: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:52.260: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:52.260: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:52.260: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)
09-12 14:31:52.270: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
09-12 14:31:52.280: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1523)
09-12 14:31:52.290: W/System.err(25491):    at com.intelligent.stocktrader.database.DatabaseHandler.addShares(DatabaseHandler.java:97)
09-12 14:31:52.310: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService.getSharePrices(ShareService.java:162)
09-12 14:31:52.310: W/System.err(25491):    at com.intelligent.stocktrader.database.ShareService$1.run(ShareService.java:71)
09-12 14:31:52.310: W/System.err(25491):    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
09-12 14:31:52.310: W/System.err(25491):    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:322)
09-12 14:31:52.310: W/System.err(25491):    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:149)
09-12 14:31:52.320: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:150)
09-12 14:31:52.320: W/System.err(25491):    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-12 14:31:52.320: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 14:31:52.320: W/System.err(25491):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 14:31:52.330: W/System.err(25491):    at java.lang.Thread.run(Thread.java:856)
09-12 14:31:52.330: W/System.err(25491): java.lang.IllegalStateException: database /data/data/com.intelligent.stocktrader/databases/stocktrader.db (conn# 0) already closed
09-12 14:31:52.330: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
09-12 14:31:52.341: W/System.err(25491):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1445)

ここからデータベースクラスを呼び出しています:

 try {
        DatabaseHandler dbhandler = new DatabaseHandler(this);
        dbhandler.addShares(results);
     } catch (Exception e) {
        e.printStackTrace();
     }
4

2 に答える 2

0

メソッド getWritableDatabase() が読み取り可能なデータベースと書き込み可能なデータベースの両方を返すことを知りませんでした。したがって、SQLite.getReadableDatabase() メソッドを呼び出す必要はありません。両方が同時に呼び出されると、一方がデータベース ロックを保持し、もう一方がロックされなくなります。データベースにアクセスしています。これが起こっていたことです。getReadableDatabase メソッドはデータベース ロックを排他的に保持していました。修正されたコードは次のとおりです。

public void addShares(ArrayList<SharePrices> shares) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String share_name;
    double share_price;
    double yesterday_price;
    for (int i = 0; i < shares.size(); i++) {
        try {
            Cursor cursor = null;
            share_name = shares.get(i).getName();
            share_price = Double.parseDouble(shares.get(i).getPrice());
            yesterday_price = Double.parseDouble(shares.get(i)
                    .getYesterdays());
            values.put(KEY_NAME, share_name);
            values.put(KEY_PRICE, share_price);
            values.put(KEY_YESTERDAY, yesterday_price);
            cursor = database.query(TABLE_SHARES,
                    new String[] { KEY_NAME }, KEY_NAME + "=?",
                    new String[] { share_name }, null, null, null, null);

            if (cursor.getCount() == 1) {
                database.update(TABLE_SHARES, values, KEY_NAME + "=?",
                        new String[] { share_name });

                Log.i("db", "updated");
            } else {
                database.insert(TABLE_SHARES, null, values);

                Log.i("db", "inserted");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    database.close();
}

これが将来誰かに役立つことを願っています。

于 2013-09-12T12:26:42.330 に答える
0

より簡単な方法でそれを実現できます。これは単なる例です。コードから実際の値などを入力する必要があります。

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(...); // all the values you need to update

    if (db.update(TABLE_NAME, values, _ID + "=" + id, null) == 0) {
        db.insert(TABLE_NAME, null, values);
    }

    db.close();
于 2013-09-12T11:58:32.237 に答える