#include <winsock2.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <mysql.h> #pragma comment(lib,"libmysql") int _tmain(int argc, _TCHAR* argv[]) { MYSQL* mysql; MYSQL_RES* results; MYSQL_ROW record; mysql = mysql_init(NULL); if(!mysql) { printf("mysql_init error!/n"); } if(!mysql_real_connect(mysql, NULL,"root","sbivfh", "mysql", 3306,NULL,0)) { printf("Failed to connect to database: Error: %s/n",mysql_error(mysql)); } if(mysql_query(mysql,"select User from user")) { printf("mysql_query: Error: %s/n",mysql_error(mysql)); } results = mysql_store_result(mysql); my_ulonglong ulNum = mysql_num_rows(results); while(record = mysql_fetch_row(results)) { printf("%s",record[0]); } mysql_free_result(results); mysql_close(mysql); mysql_server_end(); return 0; }
//use proc
--2009-09-21
#include <winsock2.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <mysql.h> #pragma comment(lib,"libmysql") int _tmain(int argc, _TCHAR* argv[]) { MYSQL* mysql; MYSQL_RES* results; MYSQL_ROW record; char szProcQueryUser[256]; ZeroMemory(szProcQueryUser,256); /* create table user1 (userid int not null); delimiter // create procedure queryuser1 () begin insert into user1 (userid) values (5); end // */ mysql = mysql_init(NULL); if(!mysql) { printf("mysql_init error!/n"); } if(!mysql_real_connect(mysql, NULL,"root","sbivfh", "mysql", 3306,NULL,0)) { printf("Failed to connect to database: Error: %s/n",mysql_error(mysql)); } strcpy(szProcQueryUser,"call queryuser1()"); if(mysql_real_query(mysql,szProcQueryUser,(unsigned int)strlen(szProcQueryUser))) { printf("mysql_real_query: Error: %s/n",mysql_error(mysql)); } if(mysql_query(mysql,"select userid from user1")) { printf("mysql_query: Error: %s/n",mysql_error(mysql)); } results = mysql_store_result(mysql); my_ulonglong ulNum = mysql_num_rows(results); while(record = mysql_fetch_row(results)) { printf("%s",record[0]); } mysql_free_result(results); mysql_close(mysql); mysql_server_end(); return 0; }
//Prepared Statement
#include<windows.h> #include<mysql.h> #pragma comment(lib,"libmysql") #define STRING_SIZE 50 #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table" #define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,col2 VARCHAR(40),col3 SMALLINT,col4 TIMESTAMP)" #define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)" int _tmain(int argc, _TCHAR* argv[]) { MYSQL* mysql; MYSQL_STMT *stmt; MYSQL_BIND bind[3]; my_ulonglong affected_rows; int param_count; short small_data; int int_data; char str_data[STRING_SIZE]; unsigned long str_length; my_bool is_null; mysql = mysql_init(NULL); if(!mysql) { printf("mysql_init error!/n"); } if(!mysql_real_connect(mysql, NULL,"root","sbivfh", "mysql", 3306,NULL,0)) { printf("Failed to connect to database: Error: %s/n",mysql_error(mysql)); } if (mysql_query(mysql, DROP_SAMPLE_TABLE)) { fprintf(stderr, " DROP TABLE failed/n"); fprintf(stderr, " %s/n", mysql_error(mysql)); exit(0); } if (mysql_query(mysql, CREATE_SAMPLE_TABLE)) { fprintf(stderr, " CREATE TABLE failed/n"); fprintf(stderr, " %s/n", mysql_error(mysql)); exit(0); } /* Prepare an INSERT query with 3 parameters */ /* (the TIMESTAMP column is not named; the server */ /* sets it to the current date and time) */ stmt = mysql_stmt_init(mysql); if (!stmt) { fprintf(stderr, " mysql_stmt_init(), out of memory/n"); exit(0); } if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE))) { fprintf(stderr, " mysql_stmt_prepare(), INSERT failed/n"); fprintf(stderr, " %s/n", mysql_stmt_error(stmt)); exit(0); } fprintf(stdout, " prepare, INSERT successful/n"); /* Get the parameter count from the statement */ param_count= mysql_stmt_param_count(stmt); fprintf(stdout, " total parameters in INSERT: %d/n", param_count); // validate parameter count if (param_count != 3) { fprintf(stderr, " invalid parameter count returned by MySQL/n"); exit(0); } /* Bind the data for all 3 parameters */ memset(bind, 0, sizeof(bind)); /* INTEGER PARAM *//* This is a number type, so there is no need to specify buffer_length */ bind[0].buffer_type= MYSQL_TYPE_LONG; bind[0].buffer= (char *)&int_data; bind[0].is_null= 0; bind[0].length= 0; /* STRING PARAM */ bind[1].buffer_type= MYSQL_TYPE_STRING; bind[1].buffer= (char *)str_data; bind[1].buffer_length= STRING_SIZE; bind[1].is_null= 0; bind[1].length= &str_length; /* SMALLINT PARAM */ bind[2].buffer_type= MYSQL_TYPE_SHORT; bind[2].buffer= (char *)&small_data; bind[2].is_null= &is_null; bind[2].length= 0; /* Bind the buffers */ if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed/n"); fprintf(stderr, " %s/n", mysql_stmt_error(stmt)); exit(0); } /* Specify the data values for the first row */ int_data= 10; /* integer */ strncpy(str_data, "MySQL", STRING_SIZE); /* string */ str_length= strlen(str_data); /* INSERT SMALLINT data as NULL */ is_null= 1; /* Execute the INSERT statement - 1*/ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), 1 failed/n"); fprintf(stderr, " %s/n", mysql_stmt_error(stmt)); exit(0); } /* Get the total number of affected rows */ affected_rows= mysql_stmt_affected_rows(stmt); fprintf(stdout, " total affected rows(insert 1): %lu/n",(unsigned long) affected_rows); /* validate affected rows */ if (affected_rows != 1) { fprintf(stderr, " invalid affected rows by MySQL/n"); exit(0); } /* Specify data values for second row, then re-execute the statement */ int_data= 1000;strncpy(str_data, "The most popular Open Source database", STRING_SIZE); str_length= strlen(str_data); small_data= 1000; /* smallint */ is_null= 0; /* reset */ /* Execute the INSERT statement - 2*/ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute, 2 failed/n"); fprintf(stderr, " %s/n", mysql_stmt_error(stmt)); exit(0); } /* Get the total rows affected */ affected_rows= mysql_stmt_affected_rows(stmt); fprintf(stdout, " total affected rows(insert 2): %lu/n", (unsigned long) affected_rows); if (affected_rows != 1)/* validate affected rows */ { fprintf(stderr, " invalid affected rows by MySQL/n"); exit(0); } /* Close the statement */ if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing the statement/n"); fprintf(stderr, " %s/n", mysql_stmt_error(stmt)); exit(0); } return 0; }
//stroe binary data
#include <winsock2.h> #include <mysql.h> #pragma comment (lib,"libmysql.lib") #define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(4, ?)" int _tmain(int argc, _TCHAR* argv[]) { MYSQL_BIND bind[1]; unsigned long length; char blog_data[100] = {0}; memset(blog_data, 0x01, sizeof(blog_data)); char* pos = blog_data; int size = 50; MYSQL *mysql = mysql_init(NULL); if(!mysql) { printf("mysql_init error!/n"); } if(!mysql_real_connect(mysql, NULL,"root","sbivfh", "test", 3306,NULL,0)) { printf("Failed to connect to database: Error: %s/n",mysql_error(mysql)); } MYSQL_STMT *stmt = mysql_stmt_init(mysql); if (!stmt) { printf("mysql_stmt_init(), out of memory/n"); } if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY))) { printf("mysql_stmt_prepare(), INSERT failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } memset(bind, 0, sizeof(bind)); //bind[0].buffer_type= MYSQL_TYPE_STRING; //bind[0].buffer_type = MYSQL_TYPE_LONG; bind[0].buffer = blog_data; //bind[0].buffer_type = MYSQL_TYPE_TINY; bind[0].buffer_type = MYSQL_TYPE_BLOB; bind[0].length= &length; bind[0].is_null= 0; /* Bind the buffers */ if (mysql_stmt_bind_param(stmt, bind)) { printf("param bind failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } /* Supply data in chunks to server */ if (mysql_stmt_send_long_data(stmt,0, pos, size)) { printf("send_long_data failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } pos += size; /* Supply the next piece of data */ if (mysql_stmt_send_long_data(stmt,0, pos, size)) { printf("send_long_data failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } /* Now, execute the query */ if (mysql_stmt_execute(stmt)) { printf("mysql_stmt_execute failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } return 0; }
#include <winsock2.h> #include <mysql.h> #pragma comment (lib,"libmysql.lib") #define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(4, ?)" /* CREATE TABLE `bintest` ( `id` int(11) NOT NULL default '0', `data` blob ) */ int _tmain(int argc, _TCHAR* argv[]) { DWORD dwArray[10]; memset(dwArray,0,sizeof(DWORD)*10); for(int i = 0; i < 10; ++i) { dwArray[i] = i; } MYSQL_BIND bind[1]; unsigned long length; char blog_data[100] = {0}; memcpy(blog_data,(char*)dwArray,sizeof(DWORD)*10); //memset(blog_data, 0x01, sizeof(blog_data)); char* pos = blog_data; int size = 50; MYSQL *mysql = mysql_init(NULL); if(!mysql) { printf("mysql_init error!/n"); } if(!mysql_real_connect(mysql, NULL,"root","sbivfh", "test", 3306,NULL,0)) { printf("Failed to connect to database: Error: %s/n",mysql_error(mysql)); } MYSQL_STMT *stmt = mysql_stmt_init(mysql); if (!stmt) { printf("mysql_stmt_init(), out of memory/n"); } if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY))) { printf("mysql_stmt_prepare(), INSERT failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } memset(bind, 0, sizeof(bind)); //bind[0].buffer_type= MYSQL_TYPE_STRING; //bind[0].buffer_type = MYSQL_TYPE_LONG; bind[0].buffer = blog_data; //bind[0].buffer_type = MYSQL_TYPE_TINY; bind[0].buffer_type = MYSQL_TYPE_BLOB; bind[0].length= &length; bind[0].is_null= 0; /* Bind the buffers */ if (mysql_stmt_bind_param(stmt, bind)) { printf("param bind failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } /* Supply data in chunks to server */ if (mysql_stmt_send_long_data(stmt,0, pos, size)) { printf("send_long_data failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } pos += size; /* Supply the next piece of data */ if (mysql_stmt_send_long_data(stmt,0, pos, size)) { printf("send_long_data failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } /* Now, execute the query */ if (mysql_stmt_execute(stmt)) { printf("mysql_stmt_execute failed/n"); printf("%s/n", mysql_stmt_error(stmt)); } //read MYSQL_RES* results; MYSQL_ROW record; if(mysql_query(mysql,"select id,data from bintest")) { printf("mysql_query: Error: %s/n",mysql_error(mysql)); } results = mysql_store_result(mysql); my_ulonglong ulNum = mysql_num_rows(results); while(record = mysql_fetch_row(results)) { printf("%s",record[0]); memset(dwArray,0,sizeof(DWORD)*10); memcpy((char*)dwArray,record[1],sizeof(DWORD)*10); } mysql_free_result(results); return 0; }
//use odbc
#include "stdafx.h" #include <windows.h> #include <sqltypes.h> #include <sqlext.h> int main() { int nResCode = -1; SQLHENV hEnv; SQLHDBC hDbc; SQLHDBC hStmt; //// Allocate the environment handle. nResCode = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnv); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { // Set the version environment attribute. nResCode = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { // Allocate the connection handle. nResCode = SQLAllocHandle(SQL_HANDLE_DBC,hEnv,&hDbc); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { nResCode = SQLConnect(hDbc,_T("mysql_odbc"),SQL_NTS,_T("root"),SQL_NTS,_T("sbivfh"),SQL_NTS); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { // Allocate statement handle nResCode = SQLAllocHandle(SQL_HANDLE_STMT,hDbc,&hStmt); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { nResCode = SQLExecDirect(hStmt,(SQLWCHAR *)_T("select id,account,password from account;"),SQL_NTS); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { // Bind columns int nID = 0; char szUser[BUFSIZ]; char szPwd[BUFSIZ]; memset(szUser,0,BUFSIZ); memset(szPwd,0,BUFSIZ); SQLLEN nLen = 0; SQLLEN nUser = 0; SQLLEN nPwd = 0; nResCode = SQLBindCol(hStmt, 1, SQL_INTEGER, &nID, BUFSIZ, &nLen); nResCode = SQLBindCol(hStmt, 2, SQL_CHAR, &szUser, BUFSIZ, &nUser); nResCode = SQLBindCol(hStmt, 3, SQL_CHAR, &szPwd, BUFSIZ, &nPwd); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { // Fetch and print each row of data. On an error, display a message and exit. for (int i = 0; ; i++) { nResCode = SQLFetch(hStmt); if(SQL_SUCCESS == nResCode || SQL_SUCCESS_WITH_INFO == nResCode) { printf("%d-%d-%s-%s/n",i + 1,nID,szUser,szPwd); } else { break; } } } } } // Process data if (nResCode == SQL_SUCCESS || nResCode == SQL_SUCCESS_WITH_INFO) { SQLCancel(hStmt); SQLFreeHandle(SQL_HANDLE_STMT, hStmt); } SQLDisconnect(hDbc); } } SQLFreeHandle(SQL_HANDLE_DBC,hDbc); } SQLFreeHandle(SQL_HANDLE_ENV,hEnv); } return 0; }
other
mysql> delimiter // mysql> create function hello (s char(20)) returns char(50) -> return concat ('hello,',s,'!'); -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter // mysql> create procedure simpleproc (OUT param1 int) -> begin -> select count(*) into param1 from user; -> end -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter // mysql> create procedure handleerdemo() -> begin -> declare continue handler for sqlstate '23000' set @x2 = 1; -> set @x = 1; -> insert into mysql.hanletest values (1); -> set @x = 2; -> insert into mysql.hanletest values (1); -> set @x = 3; -> end; -> //
MySQL++调用存储过程http://yoyo.is-programmer.com/posts/12108.html