當前位置:首頁 » 操作系統 » 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-24 13:40:34 瀏覽:332
安卓怎麼模仿蘋果彈窗 發布:2025-03-24 13:33:47 瀏覽:16
游戲官網源碼 發布:2025-03-24 13:14:04 瀏覽:571
九游原神是什麼伺服器 發布:2025-03-24 13:12:32 瀏覽:269
伺服器可以用自己的電腦做嗎 發布:2025-03-24 13:11:09 瀏覽:2
python取進程pid 發布:2025-03-24 13:09:36 瀏覽:244
高質量c編程 發布:2025-03-24 13:07:33 瀏覽:235
輸送帶緩存 發布:2025-03-24 12:57:16 瀏覽:697
資源配置一般有哪些方式 發布:2025-03-24 12:54:13 瀏覽:261
領勢FTP 發布:2025-03-24 12:48:17 瀏覽:488