#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysql.h"
int main() {
MYSQL conn;
int res;
MYSQL_RES * result;
MYSQL_ROW row;
mysql_init(&conn);
if (mysql_real_connect(&conn, "***.***.***.***", "usre_name", "password", "DataBase_name", port, NULL, 0)) {
printf("coneect mysql successful\n请选择功能:\n");
int choose;
char str1[20], str2[20];
char insert_query[80];
char delete_query[50] = "delete from age where name='";
char select_query[] = "select * from age";
printf("1.insert\n2.select\n3.delete\n");
while (scanf("%d", &choose)) {
switch (choose) {
case 1:
printf("姓名 年龄:\n");
memset(insert_query, 0, sizeof(insert_query));
//insert
scanf("%s %s", str1, str2);
strcat(insert_query, "insert into age values('");
strcat(insert_query, str1);
strcat(insert_query, "',");
strcat(insert_query, str2);
strcat(insert_query, ")");
printf("SQL语句: %s\n", insert_query);
res = mysql_query(&conn, insert_query);
if (!res) {
printf("insert %lu rows\n", (unsigned long)mysql_affected_rows(&conn));
}
else {
printf("insert error\n");
}
break;
case 2:
//select
printf("SQL语句: %s\n", select_query);
if (mysql_query(&conn, select_query) != 0) {
fprintf(stderr, "查询失败!\n");
exit(1);
}
else {
if ((result = mysql_store_result(&conn)) == NULL) {
fprintf(stderr, "保存结果集失败!\n");
exit(1);
}
else {
while ((row = mysql_fetch_row(result)) != NULL) {
printf("name is %s , ", row[0]);
printf("age is %s\n", row[1]);
}
}
}
break;
case 3:
printf("姓名:\n");
scanf("%s", str1);
strcat(delete_query, str1);
strcat(delete_query, "'");
//delete
printf("SQL语句: %s\n", delete_query);
res = mysql_real_query(&conn, delete_query, (unsigned int)strlen(delete_query));
if (!res) {
printf("delete successful\n");
}
else {
printf("delete error\n");
}
break;
}
}
mysql_close(&conn);
}
return 0;
}
————————————————
版权声明:本文为CSDN博主「南城忆一指流砂」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38422690/article/details/78660454
评论