1、在LXMysql.h 创建Query执行函数
//封装 执行sql语句 if sqllen =0 strlen获取字符长度bool Query(const char*sql,unsigned long sqllen=0);
2、在LXMysql.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语句长度为0if (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、测试,创建一个表格,执行之后查看是否创建成功
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,说明创建成功