直接套用框架
1\ VS的环境需要注意
2\ 头文件的顺序需要注意
// mysqldemo.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <WinSock2.h> //Socket,连接MySQL也需要用的 #pragma comment(lib,"ws2_32.lib") #include <mysql.h> //包含MySQL所需要的头文件 //download:http://dev.mysql.com/downloads/connector/ MYSQL mysql, *sock; //声明MySQL的句柄 int main(void) { const char * host = "127.0.0.1"; //因为是作为本机测试,所以填写的是本地IP const char * user = "root"; //这里改为你的用户名,即连接MySQL的用户名 const char * passwd = "root"; //这里改为你的用户密码 const char * db = "student"; //这里改为你要连接的数据库的名字 unsigned int port = 3306; //这是MySQL的服务器的端口,如果你没有修改过的话就是3306。 const char * unix_socket = NULL; //unix_socket这是unix下的,我在Windows下,所以就把它设置为NULL unsigned long client_flag = 0; //这个参数一般为0 char * i_query = new char[1024]; if (i_query==NULL) { return 0; } memset(i_query,0,1024); MYSQL_RES * result; //保存结果集的 MYSQL_ROW row; //代表的是结果集中的一行 mysql_init(&mysql); //连接之前必须使用这个函数来初始化 if ( (sock = mysql_real_connect(&mysql, host, user, passwd, db, port, unix_socket, client_flag) ) == NULL ) //连接MySQL { printf("连接失败,原因是: \n"); fprintf(stderr, " %s\n", mysql_error(&mysql)); exit(1); } else { fprintf(stderr, "连接MySQL成功!!\n"); } int n; printf("please input 0,1,2,3 to choose services: " "insert,select,update,delete:\n"); scanf_s("%d",&n); if (n==0) { char *name=new char[50],*sex=new char,*birthday=new char[50]; char *phone=new char[16],*native=new char[50]; int admissiondate; printf("please input the student name,sex(M/F),birthdaydate," "phone,admissiondate,nativeplace.\n" "(ex:Coco,F,1000-10-10 00:00:00,12345678912,2000,BeiJing)\n");//“,”处需要回车 // scanf_s("%s,%c,%s,%s,%d,%s",&name,&sex,&birthday,&phone,&admissiondate,&native); scanf_s("%s",name,50); fflush(stdin); scanf_s("%c",sex,1); fflush(stdin); gets_s(birthday,50); gets_s(phone,16); scanf_s("%d",&admissiondate); fflush(stdin); gets_s(native,50); sprintf_s(i_query,1024,"insert into student_basic(name,sex,birthdaydate," "phone,admissiondate,nativeplace) values(\'%s\',\'%c\'," "\'s\',\'%s\',\'%d\'\'%s\')", name,sex,birthday,phone,admissiondate,native); if (mysql_query(&mysql,i_query)!=0) { fprintf(stderr,"插入失败!\n"); exit(1); } else { fprintf(stderr,"插入成功!\n"); } } else if(n==1) { if ( mysql_query(&mysql, i_query) != 0 ) //如果连接成功,则开始查询 { fprintf(stderr, "查询失败!\n"); exit(1); } else { if ( (result = mysql_store_result(&mysql)) == NULL ) //保存查询的结果 { fprintf(stderr, "保存结果集失败!\n"); exit(1); } else { //row就是查询出来之后的每一行的记录 //row[0].....row[n]对应的每一行中某一列的记录 //row[i]对应的类型都是字符串 while ( (row = mysql_fetch_row(result)) != NULL ) //读取结果集中的数据,返回的是下一行。因为保存结果集时,当前的游标在第一行【之前】 { printf("id is %s\t\n", row[0]); //打印当前行的第一列的数据 printf("name is %s\t\n", row[1]); //打印当前行的第二列的数据 printf("sex is %s\t\n", row[2]); printf("birthday is %s\t\n", row[3]); printf("phone is %s\t\n", row[4]); printf("admissiondate is %s\t\n", row[5]); printf("nativeplace is %s\t\n", row[6]); printf("\n"); } } } } mysql_free_result(result); //释放结果集 mysql_close(sock); //关闭连接 system("pause"); exit(EXIT_SUCCESS); }
0则评论给“c语言mysql编程”