SQLiteCpp icon indicating copy to clipboard operation
SQLiteCpp copied to clipboard

How to get the records count ?

Open pengweichu opened this issue 6 years ago • 5 comments

How to obtain the records count ?

	std::string sql = "SELECT COUNT(*) FROM ";
	sql += DB_KEY_TAB;
	sql += " WHERE uuid = '";
	sql += uuid;
	sql += "';";

pengweichu avatar Apr 27 '18 10:04 pengweichu

@pengweichu you can do it like this:

string count = db.execAndGet("SELECT COUNT(*) FROM test");
cout << "COUNT(*) = "<< count << '\n';

tiendq avatar Nov 23 '18 06:11 tiendq

@tiendq When I add conditionals to the COUNT(*), it always returns zero..

` SQLite::Statement query(db, "SELECT COUNT(*) FROM room_cache WHERE room_path = :room_path");

	// Bind the integer value 6 to the first parameter of the SQL query
	query.bind(":room_path", room_path);
            
            std::string count = db.execAndGet(query.getQuery());
            std::cout << "COUNT(*) = "<< count << '\n';`

KennethThompson avatar Nov 25 '18 05:11 KennethThompson

@CalielOfSeptem did you look at what query.getQuery() return? :)

You need something like this:

while (query.executeStep())
    cout << "COUNT(*) " << query.getColumn(0) << '\n';

tiendq avatar Nov 26 '18 07:11 tiendq

@KennethThompson Have you slove this issue? I've met this, too. Both execAndGet and executeStep have I tried, but both of them return 0 (my table has 1 data). Hope for you reply:)

416207298 avatar Jun 19 '24 03:06 416207298

@KennethThompson Have you slove this issue? I've met this, too. Both execAndGet and executeStep have I tried, but both of them return 0 (my table has 1 data). Hope for you reply:)

My fault, I just figure out I use the wrong db file:(

416207298 avatar Jun 19 '24 05:06 416207298