MySQL Connector Programming

Source: Internet
Author: User
Tags goto

MySQL Connector is an interface for MySQL database client programming that provides interfaces for accessing databases over the network, which are implemented in a dynamic-link library (. dll,. So) or a static object library (. lib,. a).

You must be aware of whether these libraries are 32-bit or 64-bit when used.

Here is an example:

#include <stdio.h>
#include<stdlib.h>#include<c:\program Files\mysql\mysql Connector C6.1\include\mysql.h>//using the static object library//#pragma comment (lib, "C:\\Program Files\\mysql\\mysql Connector C 6.1\\lib\\vs12\\mysqlclient.lib")//using the dynamic link library//Make sure that Libmysql.dll can be found in the system path#pragmaComment (Lib, "C:\\Program Files\\mysql\\mysql Connector C 6.1\\lib\\libmysql.lib")voidSimpleusega () {MYSQL*Conn; Conn=Mysql_init (NULL); if(conn = =NULL) {printf ("Error%u:%s\n", Mysql_errno (conn), MYSQL_ERROR (conn)); Exit (1); } if(Mysql_real_connect (Conn,"localhost","user_name", "User_password"Null0Null0) ==NULL) {printf ("Error%u:%s\n", Mysql_errno (conn), MYSQL_ERROR (conn)); Exit (1); } if(Mysql_query (Conn,"CREATE DATABASE frist_db") {printf ("Error%u:%s\n", Mysql_errno (conn), MYSQL_ERROR (conn)); Exit (1); } mysql_close (conn);} int main () {MYSQL*mysql =NULL; Charpwd[1024x768]; Charusr[1024x768]; printf ("Target Platform Word length:%d \ n",sizeof(void*) ); printf ("Connector version:%s \ n", Mysql_get_client_info ()); //Simpleusage (); //return 0;printf ("Initializing MySQL Connector ... \ n");
Mysql_library_init (0, NULL, NULL);//initialize the MySQL C library before other work threads are generated, and do not let mysql_init call it, or it may cause thread safety issues if(! (MySQL =Mysql_init (NULL))) {printf ("Field. \ n"); Gotoend; } printf ("OK, conecting ... \ n");

Configure users and Passwordsif(0) {printf ("Please keyin user_name and password \ n" "Name:"); scanf_s ("%s"Usr1024x768); printf ("pwd:"); scanf_s ("%s"Pwd1024x768); } Else{sprintf_s (usr,1024x768,"Default_user_name"); sprintf_s (PWD,1024x768,"Default_user_password"); }

Connect to server on localhostif(!mysql_real_connect (MySQL,"localhost", USR, pwd, (Const Char*)0,3306Null0) {printf ("Filed, Error%u,%s \ n", Mysql_errno (MySQL), mysql_error (MySQL)); Gotoend; } printf ("Login succeed. \ n"); //Destroy Passwordsprintf_s (PWD,1024x768,"00000000000000");
Query database server time mysql_query (MySQL,"SELECT now ();"); if(!Mysql_errno (MySQL)) {Mysql_res*result; Mysql_row ROW; intNum_fields; inti; Result=mysql_store_result (MySQL); Num_fields=mysql_num_fields (Result); while(row =mysql_fetch_row (Result))) { for(i =0; i < Num_fields; i++) {printf ("%s", Row[i]? Row[i]:"NULL"); } printf ("\ n"); } mysql_free_result (Result); }end:system ("Pause"); Mysql_close (MySQL); Mysql_library_end ();
return 0;}

MySQL Connector Programming

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.