C ++连接器到MySQL [英] C++ Connector to MySQL
问题描述
已编辑:
我的问题是这篇文章底部的错误。
包含目录
C:\Program Files \boost
C:\Program Files \MySQL \ MySQL连接器C ++ 1.1.3 \include
C:\Program Files\MySQL\MySQL服务器5.6 \include
其他库目录
C:\Program Files \MySQL\MySQL Server 5.6 \lib
C:\Program Files\MySQL\Connector C ++ 1.1.2\lib\opt
其他依赖项
libmysql.lib
mysqlcppconn-static.lib
我的代码
#include< iostream>
#include< cstdio>
#include< cstdlib>
using namespace std;
#include< stdlib.h>
#include< Windows.h>
#include< mysql.h>
#includemysql_connection.h
#include< cppconn / driver.h>
#define hostlocalhost
#define usernameroot
#define passwordroot
#define databasetests
int main ()
{
MYSQL * conn;
conn = mysql_init(NULL);
if(conn)
{
mysql_real_connect(conn,host,username,password,database,0,NULL,0);
}
MYSQL_RES * res_set;
MYSQL_ROW row;
unsigned int i;
mysql_query(conn,SELECT * FROM tbl_clients WHERE id = 1);
res_set = mysql_store_result(conn);
unsigned int numrows = mysql_num_rows(res_set);
if(numrows)
{
row = mysql_fetch_row(res_set);
if(row!= NULL)
{
cout< Client ID:<< row [0]<< endl;
cout<< 客户名称:<< row [1]< endl;
}
}
if(res_set)
{
mysql_free_result(res_set);
}
if(conn)
{
mysql_close(conn);
}
return 0;
}
这是我得到的错误
1> ------ Build started:Project:okay,Configuration:Debug Win32 ------
1> welp.obj:error LNK2019:未解析的外部符号_mysql_num_rows @ 4在函数_main
中引用1> welp.obj:error LNK2019:未解析的外部符号_mysql_init @ 4在函数_main
中引用1> welp.obj:error LNK2019:unresolved external符号_mysql_real_connect @ 32函数_main
中引用1> welp.obj:error LNK2019:未解析的外部符号_mysql_query @ 8在函数_main
中引用1> welp.obj:error LNK2019:未解析的外部符号_mysql_store_result @ 4引用的函数_main
1> welp.obj:error LNK2019:未解析的外部符号_mysql_free_result @ 4在函数_main中引用
1> welp.obj:error LNK2019:未解析的外部符号_mysql_fetch_row @ 4在函数_main
1> welp.obj:error LNK2019:未解析的外部符号_mysql_close @ 4在函数_main
中引用1> C:\Users\Damian\documents\visual studio 2012\Projects\ okay \Debug \okay.exe:致命错误LNK1120:8未解析的外部
========== Build:0成功,1失败,0最新,0跳过== ========
请帮助,此项目将在48小时内到期
感谢
解决方案,最后两个问题是编译器/链接器问题。我理解你的沮丧,因为我知道如何早期编码,但不知道编译器/链接器。当您花一些时间阅读:
- 标题
- 函数管理(C vs C ++)
为了回答你的问题,我想你在Microsoft Visual Studio中这样做: p>
- 您需要转到项目属性并设置其他库路径(我看到您是从上一篇文章中创建的)
- 你需要指定库,我在这里出去,假设它将
mysql.lib
...有一个附加库输入在链接器部分的某个地方,你将能够识别它,因为kernel32.lib
将是int。将mysql.lib
添加到该部分。通过转到保存二进制文件的mysql文件夹来确认名称。 - 确保您使用的是静态库,
.dll
会给你新的问题,你不需要担心。这通常通过设置正确的库目录来实现。许多c ++库将提供包含正确库的预编译的静态和动态文件夹。
EDITED:
My Problem is the errors at the bottom of this post.
Heres my additional include directories
C:\Program Files\boost
C:\Program Files\MySQL\MySQL Connector C++ 1.1.3\include
C:\Program Files\MySQL\MySQL Server 5.6\include
Additional Library Directories
C:\Program Files\MySQL\MySQL Server 5.6\lib
C:\Program Files\MySQL\Connector C++ 1.1.2\lib\opt
Additional Dependencies
libmysql.lib
mysqlcppconn-static.lib
Heres my code
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#include <stdlib.h>
#include <Windows.h>
#include <mysql.h>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#define host "localhost"
#define username "root"
#define password "root"
#define database "tests"
int main()
{
MYSQL* conn;
conn = mysql_init( NULL );
if( conn )
{
mysql_real_connect( conn, host, username, password, database, 0, NULL, 0 );
}
MYSQL_RES* res_set;
MYSQL_ROW row;
unsigned int i;
mysql_query( conn, "SELECT * FROM tbl_clients WHERE id = 1" );
res_set = mysql_store_result( conn );
unsigned int numrows = mysql_num_rows( res_set );
if( numrows )
{
row = mysql_fetch_row( res_set );
if( row != NULL )
{
cout << "Client ID : " << row[0] << endl;
cout << "Client Name: " << row[1] << endl;
}
}
if( res_set )
{
mysql_free_result( res_set );
}
if( conn )
{
mysql_close( conn );
}
return 0;
}
These are the errors I get
1>------ Build started: Project: okay, Configuration: Debug Win32 ------
1>welp.obj : error LNK2019: unresolved external symbol _mysql_num_rows@4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_query@8 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_store_result@4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_free_result@4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_fetch_row@4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_close@4 referenced in function _main
1>C:\Users\Damian\documents\visual studio 2012\Projects\okay\Debug\okay.exe : fatal error LNK1120: 8 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help, This project is due in about 48 hours for my class, and I've spent so much time trying to figure this out.
Thanks
respectfully, your last two questions are compiler/linker questions. I understand your frustration, as I knew how to code early but knew nothing about compilers/linkers. When you get a moment take some time to read about:
- headers
- binary vs object file
- libraries / shared object
- compiler
- linker
- function mangling (C vs C++)
To answer your question, I am guessing you're doing this in Microsoft Visual Studio:
- You need to go to Project Properties and set Additional Library Paths (i see you did that from your last post)
- You need to specify the library, I'm going out on a limb here and assuming it will
mysql.lib
... There's an "Additional Library" input somewhere in the Linker section, you'll be able to identify it becausekernel32.lib
will be int it. Add themysql.lib
to that section. Confirm the name by going to the mysql folder which holds the binaries. - Make sure you're using the static library, the
.dll
will give you new issues that you don't need to worry about. This is usually achieved by setting the correct library directory. Many c++ libraries will ship with precompiled "Static" and "Dynamic" folders containing the correct libraries.
这篇关于C ++连接器到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!