1、配置
VC++目录中添加:
包含目录:C:\Program Files\MySQL\MySQL Server 5.7\include
库目录:C:\Program Files\MySQL\MySQL Server 5.7\lib
C:\Program Files\MySQL\MySQL Server 5.7\lib\debug
连接器输入中添加:libmysql.lib
将C:\Program Files\MySQL\MySQL Server 5.7\lib文件夹中的libmysql.lib文件拷贝到所建工程的debug文件夹下。
2.在命令提示符下的一些简单操作
(1)select user,host from mysql.user;
(2)show databases;
(3)create database bookstore;
(4)use bookstore;
(5)create table books(book_id INT, title varchar(50),author varchar(50));
(6)describe books;
(7)show tables
(8)alter table books
insert into books(book_id , title ,author) values(10001,'The End of the affairs', 'Greene Graham'); / /注意只有表项名,没有数据类型;
insert into books(book_id , title ,author) values(10002,'.......', '..........');
select *from books;
3.c语言调用mysql
#include <stdio.h>
#include <string>
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "mysql.h"
using namespace std;
int main()
{
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
if(mysql_init(&mysql)==NULL) //mysql前必须加上&号
{
fprintf(stderr,"cannot initialize MYSQL");
return 1;
}
if(!mysql_real_connect(&mysql,"localhost","root","wy19920318","bookstore",0,NULL,0)) //"mysql"为默认数据库
{
fprintf(stderr,"cannot connect mysql");
return 1;
}
//查询mysql数据库中的列
if(mysql_query(&mysql,"select * from books"))//"select * from books表示选择表books中的所有列
{
fprintf(stderr,"%d: %s \n",mysql_errno(&mysql),mysql_error(&mysql)); //mysql_errno函数输出最后一次调用API函数的错误代号,mysql_error输出错误描述
return 1;
}
else
{
result=mysql_store_result(&mysql); //获取查询结果并用函数mysql_store_result将结果存入内存
while(row=mysql_fetch_row(result)) //一次在结果集中取出一行数据,并将每行数据存储在变量row中
{printf("%d %s %s \n",row[0],row[1],row[2]);}
cvWaitKey(0);
mysql_free_result(result);
}
mysql_close(&mysql);
return 0;
}
该程序还有点问题,输出的book_id不对。