数据库语句

发布于:2025-03-11 ⋅ 阅读:(113) ⋅ 点赞:(0)

环境变量path下的目录是系统目录。

image.png

image.png

#include <iostream>
#include <mysql.h> 
#pragma comment(lib,"libmysql.lib")//链接libmysql.dll动态库的中间桥
//
MYSQL* conn;//数据库句柄。后面还有网络句柄(用来网络收发数据)
bool connect()
{
	 conn = mysql_init(NULL);
	if (conn == NULL)
	{
		printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
		return false;
	}
	//host参数可以用:"localhost",".","192.168.0.88","127.0.0.1","teacher"
	if (mysql_real_connect(conn, "127.0.0.1", "root", "123456", "worker", 0, NULL, 0) == NULL)
	{
		printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
		return false;
	}
	mysql_query(conn, "set names gb2312");
	return true;
}


void Browse()
{
	MYSQL_ROW row;//行数据.内部是一个指针数组cha**
	MYSQL_RES* res;//结果集:含有多个记录(行数据)
	int t;
	const char* allData = "select * from t_worker";
	mysql_query(conn, allData);

	res = mysql_store_result(conn); //
	while (row = mysql_fetch_row(res))
	{
		for (t = 0; t < mysql_num_fields(res); t++)
		{
			printf("%s\t", row[t]);
		}
		printf("\n");
	}

}
int main()
{
	if (!connect())
		return -1;
	const char* sSQL = "DELETE FROM t_worker WHERE f_numb=1088";
	int n = mysql_query(conn, sSQL);//0代表成功
	if(n)
		printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
	Browse();
	return 0;
}

o(conn), mysql_error(conn));
Browse();
return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到