Linux (Ubuntu 12.0.4 LTs) is connected to MySQL (as of today's latest version 5.5 ).

Source: Internet
Author: User
Tags mysql client mysql connect

First of all, the most annoying problem is the various crashing environment configuration problems. After more than five hours of consultation, I finally solved the problem. Now I can't remember it, it makes no sense.

If the configuration is specific, record the process and write a blog post independently.

In short, I remember there are two key points to configure:

1) mysql. h path configuration or usage when using this header file

# Include "... mysql. H". This is an absolute path.

However, after my configuration is completed, I only need to write it as # include <MySQL/MySQL. h>.

2) When GCC is compiling, it needs to connect to the dynamic library configuration and forget how to configure it. In short, after writing the code for calling the database, the compiled command format is as follows:

Gcc a. C-o a-lmysqlclient (of course,-o a can not be understood) and then the executable file a. Out is successfully generated. There is no problem in running the file.

Here-The lmysqlclient seems to be connected to the dynamic library file, and I don't quite understand it. It will also be used just now.

The following problems may occur to Ubuntu users:

When installing MySQL, it may be installed in the Ubuntu Software Center. When you enter MySQL search in the software center, there will be many related things, such as MySQL server and MYSQL client.

Previously, I only installed MySQL server, but it was still difficult. Later I installed MYSQL client in various Debugging Processes, and then it went normal, I don't know the specific reason. I can only say it is possible. I have wasted a lot of time, but I hope that if someone is wasting time like me, it will save you some time.

========================================================== ========================

The following is a piece of simple code that I successfully tested. I have to paste it in a rush, and I can understand all the annotations.

It is to create a connection in the main function, then call a table creation function, then call a data insertion function, and finally call a display function (I am writing this function blindly, the output information is not displayed on the terminal, just for testing). The previous section is a simple implementation of the three simple functions, and the implementation method is not very good, for personal information, see.

//// // VI test. C, you know

#include<stdio.h>#include<mysql/mysql.h>void createTable(MYSQL* my){int ret;ret=mysql_query(my,"create table mytable(name varchar(20) ,gender varchar(1) not null)");if(ret){printf("create table  failed!\n");return ;}printf("create table success!\n");}void insert(MYSQL* my){int ret;ret=mysql_query(my,"insert into mytable values('CSDN','m')");if(ret){printf("insert failed!\n");return ;}printf("insert success!\n");}void output(MYSQL* my){int ret;ret=mysql_query(my,"select * from mytable ");if(ret){printf("select failed!\n");return ;}printf("select success!\n");}int main(){MYSQL* mysql;mysql=mysql_init(NULL);if(!mysql){fprintf(stderr,"mysql_init error!\n");return 0;}printf("mysql_init success!\n");mysql=mysql_real_connect(mysql,"localhost","root","123","mydatabase",0,NULL,0);if(!mysql){fprintf(stderr,"mysql_connect error!\n");return 0;}printf("mysql connect success!\n");createTable(mysql);insert(mysql);output(mysql);mysql_close(mysql);return 0;}

Then exit the editor and compile it. The command line is as follows:

GCC test. C-o Test-lmysqlclient is compiled successfully. The executable file test is generated successfully, and the executable file execution is correct.

Open a terminal, log on to the database, view the database, tables, and so on, and see that the table is created successfully, and the data is inserted successfully, as expected.

Since then, the hateful environment configuration has come to an end...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.