MySQL 之数据添加
/*练习mysql数据库的查询*/
#include
#include
#include "/usr/local/mysql/include/mysql.h"
int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int flag, t;
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) {
printf("Failed to connect to Mysql!\n");
return 0;
}else {
printf("Connected to Mysql successfully!\n");
}
query = "select * from student";
/*查询,成功则返回0*/
flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));
if(flag) {
printf("Query failed!\n");
return 0;
}else {
printf("[%s] made...\n", query);
}
/*mysql_store_result讲全部的查询结果读取到客户端*/
res = mysql_store_result(&mysql);
/*mysql_fetch_row检索结果集的下一行*/
while(row = mysql_fetch_row(res)) {
/*mysql_num_fields返回结果集中的字段数目*/
for(t=0; t
执行:./mysqlselect
结果展示:
[root@iZ94qvmp90hZ c]# gcc -o mysqlselect -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlselect.c
[root@iZ94qvmp90hZ c]# ls -a
. a.out hello.c mysqlselect.c testmysql.c
.. datatype.c mysqlselect testmysql testmysql.o
[root@iZ94qvmp90hZ c]# ./mysqlselect
Connected to Mysql successfully!
[select * from student] made...
1 1
2 2
3 3
/*练习mysql数据库的数据添加*/
#include
#include
#include "/usr/local/mysql/include/mysql.h"
int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int flag, t;
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) {
printf("Failed to connect to Mysql!\n");
return 0;
}else {
printf("Connected to Mysql successfully!\n");
}
query = "insert into student(id,name) values(4,'5')";
/*插入,成功则返回0*/
flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));
if(flag) {
printf("Insert data failure!\n");
return 0;
}else {
printf("Insert data success!\n");
}
mysql_close(&mysql);
return 0;
}
执行:./mysqlinsert
结果展示:
[root@iZ94qvmp90hZ c]# gcc -o mysqlinsert -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqlinsert.c
[root@iZ94qvmp90hZ c]# ls -a
. a.out hello.c mysqlinsert.c mysqlselect.c testmysql.c
.. datatype.c mysqlinsert mysqlselect testmysql testmysql.o
[root@iZ94qvmp90hZ c]# ./mysqlinsert
Connected to Mysql successfully!
Insert data success!
MySQL 之删除
/*练习mysql数据库的删除*/
#include
#include
#include "/usr/local/mysql/include/mysql.h"
int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int flag, t;
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 0, NULL, 0)) {
printf("Failed to connect to Mysql!\n");
return 0;
}else {
printf("Connected to Mysql successfully!\n");
}
query = "delete from student where log_id=1";
/*删除,成功则返回0*/
flag = mysql_real_query(&mysql, query, (unsigned int)strlen(query));
if(flag) {
printf("Delete data failure!\n");
return 0;
}else {
printf("Delete data success!\n");
}
mysql_close(&mysql);
return 0;
}
执行:./mysqldelete
执行结果:
[root@iZ94qvmp90hZ c]# gcc -o mysqldelete -I /usr/local/include/mysql/ -L /usr/local/mysql/lib -lmysqlclient mysqldelete.c
[root@iZ94qvmp90hZ c]# ./mysqldelete
Connected to Mysql successfully!
Delete data success!