C Apt-get Install Libmysqlclient-dev Windows
This is a C programming tutorial for the MySQL database. It covers the basics of MySQL programming with the C API. You may also consider to look at theMySQL tutorial on ZetCode.
About MySQL database
Processing triggers for libc-bin (2.19-0ubuntu6.1). $ sudo apt-get install libmysqlclient-dev Reading package lists. Done Building dependency tree Reading state information. Done The following NEW packages will be installed: libmysqlclient-dev 0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded. MySQL C API programming tutorial. This is a C programming tutorial for the MySQL database. It covers the basics of MySQL programming with the C API. You may also consider to look at the MySQL tutorial on ZetCode. Sudo apt-get install mysql-client sudo apt-get install mysql-workbench sudo apt-get install libmysqlclient-dev I need both the libmysqlclient.so and libmysqlclientr.so libraries. Debianを使う。 pythonでmysqlを使うのに必要というので、 $ sudo apt-get install libmysqlclient-dev Reading package lists. Done Building dependency tree Reading state information. Done Package libmysqlclient-dev is not available, but is referred to by another package. This may mean that the package is missing, has b.
MySQL is a leading open source database management system. It is a multi user, multithreaded database management system. MySQL is especially popular on the web. It is one part of the very popular LAMPplatform consisting of Linux, Apache, MySQL, and PHP. MySQL currently ownedby Oracle. MySQL database is available on most important OSplatforms. It runs on BSD Unix, Linux, Windows, or Mac OS.Wikipedia and YouTube use MySQL. These sites manage millions of querieseach day. MySQL comes in two versions: MySQL server system and MySQLembedded system.
To be able to compile C examples, we need to install the MySQL C developmentlibraries. The above line shows how we can do it on Debian based Linux.
C99
This tutorial uses C99. For GNU C compiler, we need to add the -std=c99 option. For Windows users, the Pelles C IDE is highly recommended. (MSVC does not support C99.)
In C99, we can mix declarations with code. In older C programs, we wouldneed to separate this line into two lines.
First example
Our first example will test one MySQL function call.
# apt-get install mysql-server mysql-client libmysqlclient-dev Reading package lists. Done Building dependency tree Reading state information. Done Package libmysqlclient-dev is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source.
The mysql_get_client_info() shows the MySQL client version.
We include necessary header files. The mysql.h is the most important header file for MySQL function calls. The my_global.h includes some global declarations a functions. Among other things, it includes the standard input/output header file.
This code line outputs the version of the MySQL client. For this, we use the mysql_get_client_info()function call.
We exit from the script.
Here is how we compile the code example.
Example output.
Creating a database
The next code example will create a database. The code example can be divided into these parts:
- Initiation of a connection handle structure
- Creation of a connection
- Execution of a query
- Closing of the connection
The code example connects to the MySQL database system and creates a new database called testdb.
The mysql_init() function allocates or initialises a MYSQL object suitable for mysql_real_connect() function. Remember this is C99.
We check the return value. If the mysql_init() function fails, we print the error message and terminate the application.
The mysql_real_connect() function establishes a connection to the database. We provide connection handler, host name, user name and password parameters to the function. The other four parameters are the database name, port number, unix socket and finally the client flag.We need superuser priviliges to create a new database.
The mysql_query() executes the SQL statement. In our case, the statement creates a new database.
Finally, we close the database connection.
The second example already utilizes features from C99 standard. Therefore,we need to add the -std=c99 option.
This is the proof that the database was created.
Creating and populating a table
Before we create a new table, we create a user that we will use in the rest of the tutorial.
We have created a new user user12.
Here we grant all priviliges to user12 on testdb database.
The next code example will create a table and insert some data into it.
We don't use any new MySQL function call here. We use mysql_query() function call to both create a table and insert data into it.
In order to avoid unnecessary repetition, we create a custom finish_with_error() function.
We connect to testdb database. The user name is user12 and password is 34klq*. The fifth parameter is the database name.
Here we create a table named Cars. It has three columns.
We insert one row into the Cars table.
We show tables in the database.
We select all data from the table.
Retrieving data from the database
In the next example, we will retrieva data from a table.
We need to do the following steps:
- Create a connection
- Execute query
- Get the result set
- Fetch all available rows
- Free the result set
C Apt-get Install Libmysqlclient-dev Windows Xp
The example prints all columns from the Cars table.
We execute the query that will retrieve all data from theCars table.
We get the result set using the mysql_store_result()function. The MYSQL_RES is a structure for holding a result set.
We get the number of fields (columns) in the table.
We fetch the rows and print them to the screen.
We free the resources.
C Apt-get Install Libmysqlclient-dev Windows 10
Example output.
Last inserted row id
Sometimes, we need to determine the id of the last inserted row. We can determine the last inserted row id by calling the mysql_insert_id() function. The function only worksif we have defined an AUTO_INCREMENT column in the table.
A new table is created. Three rows are inserted into the table. We determinethe last inserted row id.
The Id column has an AUTO_INCREMENT type.
The mysql_insert_id() function returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.
Output.
Column headers
In the next example, we will retrieve data from the table and its column names.
We print the first three rows from the Cars table. We also include thecolumn headers.
The MYSQL_FIELD structure contains information about a field, such as the field's name, type and size. Field values are not part of this structure; they are contained in the MYSQL_ROW structure.
The first row contains the column headers. The mysql_fetch_field() call returns a MYSQL_FIELD structure. We get the column header names from this structure.
This is the output of our program.
Multiple statements
It is possible to execute multiple SQL statements in one query.We must set the CLIENT_MULTI_STATEMENTS flag in the connect method.
In the example, we execute three SELECT statements in one query.
The last option of the mysql_real_connect() method is the client flag. It is used to enable certain features. The CLIENT_MULTI_STATEMENTS enables the execution of multiple statements. This is disabled by default.
The query consists of three SELECT statements. They are separated by the semicolon ; character. The backslash character is used to separate the string into two lines. It has nothing to do with multiple statements.
The code is placed between the do/while statements. The data retrieval is to be done in multiple cycles. We will retrieve data for each SELECT statement separately.
We expect multiple result sets. Therefore, we call the mysql_next_result() function. It reads the next statement result and returns a status to indicate whether more results exist. The function returns 0 if the execution went OK and there are more results. It returns -1, when it is executed OK and there are no more results. Finally, it returns value greater than zero if an error occurred.
We check for error.
Example output.
Inserting images into MySQL database
Some people prefer to put their images into the database, some prefer to keep them on the file system for their applications.Technical difficulties arise when we work with lots of images.Images are binary data. MySQL database has a special data type to store binary data called BLOB (Binary Large Object).
For our examples, we create a new Images table. The image size can be up to 16 MB. It is determined by the MEDIUMBLOB data type.
In this example, we will insert one image into the Images table.
This include is for the strlen() function.
Here we open the image file. In the current working directory, we shouldhave the woman.jpg file.
We move the file pointer to the end of the file using the fseek()function. We are going to determine the size of the image. If an error occurs, the error indicator is set. We check the indicator using the fseek()function. In case of an error, we also close the opened file handler.
For binary streams, the ftell() function returns the number of bytes from the beginning of the file, e.g. the size of the image file. In case of an error, the function returns -1 and the errno is set. The perrro()function interprets the value of errno as an error message, and prints it to the standard error output stream.
In this array, we are going to store the image data.
We read the data from the file pointer and store it in the dataarray. The total number of elements successfully read is returned.
After the data is read, we can close the file handler.
The mysql_real_escape_string() function adds an escape character, the backslash, , before certain potentially dangerous characters in a string passed in to the function. This can help prevent SQL injection attacks. The new buffer must be at least 2*size+1 long.
Here we start building the SQL statement. We determine the size of the SQL stringusing the strlen() function.
The query must take be long enough to contain the size of the SQL stringstatement and the size of the image file. Using the snprintf()function, we write the formatted output to query buffer.
We execute the query using the mysql_real_query() function.The mysql_query() cannot be used for statements that contain binary data; we must use the mysql_real_query() instead.
Selecting images from MySQL database
In the previous example, we have inserted an image into the database.In the following example, we will select the inserted image back fromthe database.
In this example, we will create an image file from the database.
C Apt-get Install Libmysqlclient-dev Windows 64
We open a new file handler for writing.
We select the Data column from the Image table with Id 1.
The row contains raw data.

We get the length of the image.
We write the retrieved data to the disk using the fwrite() function call. We check for the error indicatorwith the ferror() function.
After we have written the image data, we close the file handlerusing the fclose() function.
This was MySQL C API tutorial. You may be also interested in MySQL Python tutorial,MySQL Visual Basic tutorial, or MySQL PHP tutorial, PostgreSQL C tutorial, or SQLite C tutorial on ZetCode.
C API (libmysqlclient) is a client library for C development.
For C-language and SQL:
- for MySQL 8.0, 5.7, 5.6, 5.5
- we recommend MySQL 8.0 C API
For C-language and NoSQL XDevApi DocStore:
- for MySQL 8.0 [not applicable to 5.7, 5.6, 5.5]
- we recommend MySQL Connector/C++ 8.0
- note: Connector/C++ 8.0 has compatible C headers
C API (libmysqlclient) is included in MySQL 8.0
- Linux: The Client Utilities Package is available from the MySQL Community Server download page.
- Repos: The Client Utilities Package is available from the Yum, APT, SuSE repositories.
- Windows: The Client Utilities Package is available from the Windows Installer.
C Apt-get Install Libmysqlclient-dev Windows 7
Previous GA versions are available from MySQL Product Archives.
C Apt-get Install Libmysqlclient-dev Windows 10
Online Documentation:
Libmysqlclient Dev Mac
Please report any bugs or inconsistencies you observe to our Bugs Database.
Thank you for your support!