| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1017 人关注过本帖
标题:[C++] 读取mysql数据库的问题,请各位大侠帮看看...
取消只看楼主 加入收藏
qk00001
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-10-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
[C++] 读取mysql数据库的问题,请各位大侠帮看看...
我是C++新手,正做一个中英文翻译的小程序,但是总是有错误,还请各位高手帮看看,下面有错误提示。
谢谢了!!!

---------------------------------------------------------------------------------------------
1>------ Build started: Project: temp6, Configuration: Release Win32 ------
1>Compiling...
1>temp6.cpp
1>.\temp6.cpp(48) : error C2146: syntax error : missing ')' before identifier 'str1'
1>.\temp6.cpp(48) : error C2059: syntax error : ')'
1>Build log was saved at "file://c:\Documents and Settings\Administrator\桌面\My_Project\temp6\temp6\Release\BuildLog.htm"
1>temp6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


#include <stdafx.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

        
using namespace std;
        
int main(void)

{
        string (str1);
try
{
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;


  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("localhost", "root", "qwerty");
  /* Connect to the MySQL menagerie database */
  con->setSchema("menagerie");

        stmt = con->createStatement();
        stmt->execute("set names \'GBK\'");
        
        
        cout << "Please enter an english word: ";
        cin >> str1;

        res=stmt->executeQuery("select * from test2 where english="str1"");----------提示错误就是说这句!但是我不知道是怎么错了?
        while (res->next())
{
    cout << res<<endl;

        cout <<setiosflags(ios::left)<<setw(9)<<res->getString(1)<<"     ";//1 is english
        cout <<setiosflags(ios::left)<<setw(9)<<res->getString(2);//2 is chinese
   
  }
  delete res;
  delete stmt;
  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;
}
搜索更多相关主题的帖子: mysql 数据库 
2009-10-30 20:02
qk00001
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-10-30
收藏
得分:0 
以下是我试过的方法:
res=stmt->executeQuery("select * from test2 where english=\'%s\'", str1);
executeQuery("select * from test2 where english="str1"");
res=stmt->executeQuery("select * from test2 where english=\'str1\'");
我也有试过用单引号‘’ 和括号(),但是都不行呀。
2009-10-30 20:04
qk00001
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-10-30
收藏
得分:0 
Please enter an english word: one//这是我输入的‘one’

# ERR: SQLException in .\temp6.cpp(main) on line 69
# ERR: Unknown column 'one' in 'where clause' (MySQL error code: 1054, SQLState:
 42S22 )

我刚刚试过,运行没有提示错误,但是这是mysql提示的错误。
谢谢版主的回帖。
2009-10-30 22:46
快速回复:[C++] 读取mysql数据库的问题,请各位大侠帮看看...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017322 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved