C-API Examples
From; https://github.com/hholzgra/connector-c-examples






mariadb_get_infov /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_real_connect() * see also http://mysql.com/mysql_real_connect *******************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> // mariadb_get_infov CONVENIENCE WRAPPER MACROS THESE WILL RETRIEVE AND PRINT //VALUE FOR A GIVEN INFO TYPE #define STRING_INFO(x) if( mariadb_get_infov( mysql, x, (void*)&string_info)) \ { printf("Couldn't get %s\n", #x); \ } else \ { \ printf("%-40s: %s\n", #x, string_info); \ } #define UINT_INFO(x) if( mariadb_get_infov(mysql, x, (void *)&uint_info)) \ { \ printf("Couldn't get %s\n", #x); \ } else \ { \ printf("%-40s: %d\n", #x, uint_info); \ } /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = NULL; const char *string_info; unsigned int uint_info; if( mysql_library_init(argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS // connection flags */ )) { fprintf( stderr, "mysql_real_connect() failed: '%s'\n", mysql_error(mysql)); puts( "Connect failed\n"); } else { puts("Connect OK\n"); // TODO THERE ARE MANY MORE OPTIONS, SEE E.G. // https://mariadb.com/kb/en/mariadb_get_infov/ STRING_INFO( MARIADB_TLS_LIBRARY ); STRING_INFO(MARIADB_CONNECTION_TLS_VERSION ); UINT_INFO( MARIADB_CONNECTION_TLS_VERSION_ID ); STRING_INFO( MARIADB_CLIENT_VERSION ); UINT_INFO( MARIADB_CLIENT_VERSION_ID ); STRING_INFO( MARIADB_CONNECTION_HOST ); STRING_INFO( MARIADB_CONNECTION_INFO ); STRING_INFO( MARIADB_CONNECTION_SERVER_TYPE ); } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mariadb_change_user /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_change_user() see also http://mysql.com/mysql_change_user ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*H***************************************************** * *******************************************************/ int main(int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts("Init faild, out of memory?"); return EXIT_FAILURE; } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql // MYSQL STRUCTURE TO USE , NULL, // SERVER HOSTNAME OR IP ADDRESS , NULL, // MYSQL USER , NULL, // PASSWORD , NULL, // DEFAULT DATABASE TO USE, null FOR NONE , 0, // PORT NUMBER, 0 FOR DEFAULT , NULL, // SOCKET FILE OR NAMED PIPE NAME , CLIENT_FOUND_ROWS // CONNECTION FLAGS )) { puts("Connect failed\n"); } else { if( mysql_change_user(mysql, "name", "passwd", "test")) { printf("Chang user failed: %s", mysql_error(mysql)); } else { puts("Change user OK\n"); } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_close /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_close() see also http://mysql.com/mysql_close ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( mysql ) { puts( "INIT OK\n"); mysql_close( mysql ); } else { puts("Init faild, out of memory?"); } mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_create_db /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_create_db() WARNING: this function is no longer present in current releases see also http://mysql.com/mysql_create_db ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { int stat; MYSQL *mysql = helper_connect(argc, argv); // SEE helper.h FOR ACTUAL CODE stat = mysql_create_db( mysql, "create_db_test"); if( stat ) { fprintf( stderr, "mysql_create_db failed: %s \n", mysql_error(mysql)); } else { printf( "OK\n" ); } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_data_seek /*H************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_data_seek() see also http://mysql.com/mysql_data_seek *********************************************************/ // comment: why no return value? #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv ) { MYSQL *mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE if( mysql_query( mysql, "SELECT user,host FROM mysql.user")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( result ) { MYSQL_ROW row; int rownum = 3, i; unsigned int num_fields = mysql_num_fields( result ); printf( "Got a valid result set, looking for result row #%d\n" , rownum); mysql_data_seek( result, rownum ); row = mysql_fetch_row( result ); if( row ) { printf(" ROW: "); for( i = 0; i < num_fields; i++) { printf("%s, ", row[i]); } putchar('\n'); mysql_free_result( result ); } else { puts("result has less than 3 rows"); } } else { printf( "Couldn't get results set: %s\n", mysql_error( mysql )); } } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_dump_debug_info /******************************************************** Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_dump_debug_info() * NOTE: this examples requires a db user with SUPER privilege * see also http://mysql.com/mysql_dump_debug_info ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE if( mysql_dump_debug_info( mysql )) { printf("Debug dump error: %s\n", mysql_error(mysql)); } else { puts( "Debug Dump OK, check server error log for output\n"); } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return EXIT_SUCCESS; }
mysql_errno /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_errno() * see also http://mysql.com/mysql_errno * MySQL C client API examples: mysql_errno() ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE mysql_query( mysql, "this is not valid SQL" ); printf( "MySQL errno: %u \n", mysql_errno( mysql )); // ERROR INFORMATION IS NOT RESET BY CALLS TO mysql_errno() printf( "MySQL errno: %u \n", mysql_errno( mysql )); helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_error /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_error() see also http://mysql.com/mysql_error ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE mysql_query( mysql, "this is not valid SQL" ); printf( "MySQL error (expected): %s\n", mysql_error( mysql )); // ERROR INFORMATION IS NOT RESET BY CALLS TO mysql_error() printf("MySQL error (expected): %s\n", mysql_error( mysql )); helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_fetch_field /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_fetch_fields() see also http://mysql.com/mysql_fetch_fields ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv ) { int i; MYSQL *mysql; MYSQL_RES *result; MYSQL_FIELD *field; mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE mysql_query( mysql, "SET NAMES utf8" ); if( mysql_query( mysql, "SELECT * FROM mysql.help_keyword LIMIT 1")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { result = mysql_store_result( mysql ); if( !result ) { printf( "Couldn't get results set: %s\n", mysql_error( mysql )); } else { i = 0; while( NULL != (field = mysql_fetch_field( result ))) { printf( "FIELD #%d\n", ++i); printf( " %-20s %s\n", "Field name", field->name); #if MYSQL_VERSION_ID >= 40100 printf( " %-20s %s\n", "Original name", field->org_name); #endif printf( " %-20s %s\n", "From table", field->table); printf( " %-20s %s\n", "Original name", field->org_table); printf( " %-20s %s\n", "Database", field->db); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %s\n", "Catalog", field->catalog); #endif printf( " %-20s %s\n", "Default", field->def); printf( " %-20s %lu\n", "CREATE field length", field->length); printf( " %-20s %lu\n", "MAX field lengt", field->max_length); #if MYSQL_VERSION_ID >= 40100 printf( " %-20s %u\n", "Field name length", field->name_length); printf( " %-20s %u\n", "Original name length", field->org_name_length); printf( " %-20s %u\n", "Table name length", field->table_length); printf( " %-20s %u\n", "Original name length", field->org_table_length); printf( " %-20s %u\n", "DB name length", field->db_length); printf( " %-20s %u\n", "Catalog name length", field->catalog_length); printf( " %-20s %u\n", "Default length", field->def_length); #endif /* TODO: decimals */ printf( "\n" ); } } } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_fetch_fields /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_fetch_fields() see also http://mysql.com/mysql_fetch_fields ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv ) { unsigned int i, num_fields; MYSQL *mysql MYSQL_RES *result; MYSQL_FIELD *fields; mysql = helper_connect( argc, argv ); // SEE helper.h FOR ACTUAL CODE mysql_query( mysql, "SET NAMES utf8" ); if( mysql_query( mysql, "SELECT LPAD( i, 7, ' ') as t FROM test.f LIMIT 1")) { printf( "Query failed: %s\n", mysql_error( mysql )); } else { result = mysql_store_result( mysql ); if( !result ) { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } else { fields = mysql_fetch_fields( result ); if( !fields ) { printf( "Faild fetching fields: %s\n", mysql_error( mysql )); } else { num_fields = mysql_num_fields( result ); for( i = 0; i < num_fields; i++) { printf( "FIELD #%d\n", i ); printf( " %-20s %s\n", "Field name", fields[i].name); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %s\n", "Original name", fields[i].org_name); #endif printf(" %-20s %s\n", "From table", fields[i].table); printf(" %-20s %s\n", "Original name", fields[i].org_table); printf(" %-20s %s\n", "Database", fields[i].db); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %s\n", "Catalog", fields[i].catalog); #endif printf(" %-20s %s\n", "Default", fields[i].def); printf(" %-20s %lu\n", "CREATE field length" , fields[i].length); printf(" %-20s %lu\n", "MAX field lengt" , fields[i].max_length); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %u\n", "Field name length" , fields[i].name_length); printf(" %-20s %u\n", "Original name length" , fields[i].org_name_length); printf(" %-20s %u\n", "Table name length" , fields[i].table_length); printf(" %-20s %u\n", "Original name length" , fields[i].org_table_length); printf(" %-20s %u\n", "DB name length" , fields[i].db_length); printf(" %-20s %u\n", "Catalog name length" , fields[i].catalog_length); printf(" %-20s %u\n", "Default length" , fields[i].def_length); #endif // TODO: decimals printf("\n"); } } } } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE returna( EXIT_SUCCESS ); }
mysql_fetch_field_direct /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_fetch_field_direct() see also https://dev.mysql.com/doc/c-api/8.3/en/mysql-fetch-field-direct.html *********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { unsigned int i, num_fields; MYSQL *mysql MYSQL_RES *result; MYSQL_FIELD *field; mysql = helper_connect(argc, argv); // SEE helper.h FOR ACTUAL CODE if( mysql_query( mysql, "SELECT * FROM mysql.help_keyword LIMIT 1")) { printf( "Query failed: %s\n", mysql_error(mysql)); } else { result = mysql_store_result( mysql ); if( !result ) { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } else { num_fields = mysql_num_fields(result); for( i = 0; i < num_fields; i++) { field = mysql_fetch_field_direct( result, i); if( !field ) { printf("Faild fetching field #%u: %s\n", i , mysql_error( mysql )); break; } printf("FIELD #%d\n", i); printf(" %-20s %s\n", "Field name", field->name); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %s\n", "Original name", field->org_name); #endif printf(" %-20s %s\n", "From table", field->table); printf(" %-20s %s\n", "Original name", field->org_table); printf(" %-20s %s\n", "Database", field->db); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %s\n", "Catalog", field->catalog); #endif printf(" %-20s %s\n", "Default", field->def); printf(" %-20s %lu\n", "CREATE field length", field->length); printf(" %-20s %lu\n", "MAX field lengt", field->max_length); #if MYSQL_VERSION_ID >= 40100 printf(" %-20s %u\n", "Field name length", field->name_length); printf(" %-20s %u\n", "Original name length", field->org_name_length); printf(" %-20s %u\n", "Table name length", field->table_length); printf(" %-20s %u\n", "Original name length", field->org_table_length); printf(" %-20s %u\n", "DB name length", field->db_length); printf(" %-20s %u\n", "Catalog name length", field->catalog_length); printf(" %-20s %u\n", "Default length", field->def_length); #endif /* TODO: flags, decimals, charset, field type */ printf("\n"); } } } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
helpers.h /*F******************************************************* * *********************************************************/ #ifndef _HELPERS_H #define _HELPERS_H 1 #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ MYSQL* helper_connect( int argc, char **argv ) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql) { fprintf( stderr, "Init faild, out of memory?"); exit( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql // MYSQL STRUCTURE TO USE , NULL // SERVER HOSTNAME OR IP ADDRESS , NULL // MYSQL USER , NULL // PASSWORD , NULL // DEFAULT DATABASE TO USE, NULL FOR NONE , 0 // PORT NUMBER, 0 FOR DEFAULT , NULL // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS // CONNECTION FLAGS ) ) { fprintf( stderr, "Connect failed\n"); mysql_library_end(); exit( EXIT_FAILURE ); } return( mysql ); } /*F******************************************************* * *********************************************************/ void helper_end( MYSQL *mysql) { mysql_close( mysql ); mysql_library_end(); } #endif /* _HELPERS_H */
mysql_fetch_row /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_fetch_row() * see also http://mysql.com/mysql_fetch_row ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { int i; unsigned int num_fields; MYSQL_ROW row; MYSQL *mysql = NULL; MYSQL_RES *result; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS )) // CONNECTION FLAGS { puts( "Connect failed\n"); } else { if( mysql_query( mysql, "SELECT * FROM City LIMIT 10")) { printf( "Query failed: %s\n", mysql_error(mysql)); } else { result = mysql_store_result( mysql ); if( !result ) { printf( "Couldn't get results set: %s\n", mysql_error(mysql)); } else { num_fields = mysql_num_fields( result ); while( (row = mysql_fetch_row( result ))) { for( i = 0; i < num_fields; i++) { printf("%s, ", row[i]); } putchar( '\n' ); } mysql_free_result( result ); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_field_count /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_field_count() * see also http://mysql.com/mysql_field_count ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> #include "helpers.h" /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = helper_connect( argc, argv); // SEE helper.h FOR ACTUAL CODE if( mysql_query( mysql, "SELECT * FROM City")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( !result ) { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } else { printf( "Current result set has %u fields\n" , mysql_field_count( mysql )); mysql_free_result( result ); } } helper_end( mysql ); // SEE helper.h FOR ACTUAL CODE return( EXIT_SUCCESS ); }
mysql_free_result /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_free_result() see also http://mysql.com/mysql_free_result ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS // CONNECTION FLAGS )) { puts( "Connect failed\n"); } else { if( mysql_query( mysql, "SELECT VERSION()")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( result ) { puts("Got a valid result set\n"); mysql_free_result( result ); } else { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_init /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_free_result() * see also http://mysql.com/mysql_free_result ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts("Connect failed\n"); } else { if( mysql_query( mysql, "SELECT VERSION()")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( result ) { puts( "Got a valid result set\n"); mysql_free_result( result ); } else { printf("Couldn't get results set: %s\n", mysql_error( mysql )); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_num_fields /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_num_fields() * see also http://mysql.com/mysql_num_fields ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // port number, 0 for default */ NULL, // socket file or named pipe name */ CLIENT_FOUND_ROWS /* connection flags */ )) { puts("Connect failed\n"); } else { if( mysql_query( mysql, "SELECT * FROM City")) { printf("Query failed: %s\n", mysql_error( mysql )); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( !result ) { printf( "Couldn't get results set: %s\n" , mysql_error( mysql )); } else { printf("Current result set has %u fields\n" , mysql_num_fields( result )); mysql_free_result( result ); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_num_rows /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_num_fields() * see also http://mysql.com/mysql_num_fields ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options(mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf" ); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts( "Connect failed\n" ); } else { if( mysql_query( mysql, "SELECT * FROM City")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( !result ) { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } else { printf( "Current result set has %u fields\n" , mysql_num_fields( result )); mysql_free_result( result ); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_options /*H******************************************************* Copyright (C) 2005 - 2022 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_options() * see also http://mysql.com/mysql_options ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT , (const char*)5 /* 5sek */ )) { printf( "MySQL Options failed: %s\n", mysql_error( mysql )); } else { puts("MySQL Options OK\n"); } if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts("Connect failed\n"); } else { puts( "Connect OK\n"); } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_opt_user_data /* GET NUMBER OF CONNECTION ATTRIBUTES */ int i, elements= 0; char **key, **value; mysql_get_optionv( mysql, MYSQL_CONNECT_ATTRS, NULL, NULL, (void*)&elements); key= (char **)malloc( sizeof( char* ) * elements); val= (char **)malloc( sizeof( char* ) * elements); mysql_get_optionv( mysql, MYSQL_OPT_CONNECT_ATTRS, &key, &val , &elements); for( i =0; i < elements; i++) printf("key: %s value: %s", key[i], val[i]);
mysql_query /*H******************************************************* Copyright (C) 2005 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_query() * see also http://mysql.com/mysql_query ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <mysql.h> #include "helpers.h" static char *server_groups[] = { "embedded", "server", (char *)NULL }; /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql; int i; if( mysql_server_init( argc, argv, server_groups)) { fputs( "server init failed", stderr); return( EXIT_FAILURE ); } mysql_thread_init(); mysql = mysql_init( NULL ); mysql_options( mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client"); mysql_options( mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR ip ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts( "Connect failed\n"); } else { for( i = 0; i < 10; i++) { printf( "iteration %d\n", i); MYSQL_STMT* stmt = mysql_stmt_init( mysql ); char* command = "SELECT 1 FROM i1"; // char* command = "UPDATE T1 SET i1 = 1"; mysql_stmt_prepare( stmt, command, strlen( command )); mysql_stmt_execute( stmt ); mysql_stmt_store_result( stmt ); while (!mysql_stmt_fetch( stmt )) {} mysql_stmt_free_result( stmt ); mysql_stmt_close( stmt ); } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_real_query /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_real_query() * see also http://mysql.com/mysql_real_query ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main(int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void *)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS // CONNECTION FLAGS )) { puts(" Connect failed\n"); } else { const char *query = "SELECT VERSION()"; if( mysql_real_query( mysql, query, strlen( query ))) { printf("Query failed: %s\n", mysql_error( mysql )); } else { puts( "Query OK"); } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_real_connect /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_real_connect() * see also http://mysql.com/mysql_real_connect ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts("Init faild, out of memory?"); return EXIT_FAILURE; } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts("Connect failed\n"); } else { puts( "Connect OK\n"); } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_store_result /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA MySQL C client API example: mysql_store_result() see also http://mysql.com/mysql_store_result ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf(stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts( "Connect failed\n" ); } else { if( mysql_query( mysql, "SELECT VERSION()")) { printf("Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result( mysql ); if( result) { puts( "Got a valid result set\n"); mysql_free_result( result ); } else { printf( "Couldn't get results set: %s\n" , mysql_error( mysql) ); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_use_result /*H******************************************************* Copyright (C) 2005 - 2019 Hartmut Holzgraefe This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * MySQL C client API example: mysql_use_result() * see also http://mysql.com/mysql_use_result ********************************************************/ #include "config.h" #include <stdlib.h> #include <stdio.h> #include <mysql.h> /*F******************************************************* * *********************************************************/ int main( int argc, char **argv) { MYSQL *mysql = NULL; if( mysql_library_init( argc, argv, NULL)) { fprintf( stderr, "could not initialize MySQL client library\n"); exit( 1 ); } mysql = mysql_init( mysql ); if( !mysql ) { puts( "Init faild, out of memory?"); return( EXIT_FAILURE ); } mysql_options( mysql, MYSQL_READ_DEFAULT_FILE, (void*)"./my.cnf"); if( !mysql_real_connect( mysql, // MYSQL STRUCTURE TO USE NULL, // SERVER HOSTNAME OR IP ADDRESS NULL, // MYSQL USER NULL, // PASSWORD NULL, // DEFAULT DATABASE TO USE, NULL FOR NONE 0, // PORT NUMBER, 0 FOR DEFAULT NULL, // SOCKET FILE OR NAMED PIPE NAME CLIENT_FOUND_ROWS /* CONNECTION FLAGS */ )) { puts("Connect failed\n"); } else { if( mysql_query( mysql, "SELECT VERSION()")) { printf( "Query failed: %s\n", mysql_error(mysql)); } else { MYSQL_RES *result = mysql_use_result( mysql ); if( result ) { puts( "Got a valid result set\n"); mysql_free_result( result ); } else { printf("Couldn't get results set: %s\n", mysql_error(mysql)); } } } mysql_close( mysql ); mysql_library_end(); return( EXIT_SUCCESS ); }
mysql_stmt_attr_set bool mysql_stmt_attr_set( MYSQL_STMT *stmt , enum enum_stmt_attr_type option, const void *arg) Example The following example opens a cursor for a prepared statement and sets the number of rows to fetch at a time to 5: MYSQL_STMT *stmt; int rc; unsigned long type; unsigned long prefetch_rows = 5; stmt = mysql_stmt_init(mysql); type = (unsigned long) CURSOR_TYPE_READ_ONLY; rc = mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type); /* ... check return value ... */ rc = mysql_stmt_attr_set( stmt, STMT_ATTR_PREFETCH_ROWS, (void*) &prefetch_rows); /* ... check return value ... */