One call experience of MySQL C API

Source: Internet
Author: User
Tags control characters gpg

MySQL provides standard C APIs,

To reference it, you need to install the mysql-devel package. In centos, I directly install mysql-devel in yum, and the result is exactly that the yum source has been reset, during the download process, the GPG key error is always prompted. Google's result is: If your yum accesses the centos update source through a proxy, the following error may occur:
GPG key retrieval failed...
The possible cause is that the GPG key file contains some HTTP control characters.
In fact, you can download an rpm-GPG-key-centos file from an update source.
# Wget http://centos.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-5
# Rpm -- import RPM-GPG-KEY-CentOS-5

 

After running MySQL, the header file of MySQL can be found in/usr/include/MySQL after mysql-devel is installed, mySQL library files can be found in/usr/lib/MySQL; this is required for compilation and connection.

Use the test database that comes with the MySQL database to test it:

First, I created a table T1 in the MySQL test database;

Create Table T1 (ID int (11) default null, name varchar (100) default null );

Insert two temporary data:

Insert into T1 (ID, name) values ("1001", "The grade is good !");

Insert into T1 (ID, name) values ("1002", "The grade is good also !");

View the table structure:

Desc T1;

Or show columns from T1;

Next, let's use the wheel. It's probably a joke. Everyone likes to call the class library a wheel. It's a little big. Let's take a look!

# Include <mysql. h>
# Include <stdio. h>
# Include <string. h>

Int main (){

MySQL; // need a instance to init
Mysql_res * res;
Mysql_row row;
Char * query;
Int T, R;
// Connect the database
Mysql_init _ ($ mysql );
If (! Mysql_real_connect (& MySQL, "localhost", "test", 0, null, 0 ))
{
Printf ("error connecting to database: % s \ n", mysql_error ($ mysql ));
}
Else
Printf ("connected .. \ n ");
// Get the result from the executing SELECT query
Query = "select * from T1 ";
T = mysql_real_query (& MySQL, query, (unsigned INT) strlen (query ));
If (t)
{
Printf ("error making query: % s \ n", mysql_error (& MySQL );
}
Else
Printf ("[% s] made .. \ n", query );
Res = mysql_store_result (& MySQL );
While (ROW = mysql_fetch_row (RES ))
{
For (t = 0; t <mysql_num_fields (RES); t ++)
{
Printf ("% s", row [T]);
}
Printf ("\ n ");
}

Printf ("mysql_free_result... \ n ");
Mysql_free_result (RES); // free result after you get the result
Sleep (1 );

Mysql_close (& MySQL );

Return 0;
}

 

Of course, there are a lot of problems, such as MySQL instance is a reference variable, often ignored, or mistakenly written as %, and strlen function belongs to string. h header file, I always mistakenly believe it is in stdio. h medium, huh, it's very handy.

The most important time is now. It is naturally the MySQL library file and header file;

GCC testsql. C-I/usr/include/MySQL-L/usr/lib/MySQL-lmysqlclient

If there is no accident, of course, I mean that the sky has collapsed and it should be able to be compiled smoothly;

Of course, the record will be printed when running;

Related Article

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.