MySQL编程删除某个元组

mysql> use test;

Database changed

mysql> select * from student;

+------+------+

| Id   | name |

+------+------+

|   29 | Mike |

|   12 | Lili |

+------+------+

2 rows in set (0.00 sec)



mysql> quit

Bye

[root@localhost ~]#

源文件mysql.c如下:

 1 #include <stdio.h>

 2 #include <stdlib.h>

 3 #include <string.h>

 4 #include <unistd.h>

 5 #include <sys/types.h>

 6 #include <dirent.h>

 7 #include "/usr/local/mysql/include/mysql.h"

 8 

 9 const char* host = "127.0.0.1";

10 MYSQL* mysql;

11 const char* user = "root";

12 const char* password = "";

13 

14 int main(int argc ,char **argv)

15 {

16         int t, r;

17 

18         mysql = mysql_init(mysql);

19         mysql = mysql_real_connect(mysql, host, user, password, "test", 0, NULL, 0);

20 

21         if (!mysql)

22         {

23                 perror("MySQL connect error!\n");

24                 return EXIT_FAILURE;

25         }

26 

27         if (mysql_query(mysql, "delete from student where Id=29"))  // 删除Id=29的那个元组 28         {

29                 perror("MySQL query error!\n");

30                 return EXIT_FAILURE;

31         }

32 

33         mysql_close(mysql);

34 

35         return 0;

36 }

[root@localhost ~]# gcc mysql.c -g -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient
[root@localhost ~]# ./a.out

mysql> use test;

Database changed

mysql> select * from student;

+------+------+

| Id   | name |

+------+------+

|   12 | Lili |

+------+------+

1 row in set (0.00 sec)



mysql> quit

Bye

[root@localhost ~]#

 

你可能感兴趣的:(mysql)