Giter Club home page Giter Club logo

mysql4cpp's Introduction

mysql4cpp

mysql api的c++简单封装

目前实现basic的sql查询和预编译sql的执行

Usage:

创建连接

Database db("db_name", "your_database_password", "localhost", "your_database_userame");
SqlConn conn = db.getConn();
if (!conn.isOpen())
    printf("connect error: %s\n", conn.getError().c_str());

执行普通sql查询

string sql = "select * from user";
ResultSet rs = conn.executeQuery(sql);
if(rs.isValid())
	while (rs.hasNext())
	{
		rs.next();
		printf("id: %d, name: %s\n", rs.getInt(1), rs.getString(2).c_str());
	}


string sql = "update user set create_time = NOW() where name = 'jeff'";
int affected = conn.executeUpdate(sql);
printf("%d rows affected\n", affected);

执行预编译的查询

Statement stmt = conn.prepareStatment("select * from user where name = ? or id = ?");
stmt.setString(1, "jack");
stmt.setInt(2, 7);
ResultSet rs = stmt.executeQuery();
if (rs.isValid())
{
	while (rs.hasNext())
	{
		rs.next();
		int id = rs.getInt("id");
		string name = rs.getString("name");
		printf("id: %d, name: %s\n", id, name.c_str());
	}
}
else
	cout << stmt.getError() << endl;

Statement stmt = conn.prepareStatment("insert into user(name, create_time) values(?, NOW()), (?, NOW()), (?, NOW()), (?, NOW())");
stmt.setString(1, "rose");
stmt.setString(2, "john");
stmt.setString(3, "mary");
stmt.setString(4, "jeff");
int affected = stmt.executeUpdate();
if (affected)
	printf("rows affected: %d\n", affected);
else
	cout << stmt.getError() << endl;

进度

  • 实现basic api封装
  • 实现预编译sql封装
  • 实现日期封装
  • 实现连接池
  • 事务支持

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.