您的位置:首页 > 运维架构 > Shell

shell、C中mysql插入数据语句

2016-04-02 00:00 507 查看
#!/bin/bash

HOSTNAME="192.168.209.43"

PORT="3306"

USERNAME="root"

PASSWORD="123456"

DBNAME="operation_monitor"

TABLENAME="error_log"

create_db_sql="create database IF NOT EXISTS ${DBNAME}"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${c reate_db_sql}"

insert_sql="insert into ${TABLENAME} values('billchen',2)"

mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${in sert_sql}"

MYSQL mysql,*sock;
mysql_init(&mysql);
if ( !(sock=mysql_real_connect( &mysql,"127.0.0.1","root","123456","operation_monitor",3306,NULL,0)))
{
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
printf(" connect mysql error!\n");
exit(1);
}
sprintf(record_day_time,"%d%d",record_time,record_time_day);
sprintf( ibuf,"insert into error_log(id,function_name,description,record_time) values('','%s','%s','%s')",function_name,description,record_day_time);
//exit(0);
if ( mysql_query(sock,ibuf))
{
printf("insert data error!i No.%d\n",lines);
}
else
{
;
}
mysql_close(&mysql);
mysql_close(sock);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C shell使用数据库