- //vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路径
- //在工程设置-》链接》库模块中添加 libmysql.lib
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <winsock.h>
- #include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
- void main(){
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *server ="localhost";
- char *user ="root";
- char *password="";
- char *database="test";
- char sql[1024]="select * from chinaren";
- conn=mysql_init(NULL);
-
- if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
- fprintf(stderr,"%s\n",mysql_error(conn));
- exit(1);
- }
-
- if(mysql_query(conn,sql)){
- fprintf(stderr,"%s\n",mysql_error(conn));
- exit(1);
- }
-
- res=mysql_use_result(conn);
-
- while((row = mysql_fetch_row(res))!=NULL){
- printf("%s\n",row[2]);
- }
-
- mysql_free_result(res);
- mysql_close(conn);
- }
===============================
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译 |
09 |
#define SELECT_QUERY "show tables;" |
11 |
int main( int argc, char **argv) |
21 |
if (!(handle = mysql_real_connect(&mysql, "localhost" , "user" , "pwd" , "dbname" ,0,NULL,0))) { |
22 |
fprintf (stderr, "Couldn't connect to engine!\n%s\n\n" ,mysql_error(&mysql)); |
24 |
sprintf (querysql,SELECT_QUERY, atoi (argv[1])); |
26 |
if (mysql_query(handle,querysql)) { |
27 |
fprintf (stderr, "Query failed (%s)\n" ,mysql_error(handle)); |
30 |
if (!(result=mysql_store_result(handle))) { |
31 |
fprintf (stderr, "Couldn't get result from %s\n" , mysql_error(handle)); |
33 |
printf ( "number of fields returned: %d\n" ,mysql_num_fields(result)); |
35 |
while (row = mysql_fetch_row(result)) { |
36 |
printf ( "table: %s\n" ,(((row[0]==NULL)&&(! strlen (row[0]))) ? "NULL" : row[0]) ) ; |
39 |
mysql_free_result(result); |
|