[問題] C++連MySQL的問題
最近想學學利用C++連MySQL,並將查詢結果顯示出來,
所以到網路上查了相關的程式碼,也將MySQL的資料庫建好,加了幾筆測試資料進去,
然後稍微改了部份內容後,似乎仍然沒有辦法執行,
希望版上的大大們可以幫忙解惑,感激不盡!
以下為程式碼:(有附上網頁上的原始註解)
#include <winsock2.h>
#include <mysql.h>
using namespace std;
#pragma comment(lib,"libmysql.lib")
#pragma comment(lib,"wsock32.lib")
#include"mysql.h"
void main()
{
MYSQL *connection;
MYSQL_RES *resource;
MYSQL_ROW result;
char *servername = "localhost";
char *user = "root";
char *password = "water";
char *database = "test";
char *socket = NULL;
/* Intitialize connection to database, and MYSQL structure. */
connection = mysql_init(NULL);
/* Connect to database */
if(!mysql_real_connect(connection, servername, user,
password, database, 0, socket, 0))
{
printf("%s\n", mysql_error(connection));
}
/* SELECT more data from database."watertest"是我的測試table */
mysql_query(connection, "SELECT * FROM watertest order
by no");
/* Resource struct with rows of returned data. */
resource = mysql_use_result(connection);
while((result = mysql_fetch_row(resource)))
{
printf("%s %s\n",result[0], result[1]);
}
/* Free memory used by resource */
mysql_free_result(resource);
/* Closes connection to database, frees memory used by
connection. */
mysql_close(connection);
/* Frees up other memory used by the libmysqlclient. */
mysql_library_end();
}
出現的錯誤訊息為:
錯誤 1 error C2871: 'std' : 此名稱的命名空間不存在
d:\cplusplus\mysql.cpp 4
錯誤 2 error C3861: 'printf': 找不到識別項
d:\cplusplus\mysql.cpp 26
錯誤 3 error C3861: 'printf': 找不到識別項
d:\cplusplus\mysql.cpp 36
希望有大大們能幫忙解惑,謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.193.10.42
推
08/24 16:20, , 1F
08/24 16:20, 1F
推
08/24 17:51, , 2F
08/24 17:51, 2F
→
08/24 18:04, , 3F
08/24 18:04, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章