直接上代码
#include <WCDB/WCDB.h>
#include <vector>
#include <string>
// Example structure for your table record
struct MyRecord {
int id;
std::string name;
int value;
};
// Update multiple rows in a table using WCDB in C++
bool updateMultiRows(
WCDB::Database& database,
const std::string& tableName,
const std::vector<MyRecord>& records
) {
bool success = true;
database.runTransaction([&](WCDB::Handle& handle) -> bool {
for (const auto& record : records) {
// Update value by id
WCDB::StatementUpdate statement;
statement.table(tableName)
.set(WCDB::Column("value"), record.value)
.where(WCDB::Column("id") == record.id);
if (!handle.execute(statement)) {
success = false;
break;
}
}
return success;
});
return success;
}
// Usage example
/*
WCDB::Database db("path_to_db.sqlite");
std::vector<MyRecord> records = {
{1, "Alice", 100},
{2, "Bob", 200},
{3, "Charlie", 300}
};
bool result = updateMultiRows(db, "my_table", records);
*/
另一处测试代码
#include <WCDB/WCDBCpp.h>
#include <vector>
#include <string>
class DBHelper {
public:
DBHelper(const std::string& path) : database(path) {
database.createTable<TableRow>("table", WCDB_FIELD(TableRow::id), WCDB_FIELD(TableRow::value));
}
struct TableRow {
int id;
std::string value;
WCDB_DECLARE_FIELD(id);
WCDB_DECLARE_FIELD(value);
WCDB_DECLARE_TABLE_TYPE(TableRow);
};
bool updateMultiRows(const std::vector<TableRow>& rows) {
WCDB::Column idColumn("id");
WCDB::Column valueColumn("value");
WCDB::StatementUpdate updateStmt;
try {
database.runTransaction([&]() -> bool {
for (const auto& row : rows) {
updateStmt.table("table")
.set(valueColumn, row.value)
.where(idColumn == row.id);
if (!database.execute(updateStmt)) {
return false;
}
updateStmt.clear();
}
return true;
});
} catch (...) {
return false;
}
return true;
}
std::vector<TableRow> getAllRows() {
std::vector<TableRow> result;
database.selectObjects<TableRow>("table", result);
return result;
}
void insertRows(const std::vector<TableRow>& rows) {
database.insertObjects<TableRow>("table", rows);
}
};
gtest代码
#include <gtest/gtest.h>
#include "UpdateMultiRows.cpp"
#include <filesystem>
class UpdateMultiRowsTest : public ::testing::Test {
protected:
std::string dbPath = "test_update_multi_rows.db";
DBHelper* dbHelper;
void SetUp() override {
if (std::filesystem::exists(dbPath)) {
std::filesystem::remove(dbPath);
}
dbHelper = new DBHelper(dbPath);
std::vector<DBHelper::TableRow> initRows = {
{1, "A"},
{2, "B"},
{3, "C"}
};
dbHelper->insertRows(initRows);
}
void TearDown() override {
delete dbHelper;
if (std::filesystem::exists(dbPath)) {
std::filesystem::remove(dbPath);
}
}
};
TEST_F(UpdateMultiRowsTest, UpdateMultipleRows) {
std::vector<DBHelper::TableRow> updateRows = {
{1, "X"},
{2, "Y"},
{3, "Z"}
};
ASSERT_TRUE(dbHelper->updateMultiRows(updateRows));
auto result = dbHelper->getAllRows();
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0].value, "X");
EXPECT_EQ(result[1].value, "Y");
EXPECT_EQ(result[2].value, "Z");
}
cmake_minimum_required(VERSION 3.14)
project(UpdateMultiRowsTest)
set(CMAKE_CXX_STANDARD 17)
find_package(WCDB REQUIRED)
find_package(GTest REQUIRED)
include_directories(${WCDB_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})
add_executable(UpdateMultiRowsTest
UpdateMultiRows.cpp
UpdateMultiRowsTest.cpp
)
target_link_libraries(UpdateMultiRowsTest
${WCDB_LIBRARIES}
GTest::gtest
GTest::gtest_main
pthread
)
# WCDB Multi Rows Update Function Example
## Prerequisites
- WCDB (https://github.com/Tencent/wcdb)
- GTest
- CMake (>= 3.14)
## Build
```bash
mkdir build
cd build
cmake ..
make
Run Test
./UpdateMultiRowsTest
Files
UpdateMultiRows.cpp: Implements the multi rows update function with WCDB.UpdateMultiRowsTest.cpp: GTest case for the update function.CMakeLists.txt: Build and link configuration.
test
```cpp
````markdown name=wcdb_tutorial.md
# WCDB Library Tutorial with GTest Example
This tutorial shows you how to use the [WCDB](https://github.com/Tencent/wcdb) (WeChat Database) library, a high-performance SQLite wrapper, in a C++ project. We'll cover basic setup, CRUD operations, and how to write unit tests for your database code using [Google Test (GTest)](https://github.com/google/googletest).
---
## Prerequisites
- C++ development environment (CMake recommended)
- WCDB C++ library ([GitHub Repo](https://github.com/Tencent/wcdb))
- Google Test library
- SQLite installed (WCDB wraps SQLite)
---
## 1. Installing WCDB
Clone the WCDB repo and follow its build instructions:
```bash
git clone https://github.com/Tencent/wcdb.git
cd wcdb
mkdir build && cd build
cmake ..
make
sudo make install
Refer to the WCDB docs for platform-specific instructions.
2. Basic Usage Example
Here’s a minimal example of using WCDB in C++:
#include <WCDB/WCDB.h>
#include <iostream>
// Define your model
class Person : public WCDB::TableBinding {
public:
int id;
std::string name;
WCDB_PROPERTY(id)
WCDB_PROPERTY(name)
};
int main() {
// Open a database
WCDB::Database db("test.db");
if (!db.canOpen()) {
std::cerr << "Cannot open database!" << std::endl;
return 1;
}
// Create table
db.createTable<Person>("person");
// Insert data
Person alice;
alice.id = 1;
alice.name = "Alice";
db.insertObject(alice, "person");
// Query data
auto results = db.getObjects<Person>("person");
for (const auto& person : results) {
std::cout << "Person: " << person.id << ", " << person.name << std::endl;
}
return 0;
}
Build with CMake:
cmake_minimum_required(VERSION 3.10)
project(WCDBExample)
find_package(WCDB REQUIRED)
add_executable(example example.cpp)
target_link_libraries(example WCDB::WCDB)
3. Writing a GTest Case for WCDB
Here’s an example GTest unit test for the above database code:
#include <gtest/gtest.h>
#include <WCDB/WCDB.h>
class Person : public WCDB::TableBinding {
public:
int id;
std::string name;
WCDB_PROPERTY(id)
WCDB_PROPERTY(name)
};
TEST(WCDBTest, InsertAndQueryPerson) {
WCDB::Database db("test_gtest.db");
ASSERT_TRUE(db.canOpen());
db.createTable<Person>("person");
Person bob;
bob.id = 2;
bob.name = "Bob";
db.insertObject(bob, "person");
auto results = db.getObjects<Person>("person");
ASSERT_FALSE(results.empty());
EXPECT_EQ(results[0].id, 2);
EXPECT_EQ(results[0].name, "Bob");
}
Build GTest Example:
cmake_minimum_required(VERSION 3.10)
project(WCDBGTest)
find_package(WCDB REQUIRED)
find_package(GTest REQUIRED)
add_executable(test_wcdb test_wcdb.cpp)
target_link_libraries(test_wcdb WCDB::WCDB GTest::GTest GTest::Main)
Run the test:
./test_wcdb
4. Tips
- WCDB provides ORM-like API for easier data mapping.
- For advanced usage (transactions, migration, multi-thread), see WCDB docs.
- Clean up test databases after tests.
5. References
Happy coding with WCDB and GTest!
```

2434

被折叠的 条评论
为什么被折叠?



