C++完成Query执行sql语句的接口封装和测试

1、在LXMysql.h 创建Query执行函数

cpp 复制代码
//封装 执行sql语句  if sqllen =0 strlen获取字符长度
	bool Query(const char*sql,unsigned long sqllen=0);

2、在LXMysql.cpp编写函数

cpp 复制代码
bool LXMysql::Query(const char* sql, unsigned long sqllen)
	{
		if (!mysql)//如果mysql没有初始化好
		{
			cerr << "Query failed: mysql is NULL" << endl;
			return false;
		}
        //如果sql语句是空的
		if (sql == NULL)
		{
			cerr << "sql is NULL" << endl;
			return false;
		}
        //如果sql语句长度为0
		if (sqllen <= 0)
		{
			sqllen = (unsigned long)strlen(sql);//强转
		}
        //如果还是<=0,就已经错啦
		if (sqllen <= 0)
		{
			cerr << "Query sql is empty or wrong format!" << endl;
		}
		int re = mysql_real_query(mysql, sql, sqllen);
		if (re != 0)
		{
			cerr << "mysql_real_query fqiled!" << mysql_error(mysql) << endl;
			return false;
		}
		return true;
	}

3、测试,创建一个表格,执行之后查看是否创建成功

cpp 复制代码
 string sql = "";
    sql = "CREATE TABLE IF NOT EXISTS `t_vedio`\
          (id INT AUTO_INCREMENT,\
          name varchar(1024),\
          data BLOB,\
          size INT,\
          PRIMARY KEY(id))";
    cout << my.Query(sql.c_str()) << endl;

4、执行,发现输出为1,说明创建成功

相关推荐
Want5953 分钟前
C/C++圣诞树①
c语言·开发语言·c++
老赵的博客14 分钟前
c++ 杂记
开发语言·c++
jimmy.hua17 分钟前
[C++刷怪笼]:set/map--优质且易操作的容器
开发语言·c++
tan180°29 分钟前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
Kt&Rs38 分钟前
MySQL复制技术的发展历程
数据库·mysql
小小菜鸡ing41 分钟前
pymysql
java·服务器·数据库
手握风云-1 小时前
MySQL数据库精研之旅第十六期:深度拆解事务核心(上)
数据库·mysql
bkspiderx1 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
w2sfot1 小时前
Passing Arguments as an Object in JavaScript
开发语言·javascript·ecmascript
郝学胜-神的一滴2 小时前
避免使用非const全局变量:C++中的最佳实践 (C++ Core Guidelines)
开发语言·c++·程序人生