When c ++-C ++ connects to the mysql database, the getString () method always fails.

Source: Internet
Author: User
≫www.cnblogs.comjoeblackzqqp4332945.html according to the tutorial above, I used the second ConnectorC ++ method. After configuring the boost path and mysql path, I modified the sample code: & quot; # include & quot; mysql_driver.h & quot; # include & quot; mysql_connection.h & quot; # include mysqlc ++ database

Http://www.cnblogs.com/joeblackzqq/p/4332945.html
According to the tutorial above, I used the second method of Connector C ++. After configuring the boost path and mysql path, I modified the sample code:

# Include
   
    
# Include
    # Include
     
      
# Include
      
       
# Include "mysql_driver.h" # include "mysql_connection.h" # include "cppconn/driver. h "# include" cppconn/statement. h "# include" cppconn/prepared_statement.h "# include" cppconn/metadata. h "# include" cppconn/exception. h "using namespace std; using namespace SQL; int main () {SQL: mysql: MySQL_Driver * driver = 0; SQL: Connection * conn = 0; try {driver = SQL: mysql: get_mysql_driver_instance (); conn = driver-> conn Ect ("tcp: // localhost: 3306/tree", "root", "123"); cout <"connection successful" <endl ;} catch (...) {cout <"connection failed" <endl;} SQL: Statement * stat = conn-> createStatement (); stat-> execute ("set names 'gbk'"); ResultSet * res; res = stat-> executeQuery ("SELECT * FROM testuser "); while (res-> next () {cout <"Id:" <res-> getInt ("id") <endl; cout <"Name: "<res-> getString (" name ") <endl; // cout <" addh Ss: "<res-> getString (" address ") <endl;} if (conn! = 0) {delete conn;} system ("pause ");}
      
     
   

For this reason, I created a database tree and table testuser:

create database tree;use tree;create table testuser(id INT(4) PRIMARY KEY,name CHAR(25),address varchar(45));desc testuser;insert into testuser values(1,'Tom','China Beijing'),(2,'Amy','America NewYork');select*from testuser;

After all the preparations are completed, start to execute:
Question:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.