MySQL数据表中插入数据并查询输出的实现

数据库 MySQL
插入数据是MySQL中最基本的操作之一,下文就为您介绍MySQL数据表中插入数据并查询输出的实现方法,供您参考学习之用。

MySQL数据表中插入数据是我们很常见的操作,下面就为您详细介绍MySQL数据表中插入数据并查询输出的实现方法步骤,如果您对MySQL数据表方面感兴趣的话,不妨一看。

  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. ----------往数据表中插入数据并查询输出----------  
  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 into */   
  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.  

 

 

 

【编辑推荐】

显示MYSQL表信息的方法

深度解析MySQL创建关联表

MySQL MyISAM表结构的恢复

mysql数据库大小写的问题讨论

MySQL表别名的另类用法

责任编辑:段燃 来源: 互联网
相关推荐

2010-11-22 13:53:46

MySQL数据表

2010-06-09 16:55:47

MySQL数据表

2010-11-24 13:11:06

MySQL遍历数据表

2010-05-18 17:17:02

MySQL数据表

2011-07-07 10:41:07

php批量删除

2010-05-12 18:35:25

MySQL数据表

2017-05-25 10:23:13

数据a表b表

2010-11-24 10:52:57

Mysql字符集

2010-06-13 17:35:17

MySQL数据表

2010-08-04 11:03:03

DB2数据表

2011-04-08 11:38:37

access数据自动联接

2017-09-11 14:50:55

MySQL数据表类型存储引擎

2011-03-15 09:15:06

MyisamchkMySQL

2011-03-15 14:36:04

MyisamchkMySQL数据表

2023-05-26 16:34:31

HBase数据模型

2011-04-14 11:19:14

MySQL

2010-10-15 11:05:31

MYSQL查询结果

2020-11-18 07:51:15

MySQL数据查询

2022-12-28 08:17:36

数据库数据导出

2009-08-11 14:51:47

C#读取Excel中数
点赞
收藏

51CTO技术栈公众号