simple c++ mysql wrapper
Clone or download
Latest commit 6699b80 Jul 27, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Jan 10, 2018
README.md Update README.md Jul 26, 2018
main.cpp Add files via upload Jul 26, 2018
mysqlWrapper.h Add files via upload Jul 26, 2018

README.md

c++ mysql wrapper

simple c++ mysql wrapper

Usage

#include <iostream>
#include "mysqlWrapper.h"

mysqlWrapper db;

int main() {

	db.connect_uc("localhost", "root", "123456" ).switchDb("db_name");


	mysqlResult res = db.prepare("SELECT * FROM test_table LIMIT 0,1").execute();
	res.next();


	std::cout << res.getString("title");


	//update query
	db.prepare("UPDATE test_table SET title = ? WHERE id = ? ")\
	.setString("test it")\
	.setInt(1)\
	.execute();



	//insert query
	db.prepare("INSERT INTO test_table(title) VALUES(?)")\
	.setString("ttttt")\
	.execute();


	//
	mysqlResult res1 = db.prepare("SELECT title FROM test_table LIMIT 0,10").execute();
	
	int i = 0;
	while( res1.next() ) {

		std::cout << "res1 result " << res1.getString("title") << std::endl;
	

		//test nested query		
		mysqlResult res2 = db.prepare("SELECT title FROM test_table LIMIT ?,1").setInt(i++).execute();
		res2.next();

		std::cout << "res2 result" << res2.getString("title") << std::endl;

	}


	return 0;
}

Build

g++ main.cpp -lmysqlcppconn

Update

  • auto connect added ( if prepare faild )

  • lambda usage added ( find function )

db.prepare("SELECT * FROM test_table LIMIT 0,10").find([]( mysqlResult& res ) {
	std::cout << res.getInt("id") << std::endl;
});