gpt4 book ai didi

连接MySQL的C++程序

转载 作者:行者123 更新时间:2023-11-30 00:41:54 25 4
gpt4 key购买 nike

我是 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;
}

最佳答案

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

关于连接MySQL的C++程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com