From 6a00fdf8076537def530e635fbd2a6bbb1827094 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sat, 25 Jul 2015 18:54:06 +0200 Subject: Add lastInsertRowId, minor improvements. --- src/sqlitepp/sqlitepp.cc | 7 ++++++- src/sqlitepp/sqlitepp_test.cc | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sqlitepp/sqlitepp.cc b/src/sqlitepp/sqlitepp.cc index d64b5ba..ef0dfa8 100644 --- a/src/sqlitepp/sqlitepp.cc +++ b/src/sqlitepp/sqlitepp.cc @@ -187,6 +187,11 @@ void Database::execute(const std::string& sql) { statement->step(); } +int Database::lastInsertRowId() const { + requireOpen(); + return sqlite3_last_insert_rowid(m_handle); +} + void Database::open(const std::string& file) { if (isOpen()) { throw std::logic_error("sqlitepp::Database::open(std::string&): " @@ -235,7 +240,7 @@ bool ResultSet::canRead() const { int ResultSet::columnCount() const { m_statement->requireOpen(); m_statement->requireCanRead(); - return sqlite3_column_count(m_statement->m_handle); + return sqlite3_data_count(m_statement->m_handle); } double ResultSet::readDouble(const int column) const { diff --git a/src/sqlitepp/sqlitepp_test.cc b/src/sqlitepp/sqlitepp_test.cc index b7ea5f6..dfa46d4 100644 --- a/src/sqlitepp/sqlitepp_test.cc +++ b/src/sqlitepp/sqlitepp_test.cc @@ -66,10 +66,15 @@ TEST(Database, insert) { statement->bind(":id", 1); statement->bind(2, "test value"); statement->execute(); + const int rowId1 = database.lastInsertRowId(); + EXPECT_NE(0, rowId1); statement->reset(); statement->bind(":id", 2); statement->bind(2, "other value"); statement->execute(); + const int rowId2 = database.lastInsertRowId(); + EXPECT_NE(0, rowId2); + EXPECT_NE(rowId1, rowId2); } TEST(Database, query) { -- cgit v1.2.1