Compiling and calling mysql database program with gcc under SCO _ MySQL

Source: Internet
Author: User
Tags informix
Environment: SCOOpenServer5.0.6, mysql3.23.42, gcc2.95.2 note: gcc has integrated functions that support mysql. you can directly call the database in the C source code without using ESQLC embedded calls like informix. 1. makefile file CCgccLIB-lcurses-lsocket-lmysqlclient-lm-lc-lnslall: gspsergspser: get_dat environment: SCO OpenServer 5.0.6, mysql 3.23.42, gcc 2.95.2
  
Note: gcc has integrated functions that support mysql. you can directly call the database in the C source code without using ESQL/C embedded calls like informix.
  
1. makefile
  
CC = gcc
  
LIB =-lcurses-lsocket-lmysqlclient-lm-lc-lnsl
  
All: gspser
  
Gspser: get_data.o getfileargv. o
  
$ (CC)-o gj get_data.o getfileargv. o $ (LIB)
  
Get_data.o: get_data.c
  
$ (CC)-c get_data.c
  
Getfileargv. o: getfileargv. c
  
$ (CC)-c getfileargv. c
  
Clean:
  
Rm-f *. o
  
2. get_data.c file
  
# Include
  
# Include
  
# Define DBCFG "./gsp. conf"
  
Main ()
  
{
  
Char dbhost [32], dbuser [16], dbpasswd [16], db [16];
  
Char query [256];
  
Int count;
  
MYSQL * mysql;/* indicates the handle to a database connection */
  
MYSQL_RES * mysql_res;/* indicates the result of a query in the returned row */
  
MYSQL_ROW mysql_row;/* string array */
  
My_ulonglong rows;
  
/* This type is used for row numbers, mysql_affected_rows (), mysql_num_rows (), and mysql_insert_id ()
  
*/
  
Getfileargv (DBCFG, "DBHOST", dbhost);/* read gsp. conf */
  
Getfileargv (DBCFG, "DBUSER", dbuser );
  
Getfileargv (DBCFG, "DBPASSWD", dbpasswd );
  
Getfileargv (DBCFG, "DATABASE", db );
  
If (! (Mysql = mysql_init (NULL)/* obtain or initialize a MYSQL structure */
  
{
  
Rintf ("mysql_init failed! ");
  
Mysql_close (mysql );
  
Exit (0 );
  
}
  
If (! Mysql_real_connect (mysql, dbhost, dbuser, dbpasswd, db, 0, NULL, 0 ))
  
{/* Connect to a MySQL server */
  
Rintf ("failed to connect to the server. please contact the system administrator! ");
  
Mysql_close (mysql );
  
Exit (0 );
  
}
  
Trcpy (query, "select * from zuh where jgm = \" 11110501 \ "order by zuh ");
  
If (mysql_query (mysql, query)/* execute an SQL query specified as an empty string */
  
{
  
Rintf ("mysql_query error! ");
  
Mysql_close (mysql );
  
Exit (0 );
  
}
  
Mysql_res = mysql_store_result (mysql);/* retrieve a complete set of results for the customer */
  
Rows = mysql_num_rows (mysql_res);/* returns the number of duplicate columns in a result set */
  
If (rows = 0)/* This management code does not exist and returns */
  
{
  
Rintf ("the returned value is null ");
  
Mysql_free_result (mysql_res);/* release the memory used by a result set */
  
Mysql_close (mysql);/* close a server connection */
  
Exit (0 );
  
}
  
For (count = 0; count   
{
  
Mysql_row = mysql_fetch_row (mysql_res);/* obtain the next row from the result set */
  
Rintf ("social ID: % s", mysql_row [0]);
  
Rintf ("group number: % s", mysql_row [1]);
  
Rintf ("Group name: % s \ n", mysql_row [2]);
  
}
  
Mysql_free_result (mysql_res );
  
Mysql_close (mysql );
  
}
  
3. getfileargv. c file
  
# Include
  
Int getfileargv (const char * filename, const char * argvname, char * retbuf)
  
{
  
FILE * fp;
  
Char line [128];
  
Char tempbuf [40];
  
Trncpy (tempbuf, argvname, 30 );
  
Tempbuf [39] = 0;
  
If (fp = fopen (filename, "r") = NULL)
  
Return-1;
  
Trcat (tempbuf, "= ");
  
While (fgets (line, 120, fp ))
  
{
  
If (line [0] = '#')
  
Continue;
  
If (! Strncmp (line, tempbuf, strlen (tempbuf )))
  
{
  
Fclose (fp );
  
Line [strlen (line)-1] = 0;
  
Trcpy (line, (char *) (strchr (line, '=') + 1 ));
  
Trcpy (retbuf, line );
  
Return 0;
  
}
  
}
  
Fclose (fp );
  
Return-2;
  
}

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.