//在工程设置-》链接》库模块中添加 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平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定义数据库 *** 作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*handle //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result //查询结果集,结构类型
MYSQL_FIELD *field//包含字段信息的结构
MYSQL_ROW row //存放一行查询结果的字符串数组
char querysql[160] //存放查询sql语句字符串
//初始化
mysql_init(&mysql)
//连接数据库
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql))
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]))
//查询数据库
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle))
}
//存储结果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle))
}
printf("number of fields returned: %d\n",mysql_num_fields(result))
//读取结果集的内容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) )
}
//释放结果集
mysql_free_result(result)
//关闭数据库连接
mysql_close(handle)
system("PAUSE")
//为了兼容大部分的编译器加入此行
return 0
}
c++不支持MySQL连接,所以常见的有两种方法
1。 ODBC连接mysql。安装mysql后,再安装个mysql connection,你百度搜一下。 然后odbc创建mysql连接。
2. mysql自带的函数库
1、用CAPI连接MySQL数据库有两个步骤:1)初始化一个连接句柄
2)建立连接
所用到的函数如下:
MYSQL *mysql_init(MYSQL *connection) // 初始化连接句柄
//成功返回MySQL结构指针,失败返回NULL
MYSQL *mysql_real_connect(MYSQL *connection,
const char *server_host,
const char *sql_user_name,
const char *sql_password,
const char *db_name,
unsigned int port_number,
const char *unix_socket_name,
unsigned int flags) //建立连接
//成功返回MySQL结构指针,失败返回NULL
以下是完整实例:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std
void mysql_err_function(MYSQL * connection)
int main()
{
//freopen("input.txt","r",stdin)
MYSQL * connection
connection = mysql_init(NULL)
if (!connection)
{
cout <<"mysql_init failed!" <<endl
exit(-1)
}
if (!mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))
{
cout <<"Connection To MySQL failed!" <<endl
mysql_err_function(connection)
}
cout <<"Connection To MySQL Server is Success..." <<endl
string str
getline(cin,str)
int res = 0
int affected_count = 0
while (str != "close" &&str != "" &&!res)
{
res = mysql_query(connection,str.c_str())
affected_count += mysql_affected_rows(connection)
if (res)
{
if (mysql_errno(connection))
{
cout <<"Error " <<mysql_errno(connection) <<" : "
<<mysql_error(connection) <<'\n' <<endl
break
}
}
getline(cin,str)
}
cout <<"Have affected " <<affected_count <<" rows!" <<endl
mysql_close(connection)
cout <<"Connection To MySQL Server is closed..." <<endl
return 0
}
void mysql_err_function(MYSQL * connection)
{
if (mysql_errno(connection))
{
cout <<"Error " <<mysql_errno(connection) <<" : "
<<mysql_error(connection) <<endl
exit(-1)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)