Guest User

Untitled

a guest
Aug 28th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <mysql.h>
  2. #include <my_global.h>
  3. #include <string.h>
  4. int main(int argc, char **argv)
  5. {
  6. MYSQL *conn;
  7. MYSQL_RES *result;
  8. MYSQL_ROW row;
  9. MYSQL_FIELD *field;
  10. int num_fields;
  11. int i;
  12. /* Change me */
  13. char *server = "localhost";
  14. char *user = "root";
  15. char *password = "sadi";
  16. char *database = "testdb";
  17. char Title[100];
  18. char Author[60];
  19. char query[150];
  20. int Id;
  21. conn = mysql_init(NULL);
  22.  
  23. /* Connect to database */
  24. if (!mysql_real_connect(conn, server,
  25. user, password,database,0, NULL, 0)) {
  26. fprintf(stderr, "%s\n", mysql_error(conn));
  27. exit(1);
  28. }
  29.  
  30. scanf("%d",&Id);
  31. printf("title: ");
  32. scanf("%s",Title);
  33. printf("Author: ");
  34. scanf("%s",Author);
  35. int quantity;
  36. scanf("%d",&quantity);
  37. sprintf(query,"INSERT INTO Books values(%d,'%s','%s',%d)",Id,Title,Author,quantity);
  38. printf("%s",query);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. mysql_query(conn, query);
  46.  
  47.  
  48.  
  49. result= mysql_use_result(conn);
  50.  
  51. /*output table name*/
  52. mysql_query(conn, "SELECT * FROM Books");
  53. result = mysql_store_result(conn);
  54.  
  55. num_fields = mysql_num_fields(result);
  56. while ((row = mysql_fetch_row(result)))
  57. {
  58. for(i = 0; i < num_fields; i++)
  59. {
  60. if (i == 0) {
  61. while(field = mysql_fetch_field(result)) {
  62. printf("%s\t", field->name);
  63. }
  64. printf("\n");
  65. }
  66. printf("%s\t", row[i] ? row[i] : "NULL");
  67. }
  68. }
  69. printf("\n");
  70.  
  71. /* close connection */
  72. mysql_free_result(result);
  73. mysql_close(conn);
  74.  
  75. return 0;
  76. }
Add Comment
Please, Sign In to add comment