当前位置:首页 » 操作系统 » vc与mysql数据库

vc与mysql数据库

发布时间: 2023-06-15 14:08:12

① C++怎么连接Mysql数据库

//
S:~在官方给的例子基础上进行修改后的例子
#include

#include

#include

#include

#include
"mysql_connection.h"
#include

#include

#include

#include

#include

using
namespace
std;
int
main(void)
try
{
sql::Driver
*driver;
sql::Connection
*con;
sql::Statement
*stmt;
sql::ResultSet
*res;
driver
=
0;
/*
Create
a
connection
*/
driver
=
get_driver_instance();
//
获取连接驱动
con
=
driver->connect("tcp://localhost:3306",
"root",
"********");
//
ip,用户,密码
/*
Connect
to
the
MySQL
test
database
*/
con->setSchema("test");
//
选择数据库
stmt
=
con->createStatement();
//
创建查询语句
res
=
stmt->executeQuery("select
*
from
a");
//
执行查询
while
(res->next())
{
cout
<<
"\t...
MySQL
replies:
";
/*
Access
column
data
by
alias
or
column
name
*/
cout
<<
res->getString(1)
<<
'\t'
<<
res->getString(2)
<<
endl;
//
输出1,2列
}
delete
res;
delete
stmt;
delete
con;
return
EXIT_SUCCESS;
}
catch
(sql::SQLException
&e)
{
cout
<<
"#
ERR:
SQLException
in
"
<<
__FILE__;
cout
<<
"("
<<
__FUNCTION__
<<
")
on
line
"
<<
__LINE__
<<
endl;
cout
<<
"#
ERR:
"
<<
e.what();
cout
<<
"
(MySQL
error
code:
"
<<
e.getErrorCode();
cout
<<
",
SQLState:
"
<<
e.getSQLState()
<<
"
)"
<<
endl;
}
运行截图:
mysql
connector/c++
文档(包括例子):
http://dev.mysql.com/doc/refman/5.1/en/connector-cpp.html
mysql
connector/c++
api下载:
http://dev.mysql.com/downloads/connector/cpp/
下载以后lib和include文件夹直接到你vc的lib和include里
然后工程库引用里添加对mysqlcppconn.lib的引用
可能要将mysqlcppconn.dll放到c:\windows文件夹下
具体可以参考这里:
http://hi..com/buptyoyo/blog/item/11ddf212cdaef40b213f2e3d.html
另外注意目前有个已知的因debug和release模式下string长度不同而引起的bug:
http://bugs.mysql.com/bug.php?id=44272
所以代码要在release下编译运行,driver->connect才能连接成功

② 在VC中创建Mysql数据库

Driver={SQL Server};Server=主机名;Database=数据库名;Uid=sa;Pwd=sa;

还有一个比较简单方法确定连接字符串:
1.建立一个.udl的文件。
2.双击打开,将里面相关项进行设置,保存,关闭。
3.有记事本打开这个udl文件,里面的字符串就是连接字符串!

c语言怎么连接mysql数据库 代码

//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平台上的编译
#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、用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);
}
}

⑤ c++代码 连接mysql数据库 怎么连接啊

您好,代码如下,希望能帮到您。还有,如果觉得俺答案还可以的话,请记得采纳答案。。

//下面的代码是一个实现C++连接MYSQL数据库的很好的例子
//这里用了建表,插入,检索,删表等常用功能
//我用VC++6.0生成,已经成功连接了。
//在VC++6.0中要想把做一下两步准备工作才可以。
//其实就是将头文件和库文件包含进来
#include <winsock.h>
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")
//单步执行,不想单步执行就注释掉
#define STEPBYSTEP
int main() {
cout << "****************************************" << endl;
#ifdef STEPBYSTEP

system("pause");
#endif

//必备的一个数据结

MYSQL mydata;
//初始化数据库

if (0 == mysql_library_init(0, NULL, NULL)) {
cout << "mysql_library_init() succeed" << endl;
} else {

cout << "mysql_library_init() failed" << endl;

return -1;

}

#ifdef STEPBYSTEP
system("pause");
#endif
//初始化数据结构
if (NULL != mysql_init(&mydata)) {
cout << "mysql_init() succeed" << endl;
} else {
cout << "mysql_init() failed" << endl;
return -1;
}

#ifdef STEPBYSTEP
system("pause");

#endif
//在连接数据库之前,设置额外的连接选项

//可以设置的选项很多,这里设置字符集,否则无法处理中文

if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {

cout << "mysql_options() succeed" << endl;
} else {

cout << "mysql_options() failed" << endl;

return -1;

}
#ifdef STEPBYSTEP

system("pause");

#endif

//连接数据
if (NULL != mysql_real_connect(&mydata, "localhost", "root", "test", "test", 3306, NULL, 0))

//这里的地址,用户名,密码,端口可以根据自己本地的情况更改

{

cout << "mysql_real_connect() succeed" << endl;
} else {
cout << "mysql_real_connect() failed" << endl;

return -1;

}

#ifdef STEPBYSTEP

system("pause");
#endif

//sql字符串

string sqlstr;

//创建一个表
sqlstr = "CREATE TABLE IF NOT EXISTS user_info";

sqlstr += "(";
sqlstr += "user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
sqlstr += "user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";

sqlstr += "user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";

sqlstr += ");";

if (0 == mysql_query(&mydata, sqlstr.c_str())) {

cout << "mysql_query() create table succeed" << endl;

} else {

cout << "mysql_query() create table failed" << endl;

mysql_close(&mydata);

return -1;

}

#ifdef STEPBYSTEP

system("pause");

#endif

//向表中插入数据
sqlstr = "INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('姓名');";

if (0 == mysql_query(&mydata, sqlstr.c_str())) {

cout << "mysql_query() insert data succeed" << endl;

} else {

cout << "mysql_query() insert data failed" << endl;

mysql_close(&mydata);

return -1;
}
#ifdef STEPBYSTEP

system("pause");
#endif

//显示刚才插入的数据

sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info";

MYSQL_RES *result = NULL;
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() select data succeed" << endl;

//一次性取得数据集

result = mysql_store_result(&mydata);

//取得并打印行数

int rowcount = mysql_num_rows(result);

cout << "row count: " << rowcount << endl;
//取得并打印各字段的名称

unsigned int fieldcount = mysql_num_fields(result);
MYSQL_FIELD *field = NULL;

for (unsigned int i = 0; i < fieldcount; i++) {
field = mysql_fetch_field_direct(result, i);
cout << field->name << "\t\t";

}

cout << endl;
//打印各行
MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
while (NULL != row) {
for (int i = 0; i < fieldcount; i++) {
cout << row[i] << "\t\t";
}
cout << endl;
row = mysql_fetch_row(result);
}
} else {
cout << "mysql_query() select data failed" << endl;

mysql_close(&mydata);
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//删除刚才建的表
sqlstr = "DROP TABLE user_info";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() drop table succeed" << endl;
} else {
cout << "mysql_query() drop table failed" << endl;
mysql_close(&mydata);
return -1;
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_server_end();
system("pause");

return 0;
}

热点内容
配置虚拟局域网是什么 发布:2025-03-26 09:28:20 浏览:201
在WIN10使用linux 发布:2025-03-26 09:27:55 浏览:37
朗逸为什么都是安卓大屏 发布:2025-03-26 09:24:03 浏览:809
编程技术入侵 发布:2025-03-26 09:06:43 浏览:400
编译原理自下而上 发布:2025-03-26 08:49:48 浏览:263
win10删除文件拒绝访问 发布:2025-03-26 08:43:58 浏览:599
exe加密的pdf文件破解 发布:2025-03-26 08:43:56 浏览:665
苹果密码存储点不开 发布:2025-03-26 08:43:06 浏览:247
埃安y70哪个配置好 发布:2025-03-26 08:33:44 浏览:970
iterm怎么连接服务器 发布:2025-03-26 08:28:45 浏览:258