我想修改数据库中某行的记录,但是不知何故出现了database is locked
错误
void Bookshop::modifyBookRecords () {
std::cout << "You want to modify a book" << std::endl;
std::cout << "Title/ISBN: ";
std::string input;
std::getline (std::cin, input);
bool isAvailable = database.checkAvailability (input);
database.closeDb ();
if (isAvailable) {
std::cout << "Title: ";
std::string title;
std::getline (std::cin, title);
std::cout << "Author: ";
std::string author;
std::getline (std::cin, author);
std::cout << "Pages: ";
int pages;
std::cin >> pages;
std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
std::cout << "Genre: ";
std::string genre;
std::getline (std::cin, genre);
std::cout << "Publisher: ";
std::string publisher;
std::getline (std::cin, publisher);
std::cout << "ISBN: ";
std::string isbn;
std::getline (std::cin, isbn);
std::cout << "Number of Copies: ";
int numberOfCopies;
std::cin >> numberOfCopies;
std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
database.updateRecord (input, title, author, pages, genre, publisher, isbn, numberOfCopies);
}
else {
std::cout << "The book was not found" << std::endl;
}
}
CheckAvailability方法
bool Database::checkAvailability (std::string& input) {
openDb (dir);
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2 (db, "SELECT * FROM Books WHERE Title = ? OR Isbn = ?", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
std::cout << sqlite3_errmsg (db);
closeDb ();
return false;
}
sqlite3_bind_text (stmt, 1, input.c_str (), input.length (), NULL);
sqlite3_bind_text (stmt, 2, input.c_str (), input.length (), NULL);
if (sqlite3_step (stmt) == SQLITE_ROW) {
return true;
sqlite3_finalize (stmt);
sqlite3_close (db);
}
else {
return false;
sqlite3_finalize (stmt);
sqlite3_close (db);
}
}
updateRecords方法
// Method updates the whole row with the values from the parameter list
void Database::updateRecord (const std::string& input,
const std::string& title,
const std::string& author,
const int& pages,
const std::string& genre,
const std::string& publisher,
const std::string& isbn,
const int& numberOfCopies) {
openDb (dir);
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2 (db, "UPDATE Books SET Title = ?, Author = ?, Pages = ?, Genre = ?, Publisher = ?, Isbn = ?, Copies = ? WHERE Title = ? OR Isbn = ?", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
std::cerr << sqlite3_errmsg (db);
closeDb ();
}
sqlite3_bind_text (stmt, 1, title.c_str (), title.length (), NULL);
sqlite3_bind_text (stmt, 2, author.c_str (), author.length (), NULL);
sqlite3_bind_int (stmt, 3, pages);
sqlite3_bind_text (stmt, 4, genre.c_str (), genre.length (), NULL);
sqlite3_bind_text (stmt, 5, publisher.c_str (), publisher.length (), NULL);
sqlite3_bind_text (stmt, 6, isbn.c_str (), isbn.length (), NULL);
sqlite3_bind_int (stmt, 7, numberOfCopies);
sqlite3_bind_text (stmt, 8, input.c_str (), input.length (), NULL);
sqlite3_bind_text (stmt, 9, input.c_str (), input.length (), NULL);
rc = sqlite3_step (stmt);
if (rc != SQLITE_OK) {
std::cerr << sqlite3_errmsg (db);
}
else {
std::cout << "books was updated successfully" << std::endl;
}
sqlite3_finalize (stmt);
closeDb ();
}
该错误在checkAvailability方法中的某处,因为当我删除它时,一切正常。有人告诉我,数据库在某处打开,因此以后不允许在该方法中打开它。但是我在checkAvailability()结尾时关闭了数据库,所以我不知道问题所在。
答案 0 :(得分:1)
要从checkAvailability
返回,然后再关闭数据库。从函数返回后,不会执行return语句之后的语句。控制流返回到函数的调用者。
bool Database::checkAvailability (std::string& input) {
openDb (dir);
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2 (db, "SELECT * FROM Books WHERE Title = ? OR Isbn = ?", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
std::cout << sqlite3_errmsg (db);
closeDb ();
return false;
}
sqlite3_bind_text (stmt, 1, input.c_str (), input.length (), NULL);
sqlite3_bind_text (stmt, 2, input.c_str (), input.length (), NULL);
if (sqlite3_step (stmt) == SQLITE_ROW) {
sqlite3_finalize (stmt);
sqlite3_close (db);
return true;
}
else {
sqlite3_finalize (stmt);
sqlite3_close (db);
return false;
}
}