数据库简单学习log 在linux中c语言操作连接数据库

	
	//Linux中用c语言操作连接数据库
	//安装链接库 
	apt-get install libmysqlclient-dev
	
	//gcc编译sql_test.c
	
	gcc -I/usr/include/mysql sql_test.c -L/usr/lib/mysql -lmysqlclient

	/*******************************sql_test.c file******************************/
	
	#include <stdlib.h>
	#include <stdio.h>
	#include <mysql.h>

        int main()
	{
        MYSQL *Conn;

        Conn=mysql_init(NULL);
							//一般都是传递一个NULL
        if(!Conn)
		{
            fprintf(stderr,"mysql_init failed\n");
            return EXIT_FAILURE;
        }

        Conn=mysql_real_connect(Conn,"localhost","root","1","example",0,NULL,0);
		//localhost 数据库所在ip
		//root 数据库用户名
		//1 数据库密码
		//example 数据库名
		//最后三个参数一般默认
		
		/*
		函数原型描述:
        MYSQL *mysql_real_connect 
				(
							  MYSQL *mysql,
							  const char *host,
							  const char *user, 
							  const char *passwd, 
							  const char *db, 
						          unsigned int port,
							  const char *unix_socket,
                                                          unsigned long client_flag
				)

		*/
        if(Conn)
	{
            printf("Connection success\n");
        }
        else
        {
            printf("Connection failed\n");
        }
        mysql_close(Conn);

        return EXIT_SUCCESS;
            
        }
	
	
	
发布了50 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_41865104/article/details/90302228