インサートをSqlLiteで動作させるのに苦労しています。次のスキーマで作成されたテーブルがあります。createtabledrink_category(_id integer not null primary key autoincrement、value unique);
添付されたコードを実行すると、「データベースファイルを開けません(コード14)」というエラーメッセージが表示されたSQLiteCantOpenDatebaseExceptionが発生します。
コードからわかるように、問題なくクエリを使用してテーブルにアクセスします。また、db.isOpen()を実行して、データベースに問題がないことを確認します。挿入されたブロックの後にコードを追加しましたが、すべて正常に機能します。どんな助けでも大歓迎です。
public Boolean add(String strValue)
{
Boolean bStatus = true;
String strSql;
String sqlValue = strValue.replaceAll("'","\'\'");
if (bStatus)
{
if (strValue.isEmpty())
{
BarDB.getInstance().setErrorMessage("No value entered.");
bStatus = false;
}
}
if (bStatus)
{
strSql = "select value from " + "drink_category" + " where value = '" + sqlValue + "' ";
Cursor cursor = SqlDbAdapter.getInstance().getDatabase().rawQuery(strSql, null);
if (null == cursor)
{
BarDB.getInstance().setErrorMessage("Problem with the database.");
bStatus = false;
}
else if (cursor.getCount() > 0)
{
BarDB.getInstance().setErrorMessage("A Drink Category with that name already exists.");
bStatus = false;
}
}
if (bStatus)
{
ContentValues cvValues = new ContentValues();
cvValues.put("value", sqlValue);
SQLiteDatabase db = SqlDbAdapter.getInstance().getDatabase();
if (db.isOpen())
{
try
{
long lerror = db.insertOrThrow("drink_category", null, cvValues);
}
catch (SQLiteException sqle)
{
BarDB.getInstance().setErrorMessage(sqle.toString());
bStatus = false;
}
}
}
return bStatus;
}