MySQL in Linux Programming

Source: Internet
Author: User

Lab environment: Ubuntu13.04

Before that, I had some experience using MySQL databases. on the Windows platform, I used the GUI (I used HeidiSQL and Workbench to manage databases) and used the mysqldb module in Python to operate MySQL databases. Recently, I have learned a lot about Linux operating system management, from Uncle bird's two books to Linux Shell programming, to C Programming under Linux. Here we will introduce how to operate databases using C language in Linux. I will build a MySQL environment, mySQL commands are used to use the C interface to operate MySQL and other processes. A detailed introduction of how to manage MySQL Databases in Linux can be learned together.

1. install and configure MySQL:

The method for installing MySQL in Ubuntu is very simple. Use the following command:

 apt-get  mysql-server

During the installation process, the system will prompt you to set the root password. This process can be skipped. However, we recommend that you set the root password when prompted during installation to avoid the trouble of setting the root password later. After the installation is complete, the mysql service is started. You can run the following command to check whether the mysql service has been successfully installed:

 -el |  mysql

If the mysql service does not run properly, run the following command to restart the mysql service:

 service mysql restart

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

 apt-get  mysql-workbench

Use the following command to start Workbench:

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

2. MySQL command line:

We use root to log on to MySQL and perform the following operations:

mysql -u root -p

Here, the system will prompt you to enter the password, just enter the MySQL password you set earlier, and then the program will enter mysql command line mode. Suppose we need to view the user information, we use the following command:

 

MySQL returns all host, user, password, and other information. Other complex operations, such as adding a database or adding a table, are the same as common data operation commands, which will be described in the following example. Let's get started with the practice of operating MySQL in Linux C!

3. Use the C language to manage MySQL databases:

First, we need to install MySQL multi-dependent libraries in Linux. The installation command is as follows:

 apt-get  libmysqlclient-dev

After this is installed, the header files and library files required for programming are complete. Let's start the C Programming journey!

First, let's prepare a space for us to toss, that is, prepare a dedicated account for tossing, a dedicated database for tossing, and a data table:

*.* TO rick@localhost IDENTIFIED BY -u rick -

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

 ()  ((   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, ,   children(childno, fname, age)  (, , );

Save the preceding SQL statement as create_children. SQL, and then use the following command to import the MySQL database foo:

mysql -u rick --password=

Okay. Write a demo for testing:

#include <stdlib.h><stdio.h> main( argc,  *& (mysql_real_connect(&my_connection, , , , , NULL, = mysql_query(&my_connection,  (!)mysql_affected_rows(&, mysql_errno(&my_connection), mysql_error(&& (mysql_error(&, mysql_errno(&my_connection), mysql_error(&

Save the preceding code as demo. c. In the above Code, we need to include the mysql. h header file to use the APIS provided by mysql to operate MySQL. After the program is written, the required link information must be added to the compilation process:

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

All right, the program is compiled successfully. Run it and try it:

./ rows

Thank you for reading this article. I hope it will help you (Published by Windows Live Writer ).

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.