2014-02-11 78 views
0

我是C++新手(儘管我有一些C語言經驗)和MySQL,我試圖製作一個從MySQL讀取數據庫的程序,我一直在關注這個tutorial,但我得到了一個當我嘗試「構建」解決方案時出錯。 (我使用Visual C++ 2008,就像他們在本教程做的。C++程序與MySQL連接

Compiling... 
test2.cpp 
c:\users\rafael\documents\visual studio 2008\projects\test2\test2\test2.cpp(43) : warning C4244: 'initializing' : conversion from 'my_ulonglong' to 'unsigned int', possible loss of data 
Compiling manifest to resources... 
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0 
Copyright (C) Microsoft Corporation. All rights reserved. 
Linking... 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
test2.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main 
C:\Users\*****\Documents\Visual Studio 2008\Projects\test2\Debug\test2.exe : fatal error LNK1120: 7 unresolved externals 

我跟着教程,我想不通這是怎麼回事,我想這是與連接體,但我不知道我能怎麼辦呢

這是我使用的代碼(source):

#include "stdafx.h" 
#include "my_global.h" // Include this file first to avoid problems 
#include "mysql.h" // MySQL Include File 
#define SERVER "localhost" 
#define USER "root" 
#define PASSWORD "********" 
#define DATABASE "test" 


int main() 
{ 
    MYSQL *connect; // Create a pointer to the MySQL instance 
    connect=mysql_init(NULL); // Initialise the instance 
    /* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/ 
    if(!connect) /* If instance didn't initialize say so and exit with fault.*/ 
    { 
     fprintf(stderr,"MySQL Initialization Failed"); 
     return 1; 
    } 
    /* Now we will actually connect to the specific database.*/ 

    connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0); 
    /* Following if statements are unneeded too, but it's worth it to show on your 
    first app, so that if your database is empty or the query didn't return anything it 
    will at least let you know that the connection to the mysql server was established. */ 

    if(connect){ 
     printf("Connection Succeeded\n"); 
    } 
    else{ 
     printf("Connection Failed!\n"); 
    } 
    MYSQL_RES *res_set; /* Create a pointer to recieve the return value.*/ 
    MYSQL_ROW row; /* Assign variable for rows. */ 
    mysql_query(connect,"SELECT * FROM TABLE"); 
    /* Send a query to the database. */ 
    unsigned int i = 0; /* Create a counter for the rows */ 

    res_set = mysql_store_result(connect); /* Receive the result and store it in res_set */ 

    unsigned int numrows = mysql_num_rows(res_set); /* Create the count to print all rows */ 

    /* This while is to print all rows and not just the first row found, */ 

    while ((row = mysql_fetch_row(res_set)) != NULL){ 
     printf("%s\n",row[i] != NULL ? 
     row[i] : "NULL"); /* Print the row data */ 
    } 
    mysql_close(connect); /* Close and shutdown */ 
    return 0; 
} 

回答

0

是的,這完全是一個連接問題:它幾乎肯定錯過mysql庫。在項目設置中,您應該添加您打算使用的.lib的名稱(和路徑)。