我有一个SQLite
数据库设置在我的C++
应用程序,
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
。
我发现了问题,我正在传递路径作为参数,在那里我调用UpdateSampleType()
而不是文件名。