Linux installation MySQL and the use of C language to manipulate the database method C language Connection Mysql_c language

Source: Internet
Author: User
Tags prepare mysql command line

1. mysql Installation and configuration:

Installing the MySQL method under Ubuntu is simple, using the following command:

Copy Code code as follows:

sudo apt-get install Mysql-server

During installation, the system prompts you to set the root password, which can be skipped, but it is recommended that you set the root password when you install it, so that you don't have to set up trouble behind it. After the installation is complete, the MySQL service is started and can be viewed using commands to verify that the MySQL service has been installed successfully:

Copy Code code as follows:

Ps-el | grep MySQL

If the MySQL service is not running properly, you can use the following instructions to restart the MySQL service:

Copy Code code as follows:

sudo service MySQL restart

Like to use the Workbench interface, you also need to install Workbench:

Copy Code code as follows:

sudo apt-get install Mysql-workbench

Workbench starts with the following command:

Copy Code code as follows:

Mysql-workbench--log-level=debug3--verbose

2. mysql command line:

We use root to log in to MySQL, and then do the relevant actions:

Copy Code code as follows:

Mysql-u root-p

Here, you will be prompted to enter the password, you only need to enter the MySQL password set before, and then the program will enter the MySQL command line mode, assuming we need to view the user information, we use the following command:

Copy Code code as follows:

Use MySQL
SELECT host, user, password from user;

MySQL will return all information such as Host,user and password. Other more complex operations, such as adding a database, adding tables, and common data manipulation commands, are described in an example. Let us quickly into the Linux under the C operation MySQL practice it!

3. Use c language to manage MySQL database:

First of all, we need to install under Linux operating MySQL multiple dependent library, the installation commands are as follows:

Copy Code code as follows:

sudo apt-get install Libmysqlclient-dev

After installing this, we need to program the header files, library files, etc., let's start the C programming journey!

First, let's prepare a space we use to toss, that is to prepare a toss exclusive account, a toss exclusive database and datasheet, etc.:

Copy Code code as follows:

#添加账户
GRANT all on *.* to rick@localhost identified by ' secret '
\q
#使用新创建的rick账户登录
Mysql-u rick-p
#创建数据库
CREATE DATABASE foo;

Then we insert the data table and test data using a SQL file:

Copy Code code as follows:

--
--Create The table children
--
CREATE TABLE Children (
Childno Int (one) 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 SQL statement as Create_children.sql, and then use the following command to import the MySQL database foo:

Copy Code code as follows:

Mysql-u Rick--password=secret Foo
\. Create_children.sql

Well, write a demo to test it:

Copy Code code as follows:

#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 success\n");
res = mysql_query (&my_connection, INSERT into children (fname, age) VALUES (' Ann ', 3) ");
if (!res) {
printf ("Inserted%lu rows\n",
(unsigned long) mysql_affected_rows (&my_connection));
} else {
fprintf (stderr, "Insert error%d:%s\n", Mysql_errno (&my_connection), Mysql_error (&my_connection));
}

Mysql_close (&my_connection);
} else {
fprintf (stderr, "Connection failed\n");
if (Mysql_error (&my_connection)) {
fprintf (stderr, "Connection error%d:%s\n", 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 with the API provided by MySQL. The program is written, the compilation process needs to add the required link information:

Copy Code code as follows:

Gcc-i/usr/include/mysql Demo.c-l/usr/lib/mysql-lmysqlclient-o Demo

OK, the program compiles successfully, run a try:

Copy Code code as follows:

./demo

#结果如下
Connection success
Inserted 1 rows

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.