mysql执行插入操作_C连接MySQL执行插入操作(1)

#include

#include

#include

#include

#include "include/mysql.h"

int main(int argc, char *argv[]){

MYSQL my_connection;

MYSQL_RES *res_ptr;

MYSQL_ROW row;

int res;

char db[20] = "cms_";

char *ids;

char *shell;

char flag;

if( argc!=3){

printf("\n---------------------------------Note:------------------------- \n");

printf("Author: baiyu929\n");//作者

printf("Date: 2013年7月23日\n");//日期

printf("----------------------------------------------------------------- \n\n");

//exit(1);

}

//连接初始化

if(!mysql_init(&my_connection)){

fprintf(stderr, "数据库初始化失败!\n");

return EXIT_FAILURE;

}

if (mysql_real_connect(&my_connection, "localhost", "root", "123456","html_db",0,NULL,0)){

printf("数据库连接成功!\n");

strcat(db, argv[1]);

mysql_select_db(&my_connection, db);

printf("Connection success\n");

mysql_query(&my_connection, "set names gbk");

res= mysql_query(&my_connection, argv[2]);

if(res){

printf("SELECT error:%s\n",mysql_error(&my_connection));

}else{

res_ptr= mysql_store_result(&my_connection);

if(res_ptr){

printf("Retrieved %lu Rows\n", (unsigned long)mysql_num_rows(res_ptr));

ids= malloc(1);

*ids = '\0';

while(row= mysql_fetch_row(res_ptr)){

printf("%s\n", row[5]);

ids= realloc(ids, strlen(ids)+ strlen(" ")+1);

strcat(ids, " ");

ids= realloc(ids, strlen(ids)+ strlen(row[0])+1);

strcat(ids, row[0]);

}

}

mysql_free_result(res_ptr);

}

mysql_close(&my_connection);

}

shell= malloc(100 + strlen(argv[1]) + strlen(ids));

sprintf(shell,"/usr/local/bin/php /www/titancms/cron/publish_%s.php -a%s", argv[1], ids);

free(ids);

printf("您要执行的是:%s\n",shell);

printf("请检查命令是否正确? (y/n)\n");

scanf("%s",&flag);

if(flag=='y'){

system(shell);

}

free(shell);

return EXIT_SUCCESS;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值