操作数据库的时候经常会用到更新数据的操作,以下是更新数据库的实例:
void _UpdateData()
{
Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("test.db", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);
//获取当前数据表中的数据
{
pStmt->Sql("SELECT * FROM user");
while (pStmt->FetchRow())
{
int nData = pStmt->GetColumnInt(0);
std::string str1 = pStmt->GetColumnString(1);
std::string str2 = pStmt->GetColumnString(2);
int nData1 = pStmt->GetColumnInt(3);
double db1 = pStmt->GetColumnDouble(4);
int yyyy = 66;
}
pStmt->FreeQuery();
}
//修改表中的数据
{
pStmt->Sql("UPDATE user SET lastName=@lastName, age=@age WHERE userID=@userID");
pStmt->BindString(1, "Urushihara");
pStmt->BindInt(2, 56);
pStmt->BindInt(3, 2);
pStmt->ExecuteAndFree();
}
{
pStmt->Sql("SELECT * FROM user");
while (pStmt->FetchRow())
{
int nData = pStmt->GetColumnInt(0);
std::string str1 = pStmt->GetColumnString(1);
std::string str2 = pStmt->GetColumnString(2);
int nData1 = pStmt->GetColumnInt(3);
double db1 = pStmt->GetColumnDouble(4);
int yyyy = 66;
}
pStmt->FreeQuery();
}
pDatabase->Close();
}