Http://www.mysql.com/doc/en/C.html
[Post from http://homepage.qdcatv.com.cn/antonio/mysql/mysql.htm]
To perform a query, follow these steps. First, execute a query and save the result,
The result is a subset. Here is a small example:
# Include
# Include
# Include "mysql. H"
MySQL;
Mysql_res * res;
Mysql_row row;
Void exiterr (INT exitcode)
{
Fprintf (stderr, "% s \ n", mysql_error (& MySQL ));
Exit (exitcode );
}
Int main ()
{
Uint I = 0;
If (! (Mysql_connect (& MySQL, "host", "username", "password ")))
Exiterr (1 );
If (mysql_select_db (& MySQL, "payroll "))
Exiterr (2 );
If (mysql_query (& MySQL, "Select name, rate from emp_master "))
Exiterr (3 );
If (! (RES = mysql_store_result (& MySQL )))
Exiterr (4 );
While (ROW = mysql_fetch_row (RES ))){
For (I = 0; I <mysql_num_fields (RES); I ++)
Printf ("% s \ n", row [I]);
}
Mysql_free_result (RES );
Mysql_close (& MySQL );
}
The mysql_query function sends the query to the server. If the query is successful, mysql_store_result is called.
The function allocates a mysql_res structure and obtains a result set from the server. You can use
The mysql_fetch_row function is used to view data. In this way, you will obtain a mysql_row pointer pointing to the number
Data. The mysql_row pointer is a simple string array. All data types are converted
String to the client.
The mysql_num_fields function tells you how many columns are returned. You can continue to call mysql_fetch_row
Until it returns a null pointer to get each row in the query.
Note that in this example, we do not check the empty pointer column. If you do not use a table with non-empty columns
You must check whether the column of a special row is empty.
Once you have used a result set, you must release it. This is done through mysql_free_result.
Finally, call mysql_close to close the connection between you and the database.
View result set
You do not need to call mysql_fetch_row to find the total number of rows in the returned result set. This
Int mysql_num_rows (mysql_res * result.
Change to the row returned by the next mysql_fetch_row call. You can use
Void mysql_data_seek (mysql_res * res, uint offset) is changed to any row.
Get more information
You can use these additional functions to find more information about a query and obtain it from the server.
This information.
If you execute an update, insert, or delete query, you can use
Int mysql_affected_rows to find out how many rows of data are affected.
If binary data exists in your database, it is useful to know the Data Length. Unsigned
Int * mysql_fetch_lengths (mysql_res * MySQL) will return 1 indicating each column in The result set
.
When you insert a table with the auto_increment column, you can use
Int mysql_insert_id (MySQL * MySQL) to find the ID of the row generated.
======================================
I have been connected!
# Include "/include/MySQL. H"/* absolute path */
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Char * user = "root", * Pwd = "MySQL", * dbname = "MySQL ";
MySQL;
Mysql_res * mysql_ret;
Mysql_row;
Unsigned long num_rows;
Int ret;
Mysql_init (& MySQL );
If (mysql_real_connect (& MySQL, null, user, PWD, dbname, 0, null, 0 ))
{
Printf ("connection success! \ N ");
Ret = mysql_query (& MySQL, "select * from user ");
If (! RET)
{
Printf ("query success! \ N ");
Mysql_ret = mysql_store_result (& MySQL );
If (mysql_ret! = NULL)
{
Printf ("store result success! \ N ");
Num_rows = mysql_num_rows (mysql_ret );
If (num_rows! = 0)
{
Printf ("% d \ n", num_rows );
While (mysql_row = mysql_fetch_row (mysql_ret ))
{
Printf ("% s \ t % s \ n", mysql_row [0], mysql_row [1], mysql_row [2], mysql_row [3], mysql_row [4], mysql_row [5]);
}
}
Else
{
Printf ("mysql_num_rows failed! \ N ");
Exit (-1 );
}
Mysql_free_result (mysql_ret );
Exit (0 );
}
Else
{
Printf ("store result failed! \ N ");
Exit (-1 );
}
}
Else
{
Printf ("query failed! \ N ");
Exit (-1 );
}
}
Else
{
Printf ("Connection Failed \ n ");
Exit (-1 );
}
}
If you include the correct header file and are connected, it tells you that there is no symbolic connection.
You should connect to the library you need
Under my/lib/MySQL/libmysqlclient. So
Gcc-L/lib/MySQL-lmysqlclient-O tes. C for compilation
RedHat_shu@hotmail.com