自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢(xún)輸出的實(shí)現(xiàn)

數(shù)據(jù)庫(kù) MySQL
插入數(shù)據(jù)是MySQL中最基本的操作之一,下文就為您介紹MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢(xún)輸出的實(shí)現(xiàn)方法,供您參考學(xué)習(xí)之用。

MySQL數(shù)據(jù)表中插入數(shù)據(jù)是我們很常見(jiàn)的操作,下面就為您詳細(xì)介紹MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢(xún)輸出的實(shí)現(xiàn)方法步驟,如果您對(duì)MySQL數(shù)據(jù)表方面感興趣的話,不妨一看。

  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. ----------往數(shù)據(jù)表中插入數(shù)據(jù)并查詢(xún)輸出----------  
  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創(chuàng)建關(guān)聯(lián)表

MySQL MyISAM表結(jié)構(gòu)的恢復(fù)

mysql數(shù)據(jù)庫(kù)大小寫(xiě)的問(wèn)題討論

MySQL表別名的另類(lèi)用法

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-11-22 13:53:46

MySQL數(shù)據(jù)表

2010-06-09 16:55:47

MySQL數(shù)據(jù)表

2010-11-24 13:11:06

MySQL遍歷數(shù)據(jù)表

2010-05-18 17:17:02

MySQL數(shù)據(jù)表

2011-07-07 10:41:07

php批量刪除

2017-05-25 10:23:13

數(shù)據(jù)a表b表

2010-05-12 18:35:25

MySQL數(shù)據(jù)表

2010-06-13 17:35:17

MySQL數(shù)據(jù)表

2010-08-04 11:03:03

DB2數(shù)據(jù)表

2010-11-24 10:52:57

Mysql字符集

2011-04-08 11:38:37

access數(shù)據(jù)自動(dòng)聯(lián)接

2011-03-15 14:36:04

MyisamchkMySQL數(shù)據(jù)表

2023-05-26 16:34:31

HBase數(shù)據(jù)模型

2017-09-11 14:50:55

MySQL數(shù)據(jù)表類(lèi)型存儲(chǔ)引擎

2011-03-15 09:15:06

MyisamchkMySQL

2009-09-07 16:13:14

LINQ to SQL

2022-12-28 08:17:36

數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)出

2011-04-14 11:19:14

MySQL

2010-10-15 11:05:31

MYSQL查詢(xún)結(jié)果

2020-11-18 07:51:15

MySQL數(shù)據(jù)查詢(xún)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)