|
楼主 |
发表于 2005-11-16 16:14:33
|
显示全部楼层
/*
* 头文件
*/
#include <stdio.h>
#include <mysql/mysql.h>
MYSQL *db_handle;
int Connect(void);
int Insert(int style,int type,char *msg_from,char *msg_to);
void Close(void);
/*
* 利用c语言的API连接数据库mipls
*/
#include "conmysql.h"
int Connect(void)
{
MYSQL *mysql;
mysql_init(mysql);
if((db_handle = mysql_real_connect(mysql,"localhost","root","123456","mipls",0,NULL,0))==NULL)
{
fprintf(stderr,"%s\n",mysql_error(db_handle));
mysql_close(db_handle);
return 0;
}
return 1;
}
int Insert(int style,int type,char *msg_from,char *msg_to)
{
char buff[200];
char *insertstring="insert into message(style,type,msg_from,msg_to) values(%d,%d,'%s','%s')";
sprintf(buff,insertstring,style,type,msg_from,msg_to);
insertstring=buff;
printf("the value of insertstring : %s\n",insertstring);
if (mysql_query(db_handle,insertstring)!=0)
{
fprintf(stderr,"%s\n",mysql_error(db_handle));
mysql_close(db_handle);
return 1;
}
printf("insert ok\n");
return 0;
}
void Close(void)
{
mysql_close(db_handle);
}
/*
*usemysql.c main函数
*/
#include "conmysql.h"
int main(void)
{
if(Connect())
{
if (Insert(2,233,"mn","lsr"))
{
printf("datas not insert into database\n");
return 1;
}
}
Close();
return 1;
} |
|