Create a mysql C development environment in ubuntu 8.04

Source: Internet
Author: User
Build a mysql C development environment in ubuntu 8.04-[programming] Tag: C Linux

Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://feizf.blogbus.com/logs/30689586.html

Install mysql

Kissgnu @ kissgnu-desktop :~ $ Sudo apt-get install mysql-servel mysql-client
During the download and installation process, you will be prompted to enter the root password

Restart the mysql server:
Kissgnu @ kissgnu-desktop :~ $ Sudo/init. d/mysql restart

Authorize normal users to operate databases:
Kissgnu @ kissgnu-desktop :~ $ Mysql-u root-p (root login database)
Mysql> create database mydb; (create a database)
Mysql> grant all privileges on mydb. * to kissgnu @ localhost identified by 'kissgnu ';
Mysql> quit;

The format for assigning data operation permissions to a specific user is:
Grant permission 1, permission 2,... Permission n on database name. Table name to username @ user IP address identified by 'Connection password ';
The permissions include select, insert, update, delete, create, drop, index, alter, grant, references, reload, shutdown, process, and file.

Common users operate databases:
Kissgnu @ kissgnu-desktop :~ $ Mysql-u kissgnu-p (log on to the kissgnu account)
Mysql> show databases; (display all databases)
+ -------------------- +
| Database |
+ -------------------- +
| Information_schema |
| Mydb |
+ -------------------- +
2 rows in set (0.00 sec)

For the following database operations, see:
Http://feizf.blogbus.com/logs/5072906.html

Install the C programming interface:
Kissgnu @ kissgnu-desktop :~ $ Sudo apt-get install libmysqlclient15-dev
Related mysql header files and library files are installed in the/usr/include/mysql/and/usr/lib/mysql directories.
Kissgnu @ kissgnu-desktop :~ $ Gcc xxx. c-I/usr/include/mysql-L/usr/lib/mysql-lmysqlclient-o xxx

For specific programming interfaces, see sun website documentation http://dev.mysql.com/doc/refman/5.0/en/c.html
And an article http://hi.baidu.com/kangakang203/blog/item/b1c43ffe6d3188345c600828.html

The following is an example. Assume that the content of the mydb. pet table is as follows:
+ ---------- + ------- + --------- + ------ + ------------ + ------- +
| Name | owner | species | sex | birth | death |
+ ---------- + ------- + --------- + ------ + ------------ + ------- +
| Puffball | Diane | hamster | f | © 3-30 | NULL |
| Hak | jack | euro | m | 2001-03-05 | NULL |
+ ---------- + ------- + --------- + ------ + ------------ + ------- +

Program code:

// Demo. c
# Include <stdio. h>
# Include <mysql. h>

Int main (int argc, char ** argv)
{
MYSQL mysql_conn;/* Connection handle */
MYSQL_RES * mysql_result;/* Result handle */
MYSQL_ROW mysql_row;/* Row data */
Int f1, f2, num_row, num_col;

If (mysql_init (& mysql_conn )! = NULL)
{
If (mysql_real_connect (& mysql_conn, "localhost", "kissgnu ",
"Kissgnu", "mydb", MYSQL_PORT, NULL, 0 )! = NULL)
{
If (mysql_query (& mysql_conn, "select * from pet") = 0)
{
Mysql_result = mysql_store_result (& mysql_conn );
Num_row = mysql_num_rows (mysql_result );
/* Get the no. of row */
Num_col = mysql_num_fields (mysql_result );
/* Get the no. of column */

Printf ("row = % d, col = % d \ n", num_row, num_col );
For (f1 = 0; f1 <num_row; f1 ++)
{
Mysql_row = mysql_fetch_row (mysql_result );
For (f2 = 0; f2 <num_col; f2 ++)
{
/* Fetch one by one */
Printf ("[Row % d, Col % d] ==> [% s] \ n", f1, f2, mysql_row [f2]);
}
}
} Else
{
(Void) printf ("Query fails \ n ");
}
} Else
{
(Void) printf ("Connection fails \ n ");
}
} Else
{
(Void) printf ("Initialization fails \ n ");
}

Mysql_free_result (mysql_result );
Mysql_close (& mysql_conn );

Printf ("quit \ n ");
Return 0;
}

Kissgnu @ kissgnu-desktop :~ $ Gcc demo. c-I/usr/include/mysql-L/usr/lib/mysql-lmysqlclient-o demo
Kissgnu @ kissgnu-desktop :~ /Mymysql $./demo
Row = 2, col = 6
[Row 0, Col 0] ==> [Puffball]
[Row 0, Col 1] ==> [Diane]
[Row 0, Col 2] ==> [hamster]
[Row 0, Col 3] ==> [f]
[Row 0, Col 4] ==> []
[Row 0, Col 5] ==> [(null)]
[Row 1, Col 0] ==> [hak]
[Row 1, Col 1] ==> [jack]
[Row 1, Col 2] ==> [euro]
[Row 1, Col 3] ==> [m]
[Row 1, Col 4] ==> []
[Row 1, Col 5] ==> [(null)]
Quit

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.