jjzjj

c++ - MySql C++连接器getString()不能正常工作,而getInt可以正常工作

coder 2023-10-14 原文

我试图将C++连接到MySql,但无法正常工作。我从Oracle网站使用Windows的最新MySql和C++/Connector。我也使用VS2010。

它可以编译,并且除了getString之外,其他所有方法都可以正常工作!我使用了他们提供的一些调整示例:

#include "stdafx.h"


#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <driver/mysql_public_iface.h>

#include "examples.h"

using namespace std;

int main(int argc, const char **argv)
{
string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

/* sql::ResultSet.rowsCount() returns size_t */
size_t row;
stringstream sql;
stringstream msg;
int i, affected_rows;

cout << boolalpha;
cout << "1..1" << endl;
cout << "# Connector/C++ connect basic usage example.." << endl;
cout << "#" << endl;

try {
    sql::Driver * driver = sql::mysql::get_driver_instance();
    /* Using the Driver to create a connection */
    std::auto_ptr< sql::Connection > con(driver->connect("localhost", "root", "root"));

    /* Creating a "simple" statement - "simple" = not a prepared statement */
    std::auto_ptr< sql::Statement > stmt(con->createStatement());

    /* Create a test table demonstrating the use of sql::Statement.execute() */
    stmt->execute("USE publications");
    cout << "#\t Test table created" << endl;

    /* Populate the test table with data */

    cout << "#\t Test table populated" << endl;

    {
        /*
        Run a query which returns exactly one result set like SELECT
        Stored procedures (CALL) may return more than one result set
        */
        std::auto_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT id, task FROM to_do_list ORDER BY id ASC"));
        cout << "#\t Running 'SELECT id, task FROM to_do_list ORDER BY id ASC'" << endl;

        /* Number of rows in the result set */
        cout << "#\t\t Number of rows\t";
        cout << "res->rowsCount() = " << res->rowsCount() << endl;
        if (res->rowsCount() != EXAMPLE_NUM_TEST_ROWS) {
            msg.str("");
            msg << "Expecting " << EXAMPLE_NUM_TEST_ROWS << "rows, found " << res->rowsCount();
            throw runtime_error(msg.str());
        }

        /* Fetching data */
        row = 0;
        while (res->next()) {
            cout << "#\t\t Fetching row " << row << "\t";
            /* You can use either numeric offsets... */
            cout << "id = " << res->getInt("id");
            /* ... or column names for accessing results. The latter is recommended. */
            cout << ", task = '" << res->getString("task") << "'" << endl;
            row++;
            system("PAUSE");
        }
    }

...//其余部分无关紧要,因为它在这里中断。

这是输出:
1..1
# Connector/C++ connect basic usage example..
#
#        Test table created
#        Test table populated
#        Running 'SELECT id, task FROM to_do_list ORDER BY id ASC'
#                Number of rows res->rowsCount() = 4
#                Fetching row 0 id = 3, task = ''
Press any key to continue . . .
#                Fetching row 1 id = 7, task = ''
Press any key to continue . . .
   ☼   Ñ╤½x R=  I>             ☼   £I> task  ↕ 4å@ xQ@ ♦   ☼   ☻    ocalhost ½xÇ
'
Press any key to continue . . .

这就是表格的样子,很明显它无法正常工作。我只是无法从MySql中获取varchar(120)的字符串:
mysql> use publications
Database changed
mysql> select id, task from to_do_list;
+----+---------------------------+
| id | task                      |
+----+---------------------------+
|  9 | prepare for calculus      |
|  3 | buy jeans                 |
|  8 | buy new scale             |
|  7 | buy Ethics book for class |
+----+---------------------------+
4 rows in set (0.00 sec)

我尝试了VS2008,这就是我得到的:

msvcp90d.dll!104e8dcb()
[Frames below may be incorrect and/or missing, no symbols loaded for msvcp90d.dll]
try_mysql_native.exe!std::operator<<(std::basic_ostream > & os={...}, const sql::SQLString & str={...}) Line 196 + 0x1f bytes C++ try_mysql_native.exe!main(int argc=1, const char * * argv=0x003e63e8) Line 115 + 0xa5 bytes C++ try_mysql_native.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C try_mysql_native.exe!mainCRTStartup() Line 403 C kernel32.dll!7c817077()



还有这个:

'try_mysql_native.exe':已加载'C:\Documents and Settings\Eugene\My Documents\Visual Studio 2008\Projects\try_mysql_native\Debug\try_mysql_native.exe',已加载符号。
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\ntdll.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\kernel32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\mysqlcppconn.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\libmysql.dll',已加载符号。
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\advapi32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\rpcrt4.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\secur32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\ws2_32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\msvcrt.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\ws2help.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcp90.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.4974_x-ww_d889290f\msvcr90.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\mswsock.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\dnsapi.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\winrnr.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\wldap32.dll'
'try_mysql_native.exe':已加载'C:\Program Files\Bonjour\mdnsNSP.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\iphlpapi.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\user32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\gdi32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\imm32.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\rasadhlp.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\hnetcfg.dll'
'try_mysql_native.exe':已加载'C:\WINDOWS\system32\wshtcpip.dll'
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的首次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处的第一次机会异常:0xC0000005:访问冲突读取位置0x6e61656a。
try_mysql_native.exe中0x104e8dcb处未处理的异常:0xC0000005:访问冲突读取位置0x6e61656a。

最佳答案

您确定任务栏没有设置二进制排序规则吗?如果是这样,连接器可能会为同一列返回不同的元数据。

更新:

另外,请检查连接器是否已使用与项目相同的运行时进行编译。如果使用VS,我敢打赌另一个是用/MT编译的,另一个是用/MD编译的。这样,他们使用不同的堆,将无法正常工作。

关于c++ - MySql C++连接器getString()不能正常工作,而getInt可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4822958/

有关c++ - MySql C++连接器getString()不能正常工作,而getInt可以正常工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  6. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  9. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

  10. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

随机推荐