Install MySQL database in Linux and use C programming language to access database _ MySQL

Source: Internet
Author: User
Tags mysql commands
Install MySQL database in Linux and use C language programming to access database bitsCN.com

Installing software in ubuntu is quite simple. a simple apt-get install solution can solve this problem. The only drawback compared with the source code installation method is that you cannot customize the installation directory of the software. However, this is not a big drawback. Next we will use the apt-get tool to install the MySQL database.

Before installation, check whether the system has installed MySQL. For example:

The result shows that MySQL is not installed on your system. Run apt-get install mysql-server mysql-client to install the SDK. For example:

Enter y for installation. a page will pop up asking you to enter the MySQL administrator root password, and then confirm again. For example:

Then the system automatically installs MySQL until it is complete.

After the installation is complete, you can use netstat-tap | grep mysql to check whether the system has the mysql service, for example:

The above information indicates that MySQL is successfully installed. The mysql service is automatically started every time the system starts.

The configuration file of mysql is not described here in/etc/mysql/my. cnf.

Run the mysql-u root-p command to log on to the database. press enter and enter the MySQL administrator password.

To view the character set used by the database, enter show variables like 'character % ';

If you want to change this information, change it in the configuration file my. cnf.

So far, mysql is successfully installed.

The following describes several common mysql commands:

Display the database list: show databases;
Switch to a database: use "db_name ";
Query tables in the database: show tables; (select a database before querying the table)
Show create table "table_name ";
View the table field information and attributes: describe "table_name"; desc "table_name ";
View the database running environment information: status;

Create database [if not exists] "db_name ";
Delete a database: drop database [if exists] "db_name ";

Create a data table: create table "table_name" (field code field type ,...);
Delete a data table: drop table "table_name ";

Insert a record to the table: insert into "table_name" [(Field 1, field 2,...)] values (value 1, value 2 ,...);
Delete a table Record: delete from "table_name" where nid = 100;
Update "table_name" set level = 2, salary = 2000.0 where condition
Query the data in the table: select * from "table_name ";
Select Field 1, field 2 from "table_name ";

Next, let's talk about how to use C programming to operate databases:

When developing C programs, we need the header file mysql. h. The database we installed earlier does not contain this header file. if you need to develop an application, you must install another Development Kit named libmysqlclient-dev. Follow these steps:

Now, we can use C to develop applications. To operate the database, you only need to include the header file mysql. h.

Below is a simple example of C language code for database operations:

 1 /* Simple C program that connects to MySQL Database server */ 2 #include 
 
   3 #include 
  
    4 #include 
   
     5  6 int main(void) 7 { 8     MYSQL        *conn; 9     MYSQL_RES    *res;10     MYSQL_ROW    row; 11 12     char *server = "localhost";13     char *user = "root";14     char *password = "12345";15     char *database = "mysql";16 17     conn = mysql_init(NULL);18 19     /* Connect to database */20     if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {21             fprintf(stderr, "%s/n", mysql_error(conn));22             exit(1);23     }24 25     /* send SQL query */26     if (mysql_query(conn, "show tables")) {27             fprintf(stderr, "%s/n", mysql_error(conn));28             exit(1);29     }30 31     res = mysql_use_result(conn);32 33     /* output table name */34     printf("MySQL Tables in mysql database:/n");35     while ( (row = mysql_fetch_row(res)) != NULL)36             printf("%s /n", row[0]);37 38     /* close connect */39     mysql_free_result(res);40     mysql_close(conn);41 }
   
  
 
View Code

Use the following code during compilation:

gcc $(mysql_config --cflags)  xxx.c -o xxx $(mysql_config --libs)

This program connects to the local database mysql, and then outputs all the table information contained in the database mysql.

BitsCN.com

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.