用于连接MySQL的C ++程序

时间:2014-02-11 14:53:17

标签: c++ mysql sql database database-connection

我是C ++的新手(虽然我对C有一些经验)和MySQL,我正在尝试创建一个从MySQL读取数据库的程序,我一直在关注这个tutorial但是我在收到错误时遇到了错误我尝试“构建”解决方案。 (我正在使用Visual C ++ 2008,就像他们在教程中一样。

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 _mysql_close@4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_fetch_row@4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_num_rows@4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_store_result@4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_query@8 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_init@4 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;
}

1 个答案:

答案 0 :(得分:0)

是的,它完全是一个链接器问题:它几乎肯定错过了mysql库。在项目设置中,您应该添加要使用的.lib的名称(和路径)。