【发布时间】:2021-06-08 17:27:14
【问题描述】:
我的C++ 应用程序中有一个SQLite 数据库设置,
Database::Database()
{
/* Create SQL statement */
std::string sample = "CREATE TABLE IF NOT EXISTS SAMPLES("
"FAVORITE INT NOT NULL,"
"FILENAME TEXT NOT NULL,"
"SAMPLEPACK TEXT NOT NULL,"
"CHANNELS INT NOT NULL,"
"LENGTH INT NOT NULL,"
"SAMPLERATE INT NOT NULL,"
"BITRATE INT NOT NULL,"
"BITSPERSAMPLE INT NOT NULL,"
"PATH TEXT NOT NULL,"
"TYPE TEXT NOT NULL);";
try
{
rc = sqlite3_open("Samples.db", &DB);
rc = sqlite3_exec(DB, sample.c_str(), NULL, 0, &ErrorMessage);
if (rc != SQLITE_OK)
{
msgDialog = new wxMessageDialog(NULL, "Error! Cannot create table.", "Error", wxOK | wxICON_ERROR);
msgDialog->ShowModal();
sqlite3_free(ErrorMessage);
}
else
{
wxLogDebug("Table created successfully.");
}
sqlite3_close(DB);
}
catch (const std::exception &exception)
{
wxLogDebug(exception.what());
}
}
我有一个向其中插入数据的函数,
void Database::InsertSample(int Favorite, std::string Filename,
std::string SamplePack, int Channels, int Length,
int SampleRate, int Bitrate, std::string Comment,
std::string Path, std::string Type)
{
try
{
rc = sqlite3_open("Samples.db", &DB);
std::string insert = "INSERT INTO SAMPLES (FAVORITE, FILENAME, SAMPLEPACK, CHANNELS, \
LENGTH, SAMPLERATE, BITRATE, BITSPERSAMPLE, PATH, TYPE) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
rc = sqlite3_prepare_v2(DB, insert.c_str(), insert.size(), &stmt, NULL);
rc = sqlite3_bind_int(stmt, 1, Favorite);
rc = sqlite3_bind_text(stmt, 2, Filename.c_str(), Filename.size(), SQLITE_STATIC);
rc = sqlite3_bind_text(stmt, 3, SamplePack.c_str(), SamplePack.size(), SQLITE_STATIC);
rc = sqlite3_bind_int(stmt, 4, Channels);
rc = sqlite3_bind_int(stmt, 5, Length);
rc = sqlite3_bind_int(stmt, 6, SampleRate);
rc = sqlite3_bind_int(stmt, 7, Bitrate);
rc = sqlite3_bind_text(stmt, 8, Comment.c_str(), Comment.size(), SQLITE_STATIC);
rc = sqlite3_bind_text(stmt, 9, Path.c_str(), Path.size(), SQLITE_STATIC);
rc = sqlite3_bind_text(stmt, 10, Type.c_str(), Type.size(), SQLITE_STATIC);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
wxLogWarning("No data inserted.");
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK)
{
msgDialog = new wxMessageDialog(NULL, "Error! Cannot insert data into table.", "Error", wxOK | wxICON_ERROR);
msgDialog->ShowModal();
sqlite3_free(ErrorMessage);
}
else
{
wxLogInfo("Data inserted successfully. %s", ErrorMessage);
}
sqlite3_close(DB);
}
catch (const std::exception &exception)
{
wxLogDebug(exception.what());
}
}
我这样称呼它,
db.InsertSample(0, Filename.ToStdString(), Artist.to8Bit(true),
Channels, Length, SampleRate, Bitrate,
Comment.to8Bit(true), Path.ToStdString(), "Sample");
数据库工作正常,我还有其他删除和删除功能。但是今天我给它加了一列TYPE,并在里面插入了"Sample",只是为了测试,还有这样的更新功能,
void Database::UpdateSampleType(std::string& filename, std::string type)
{
try
{
rc = sqlite3_open("Samples.db", &DB);
std::string select = "UPDATE SAMPLES SET TYPE = ? WHERE FILENAME = ?;";
rc = sqlite3_prepare_v2(DB, select.c_str(), select.size(), &stmt, NULL);
rc = sqlite3_bind_text(stmt, 1, type.c_str(), type.size(), SQLITE_STATIC);
rc = sqlite3_bind_text(stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
wxLogDebug("Record found, updating..");
}
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK)
{
msgDialog = new wxMessageDialog(NULL, "Error! Cannot update record.", "Error", wxOK | wxICON_ERROR);
msgDialog->ShowModal();
sqlite3_free(ErrorMessage);
}
else
{
wxLogDebug("Updated record successfully.");
}
sqlite3_close(DB);
}
catch (const std::exception &exception)
{
wxLogDebug(exception.what());
}
}
我得到的输出为Updated record successfully,但如果我尝试在sqlitebrowser 中打开数据库,它仍然显示为Sample。
【问题讨论】:
-
您应该添加更多错误检查。
-
我添加了检查
if rc == SQLITE_BUSY,SQLITE_ABORT,SQLITE_NOMEM,SQLITE_LOCKED,SQLITE_IOERR,SQLITE_CORRUPT,SQLITE_READONLY,SQLITE_ERROR,SQLITE_INTERNAL, @,仍然只有Updated record successfully打印。