Linux installs mysql and USES c language to manipulate the database method c language to connect to mysql

  • 2020-04-02 02:08:58
  • OfStack

1. Installation and configuration of MySQL:

Installing MySQL on Ubuntu is easy, using the following command:


sudo apt-get install mysql-server

During installation, the system will prompt you to set the root password. This process can be skipped, but it is recommended to set the root password when prompted during installation, so as to avoid the trouble of setting it later. After the installation, the system will start the mysql service, you can use the command to check whether the mysql service has been installed successfully:


ps -el | grep mysql

If the mysql service is not running properly, the following instructions can be used to restart the mysql service:


sudo service mysql restart

If you like to use the Workbench interface, you also need to install Workbench:


sudo apt-get install mysql-workbench

The Workbench startup USES the following command:


mysql-workbench --log-level=debug3 --verbose

2. MySQL command line:

We use root to log into MySQL and do the following:


mysql -u root -p

Here, the system will prompt for the password, just need to enter the MySQL password set before, and then the program will enter the MySQL command line mode, assuming that we need to see the user information, we use the following command:


use mysql
SELECT host, user, password FROM user;

MySQL will return all the information such as host, user and password. Other more complex operations, such as adding a database, adding a table, and so on, are consistent with normal data manipulation commands, which will be illustrated with an example later. Let's get into the practice of C operating MySQL under Linux!

3. Use C language to manage MySQL database:

First of all, we need to install the MySQL multi-dependent library operating under Linux. The installation command is as follows:


sudo apt-get install libmysqlclient-dev

After installing this, we need to program the header files, library files and so on complete, let's start the journey of C programming!

First of all, let's prepare a space for us to play around with, that is, prepare a play around exclusive account, a play around exclusive database and data table, etc. :


# Add account 
GRANT ALL ON *.* TO rick@localhost IDENTIFIED BY 'secret'
q
# Use the newly created rick The account login 
mysql -u rick -p
# Create a database 
CREATE DATABASE foo;

Then, we use an SQL file to insert the data table and test the data:


--
-- Create the table children
--
CREATE TABLE children (
    childno int(11) NOT NULL auto_increment,
    fname varchar(30),
    age int(11),
    PRIMARY KEY (childno)
);
--
-- Populate the table 'children'
--
INSERT INTO children(childno, fname, age) VALUES (1, 'Jenny', 21);
INSERT INTO children(childno, fname, age) VALUES (2, 'Andrew', 17);
INSERT INTO children(childno, fname, age) VALUES (3, 'Gavin', 8);
INSERT INTO children(childno, fname, age) VALUES (4, 'Duncan', 6);
INSERT INTO children(childno, fname, age) VALUES (5, 'Emma', 4);
INSERT INTO children(childno, fname, age) VALUES (6, 'Alex', 15);
INSERT INTO children(childno, fname, age) VALUES (7, 'Adrian', 9);

Save the above SQL statement as create_children.sql and import the MySQL database foo using the following command:


mysql -u rick --password=secret foo
. create_children.sql

Okay, let's write a demo to test it:


#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[]) {
    MYSQL my_connection;
    int res;
    mysql_init(&my_connection);
    if (mysql_real_connect(&my_connection, "localhost",
                "rick", "secret", "foo", 0, NULL, 0)) {
        printf("Connection successn");
        res = mysql_query(&my_connection, "INSERT INTO children(fname, age) VALUES('Ann', 3)");
        if (!res) {
            printf("Inserted %lu rowsn",
                    (unsigned long)mysql_affected_rows(&my_connection));
        } else {
            fprintf(stderr, "Insert error %d: %sn", mysql_errno(&my_connection), mysql_error(&my_connection));
        }
        mysql_close(&my_connection);
    } else {
        fprintf(stderr, "Connection failedn");
        if (mysql_error(&my_connection)) {
            fprintf(stderr, "Connection error %d: %sn", mysql_errno(&my_connection), mysql_error(&my_connection));
        }
    }
    return EXIT_SUCCESS;
}

Save the above code as demo.c. In the above code, we need to include the mysql.h header file to manipulate mysql using the API provided by mysql. The program is written, the compilation process needs to add the required link information:


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

Okay, the program compiles successfully, run it and try it:


./demo
# The results are as follows 
Connection success
Inserted 1 rows


Related articles: