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.
- Create table demotable (
- Id int (11) not null auto_increment,
- Demodata varchar (255) default NULL,
- Primary key (id)
- ) TYPE = MyISAM;
-
- ---------- Insert data to the data table and query the output ----------
- # Include <mysql. h>/* Headers for MySQL usage */
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
-
- Int main (int argc, char ** argv ){
- MYSQL demo_db;
- Mysql_init (& demo_db );
-
- Int insert_id;
- Char * encdata, * query;
- Int datasize;
- MYSQL_RES * res;/* To be used to fetch information */
- MYSQL_ROW row;
-
- If (argc <2 ){
- Printf ("Please supply a string for insertion into the database \ n ");
- Exit (0 );
- }
-
- If (! Mysql_real_connect (& demo_db, "localhost", "root", "mysql", "demodb", 0, NULL, 0 )){
- Printf (mysql_error (& demo_db ));
- Exit (1 );
- }
-
- // If (mysql_select_db (& demo_db, "demodb") {/* Select the database we want to use */
- // Printf (mysql_error (& demo_db ));
- // Exit (1 );
- //}
-
- Encdata = malloc (2 * strlen (argv [1]) + 1 );
-
- Datasize = mysql_real_escape_string (& demo_db, encdata, argv [1], strlen (argv [1]);
- // Printf ("% s \ n", encdata );
-
- Query = malloc (datasize + 255 );
- Sprintf (query, "insert into demotable (demodata) VALUES ('% s')", encdata);/* Build query */
- // Printf ("% s \ n", query );
-
- If (mysql_real_query (& demo_db, query, strlen (query) {/* Make query */
- Printf (mysql_error (& demo_db ));
- Exit (1 );
- }
- Free (query );
-
- Insert_id = mysql_insert_id (& demo_db);/* Find what id that data was given */
-
- Query = malloc (255 );
- Sprintf (query, "SELECT demodata FROM demotable WHERE id = % d", insert_id );
- If (mysql_real_query (& demo_db, query, strlen (query) {/* Make query */
- Printf (mysql_error (& demo_db ));
- Exit (1 );
- }
- Free (query );
-
- Res = mysql_store_result (& demo_db);/* Download result from server */
- Row = mysql_fetch_row (res);/* Get a row from the results */
- Printf ("You inserted \" % s \ ". \ n", row [0]);
- Mysql_free_result (res);/* Release memory used to store results .*/
- Mysql_close (& demo_db );
-
- Return 0;
- }
-
-
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