私は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();
}