ورود

View Full Version : استفاده از mysql در win32 console application



m_basij
سه شنبه 08 شهریور 1390, 22:39 عصر
سری به سایت mysql زدم یک اموزش چندین صفحه ای گذاشته بود و یک سرس برنامه اتصال به دیتابیس مای اسکیو ال. من باینری وبندوزش رو دانلود کردم و روی ویژوال استادیو تنظیمش کردم ولی وقتی برنامه را کامپایل می کنی چند خطا میده که نمی دونم چرا اتفاق می افته. دوستانی که اطلاع دارن از علت رخ دادن این خطا یا کلاسی رو سراغ دارن که بشه ازش برای اتصال به دیتابیس استفاده کرد ممنون میشم راهنمایی کنند.( این کدی که کامپایل می کنم.
// mo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}


و این هم لاگ خروجی
1>------ Build started: Project: mo, Configuration: Debug Win32 ------
1>Linking...
1>mo.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: char const * __thiscall sql::SQLException::getSQLState(void)const " (__imp_?getSQLState@SQLException@sql@@QBEPBDXZ) referenced in function __catch$_main$0
1>mo.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QBEHXZ) referenced in function __catch$_main$0
1>mo.obj : error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main
1>D:\mo\Debug\mo.exe : fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://d:\mo\mo\Debug\BuildLog.htm"
1>mo - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

kamyarfares
چهارشنبه 09 شهریور 1390, 00:31 صبح
دوست عزیز منم از این سایت خیلی مطلب گرفتم ولی هیچ کدومشون درس کار نمیکنن. هرچقدرم ویرایشش میکنی ایراداش خیلی زیادن.
من خودم دنبال کد اتصال ++visual c با SQL Server هستم هرچه میگردم یه کد درست حسابی پیدا نمیکنم. حتی به سایت خود مایکروسافت هم رفتم ولی قاطی پاتی نوشته مشخص نیست چی نوشته. ازت ممنون میشم کدشو برام پیدا کنی

m_basij
یک شنبه 13 شهریور 1390, 13:31 عصر
کسی نبود جوابی بده؟

r00tkit
یک شنبه 13 شهریور 1390, 13:39 عصر
.lib ها رو درست تنظیم کردی ؟

m_basij
یک شنبه 13 شهریور 1390, 23:33 عصر
والا وقتی پکیج کانکتوره را دانلود کردم دو فایل داشت یکی lib و یکی هم include که در تنظیمات ویژوال استادیو داخل include files , library files مسیر این دو پوشه را اضافه کردم. البته باید بگم که این دو پوشه خودشون شامل دو پوشه هستند که من کلی انتخابشون کردم فکر نکنم مشکلی داشته باشه اخه فرقی نباید داشته باشه که دونه دونه پوشه ها رو بگیری یا پوشه اصلیه رو انتخاب کنی. به هر حال محتویات این پکیج در لیست زیر معلوم است.

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
04/18/2009 12:10 AM 19,451 COPYING
08/29/2011 03:14 PM <DIR> include
08/29/2011 03:14 PM <DIR> lib
04/18/2009 12:10 AM 9,321 README
2 File(s) 28,772 bytes
4 Dir(s) 33,501,761,536 bytes free

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5>cd include

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
08/29/2011 03:14 PM <DIR> cppconn
04/18/2009 12:10 AM 2,994 mysql_connection.h
04/18/2009 12:10 AM 1,583 mysql_driver.h
2 File(s) 4,577 bytes
3 Dir(s) 33,501,761,536 bytes free

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include>cd cppconn

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
04/18/2009 12:10 AM 1,103 build_config.h
04/18/2009 12:15 AM 2,185 config.h
04/18/2009 12:10 AM 2,967 connection.h
04/18/2009 12:10 AM 898 datatype.h
04/18/2009 12:10 AM 1,251 driver.h
04/18/2009 12:10 AM 3,890 exception.h
04/18/2009 12:10 AM 12,202 metadata.h
04/18/2009 12:10 AM 938 parameter_metadata.h
04/18/2009 12:10 AM 2,187 prepared_statement.h
04/18/2009 12:10 AM 3,114 resultset.h
04/18/2009 12:10 AM 2,020 resultset_metadata.h
04/18/2009 12:10 AM 1,350 statement.h
04/18/2009 12:10 AM 1,965 warning.h
13 File(s) 36,070 bytes
2 Dir(s) 33,501,761,536 bytes free

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn>cd ...

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn>cd ..

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include>cd ..

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5>cd lib

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
08/29/2011 03:14 PM <DIR> debug
08/29/2011 03:14 PM <DIR> opt
0 File(s) 0 bytes
4 Dir(s) 33,501,761,536 bytes free

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib>cd debug

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\debug>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\debug

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
04/18/2009 12:19 AM 6,623,772 mysqlcppconn-static.lib
04/18/2009 12:19 AM 815,104 mysqlcppconn-static.pdb
04/18/2009 12:19 AM 730,624 mysqlcppconn.dll
04/18/2009 12:19 AM 41,986 mysqlcppconn.lib
04/18/2009 12:19 AM 3,977,216 mysqlcppconn.pdb
5 File(s) 12,188,702 bytes
2 Dir(s) 33,501,761,536 bytes free

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\debug>cd ..

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib>cd opt

D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt>dir
Volume in drive D is software
Volume Serial Number is 00DD-6831

Directory of D:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt

08/29/2011 03:14 PM <DIR> .
08/29/2011 03:14 PM <DIR> ..
07/29/2008 09:05 AM 572,928 MSVCP90.dll
07/29/2008 09:05 AM 655,872 MSVCR90.dll
04/18/2009 12:16 AM 5,643,108 mysqlcppconn-static.lib
04/18/2009 12:16 AM 446,976 mysqlcppconn.dll
04/18/2009 12:16 AM 41,986 mysqlcppconn.lib
5 File(s) 7,360,870 bytes
2 Dir(s) 33,501,761,536 bytes free

حالا اگه اشتباه لینک کردم به اینها بگید.(در ضمن باید بگم از قسمت tools->option->vc++ direction این کارو انجام دادم)

m_basij
سه شنبه 15 شهریور 1390, 19:08 عصر
کسی نیست جواب مارو بده؟!