c语言怎么连接mysql数据库 代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言怎么连接mysql数据库 代码相关的知识,希望对你有一定的参考价值。

//vc工具中添加E:\\WAMP\\BIN\\mysql\\MYSQL5.5.8\\LIB 路径
//在工程设置-》链接》库模块中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\\wamp\\bin\\mysql\\mysql5.5.8\\include\\mysql.h"
void main()
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
fprintf(stderr,"%s\\n",mysql_error(conn));
exit(1);

if(mysql_query(conn,sql))
fprintf(stderr,"%s\\n",mysql_error(conn));
exit(1);

res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL)
printf("%s\\n",row[2]);

mysql_free_result(res);
mysql_close(conn);

===============================
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]

MYSQL mysql,*handle; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result; //查询结果集,结构类型
MYSQL_FIELD *field ; //包含字段信息的结构
MYSQL_ROW row ; //存放一行查询结果的字符串数组
char querysql[160]; //存放查询sql语句字符串
//初始化
mysql_init(&mysql);
//连接数据库
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0)))
fprintf(stderr,"Couldn\'t connect to engine!\\n%s\\n\\n",mysql_error(&mysql));

sprintf(querysql,SELECT_QUERY,atoi(argv[1]));
//查询数据库
if(mysql_query(handle,querysql))
fprintf(stderr,"Query failed (%s)\\n",mysql_error(handle));

//存储结果集
if (!(result=mysql_store_result(handle)))
fprintf(stderr,"Couldn\'t get result from %s\\n", mysql_error(handle));

printf("number of fields returned: %d\\n",mysql_num_fields(result));
//读取结果集的内容
while (row = mysql_fetch_row(result))
printf("table: %s\\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) ) ;

//释放结果集
mysql_free_result(result);
//关闭数据库连接
mysql_close(handle);
system("PAUSE");
//为了兼容大部分的编译器加入此行
return 0;
参考技术A 库文件的链接:
VC++ 6.0直接在工具栏-生成-里面就有一个链接的
或者在文件中直接用这个语句
#pragma comment(lib,“你想用的库文件.lib”)
格式就是这样

c语言连接mysql——insert的代码实现

//mysql_insert
//gcc mysql_insert.c -o test -I /usr/include/mysql -L /usr/lib -lmysqlclient

#include <stdio.h>
#include <mysql/mysql.h>
#include <string.h>

#define HOST "localhost"
#define USERNAME "SERVER"
#define PASSWORD "SERVER_PASSWORD"
#define DATABASE "remote_control"

int main()
{
char device_id[12] = "12345678901x";
char device_mac[20] = "12345678901234567890";
char device_nikname[20] = "12345678901234567890";
char device_online_status[1] = "1";
char device_switch_status[1] = "1";
char dstr[107];
char *insert_device_sql = &dstr[0];

strcpy (dstr, "insert into devices values(‘");
strcat (dstr, device_id);
strcat (dstr, "‘,‘");
strcat (dstr, device_mac);
strcat (dstr, "‘,‘");
strcat (dstr, device_nikname);
strcat (dstr, "‘,‘");
strcat (dstr, device_online_status);
strcat (dstr, "‘,‘");
strcat (dstr, device_switch_status);
strcat (dstr, "‘)");
//printf("insert_device_sql =%d\n", (int)strlen(dstr));
//char *insert_device_sql = "insert into devices values(‘‘,‘‘,‘‘,‘‘,‘‘)";

char user_id[12] = "123456789012";
char user_account[11] = "12345678901";
char user_password[8] = "12345678";
char user_online_status[1] = "1";
char ustr[82];
char *insert_user_sql = &ustr[0];

strcpy (ustr, "insert into users values(‘");
strcat (ustr, user_id);
strcat (ustr, "‘,‘");
strcat (ustr, user_account);
strcat (ustr, "‘,‘");
strcat (ustr, user_password);
strcat (ustr, "‘,‘");
strcat (ustr, user_online_status);
strcat (ustr, "‘)");
//printf("insert_user_sql= %d\n", (int)strlen(ustr));
//char *insert_user_sql = "insert into user values(‘‘,,‘‘,‘‘)";

//char *sql = insert_device_sql;
char *sql = insert_user_sql;

MYSQL my_connection;
mysql_init(&my_connection);/*初始化mysql连接my_connection*/

if (!mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS))
{/*连接失败*/
printf("数据库连接失败: %s\n", mysql_error(&my_connection));
return 0;
}

mysql_query(&my_connection, "set names utf8");/*设置编码为utf8*/

int res;
res = mysql_real_query(&my_connection, sql, (unsigned int)strlen(sql));
if (res)
{/*执行失败*/
printf("sql語句执行失败: %s\n", mysql_error(&my_connection));
mysql_close(&my_connection);
return 0;
}

printf("数据插入成功!\n");
printf("%d行数据受到影响!\n", (int)mysql_affected_rows(&my_connection));

mysql_close(&my_connection);

return 0;
}

以上是关于c语言怎么连接mysql数据库 代码的主要内容,如果未能解决你的问题,请参考以下文章

C语言有mysql数据库连接池吗?

易语言怎么连接远程的MySQL数据库?

用C语言怎么实现与数据库的连接

怎样用c语言给mysql数据库插数据

c语言读取mysql库中的数据的程序头文件怎么设置

C语言连接mysql数据库