新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢輸出的實(shí)現(xiàn)
MySQL數(shù)據(jù)表中插入數(shù)據(jù)是我們很常見(jiàn)的操作,下面就為您詳細(xì)介紹MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢輸出的實(shí)現(xiàn)方法步驟,如果您對(duì)MySQL數(shù)據(jù)表方面感興趣的話,不妨一看。

創(chuàng)新互聯(lián)是專業(yè)的饒平網(wǎng)站建設(shè)公司,饒平接單;提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行饒平網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
- CREATE TABLE demotable (
- id int(11) NOT NULL auto_increment,
- demodata varchar(255) default NULL,
- PRIMARY KEY (id)
- ) TYPE=MyISAM;
- ----------往數(shù)據(jù)表中插入數(shù)據(jù)并查詢輸出----------
- #include
/* Headers for MySQL usage */ - #include
- #include
- #include
- 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 into */
- 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;
- }
文章名稱:MySQL數(shù)據(jù)表中插入數(shù)據(jù)并查詢輸出的實(shí)現(xiàn)
標(biāo)題來(lái)源:http://www.dlmjj.cn/article/cocejee.html


咨詢
建站咨詢
