How to insert and query data in a MySQL DATA table

Source: Internet
Author: User

Inserting data in a MySQL DATA table is a common operation. The following describes how to insert data in a MySQL DATA table and query and output the data, if you are interested in MySQL Data Tables, take a look.

 
 
  1. Create table demotable (
  2. Id int (11) not null auto_increment,
  3. Demodata varchar (255) default NULL,
  4. Primary key (id)
  5. ) TYPE = MyISAM;
  6.  
  7. ---------- Insert data to the data table and query the output ----------
  8. # Include <mysql. h>/* Headers for MySQL usage */
  9. # Include <stdio. h>
  10. # Include <stdlib. h>
  11. # Include <string. h>
  12.  
  13. Int main (int argc, char ** argv ){
  14. MYSQL demo_db;
  15. Mysql_init (& demo_db );
  16.  
  17. Int insert_id;
  18. Char * encdata, * query;
  19. Int datasize;
  20. MYSQL_RES * res;/* To be used to fetch information */
  21. MYSQL_ROW row;
  22.  
  23. If (argc <2 ){
  24. Printf ("Please supply a string for insertion into the database \ n ");
  25. Exit (0 );
  26. }
  27.  
  28. If (! Mysql_real_connect (& demo_db, "localhost", "root", "mysql", "demodb", 0, NULL, 0 )){
  29. Printf (mysql_error (& demo_db ));
  30. Exit (1 );
  31. }
  32.  
  33. // If (mysql_select_db (& demo_db, "demodb") {/* Select the database we want to use */
  34. // Printf (mysql_error (& demo_db ));
  35. // Exit (1 );
  36. //}
  37.  
  38. Encdata = malloc (2 * strlen (argv [1]) + 1 );
  39.  
  40. Datasize = mysql_real_escape_string (& demo_db, encdata, argv [1], strlen (argv [1]);
  41. // Printf ("% s \ n", encdata );
  42.  
  43. Query = malloc (datasize + 255 );
  44. Sprintf (query, "insert into demotable (demodata) VALUES ('% s')", encdata);/* Build query */
  45. // Printf ("% s \ n", query );
  46.  
  47. If (mysql_real_query (& demo_db, query, strlen (query) {/* Make query */
  48. Printf (mysql_error (& demo_db ));
  49. Exit (1 );
  50. }
  51. Free (query );
  52.  
  53. Insert_id = mysql_insert_id (& demo_db);/* Find what id that data was given */
  54.  
  55. Query = malloc (255 );
  56. Sprintf (query, "SELECT demodata FROM demotable WHERE id = % d", insert_id );
  57. If (mysql_real_query (& demo_db, query, strlen (query) {/* Make query */
  58. Printf (mysql_error (& demo_db ));
  59. Exit (1 );
  60. }
  61. Free (query );
  62.  
  63. Res = mysql_store_result (& demo_db);/* Download result from server */
  64. Row = mysql_fetch_row (res);/* Get a row from the results */
  65. Printf ("You inserted \" % s \ ". \ n", row [0]);
  66. Mysql_free_result (res);/* Release memory used to store results .*/
  67. Mysql_close (& demo_db );
  68.  
  69. Return 0;
  70. }
  71.  
  72.  

How to display MYSQL table information

In-depth parsing MySQL Association Table Creation

Restoration of MySQL MyISAM Table Structure

Case sensitivity of mysql Databases

Alternative usage of MySQL table alias

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.