Jump to content

visual c++ ODBC query

- - - - -

  • Please log in to reply
11 replies to this topic

#1
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hello guys!
I'm trying to create an application wich will connect to a mySQL database and do few updates.I made a windows form application,created a successfull ODBC connection to the mySQL database and here my nightmare starts...

How am I supposed to execute queries and get the results?I lost 3 days searching for it,I have read almost all of the documentations and nothing is comming out... In the most cases I got beautifull error messages regarding the header files like "mysql.h" and others.
I also tried an visual c++ addon called "dbforge fusion" - wich makes the thinks a little better,but I have the same problem there...No way to execute and print the query results...
I think I'm officially f*****

Can someone help me with this?

THANKS in advance! :)

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Did you read ODBC tutorials like this one? If you are using ODBC then you don't need mysql.h
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi Dragon!
I'm fighting with this tutorial since yesterday and the only thing I'm getting is a bunch of errors. :crying:

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
What compiler are you using? The code in that tutorial compiles ok for me using VC++ 2010 express. Just had to make sure my compiler wasn't compiling for UNICODE.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Im using visual studio 2010...No idea whats wrong...

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
My guess (afterall you didn't post any of the error messages) is that your compiler project is set up for UNICODE. Change that by Projects --> Properties (at the bottom of the menu) --> configuration Properties (left lane) --> General (left pane) --> Character Set (right pane) --> then select "Not Set" from the combo box
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi Dragon! :)
I'm getting this message:


>mysqlexample.cpp(2): warning C4627: '#include <stdlib.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(3): warning C4627: '#include <iostream>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(5): warning C4627: '#include <cppconn/driver.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(6): warning C4627: '#include <cppconn/exception.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(7): warning C4627: '#include <cppconn/resultset.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(8): warning C4627: '#include <cppconn/statement.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(9): warning C4627: '#include <cppconn/prepared_statement.h>': skipped when looking for precompiled header use

1>          Add directive to 'StdAfx.h' or rebuild precompiled header

1>mysqlexample.cpp(48): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

1>  Generating Code...

1>

1>Build FAILED.


Don't laught at this - I'm pretty sure its somthing about the "cppconn" - maybe I have to replace it with somthing... No idea at all :crying:
Since I'm using mySql I used this tutorial,but it seems quet impossible for me - maybe because it's my 1st time attempting to make a db connection.
I'm not sure either if I have edited this correctly:


driver = get_driver_instance();

        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");

        con->setSchema("test");


I can send you my remote db info (in case you want to try).

Thanks for your replays and will to help me!

#8
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Oh, I see what's wrong. You need to turn off pre-compiled headers. To do that, select Projects --> Properties --> Configuration Properties --> c++ --> Precompiled Headers --> then select "Not using precompiled headers" in the combo box. You can do this easier when you create the project -- at one point it has a checkbox to turn off precompiled headers.

Attached Files


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#9
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hey Dragon! It worked somehow! Now I'm getting just 1 error:



1>mysqlexample.cpp(4): fatal error C1083: Cannot open include file: 'cppconn/driver.h': No such file or directory


Am I supposed to rename the "driver.h" to somthing else?

Sorry for my stupid questions. It's somthing new to me and actually I haven't cleared it in my mind yet.

Once again,THANKS for helping me!

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
I have no idea where you get that cppconn folder. You need to post some code
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
I'm running this code:


#include <stdlib.h>

#include <iostream>


#include <cppconn/driver.h>

#include <cppconn/exception.h>

#include <cppconn/resultset.h>

#include <cppconn/statement.h>

#include <cppconn/prepared_statement.h>


int main(){


    sql::Driver *driver;

    sql::Connection *con;

    sql::Statement *stmt;

    sql::ResultSet *res;

    sql::PreparedStatement *pstmt;

    

    try{

        driver = get_driver_instance();

        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");

        con->setSchema("test");

        

        stmt = con->createStatement();

        stmt->execute("insert into example values(4,'four'),(5, 'five')");

        delete stmt;


        pstmt = con->prepareStatement("select * from example");

        res = pstmt->executeQuery();

        while (res->next())

            std::cout<<res->getInt("id")<<"  "<<res->getString("data")<<std::endl;

        delete res;

        delete pstmt;


        pstmt = con->prepareStatement("delete from example where id=?");

        pstmt->setInt(1,4);

        pstmt->executeUpdate();

        pstmt->setInt(1,5);

        pstmt->executeUpdate();


        delete pstmt;


        delete con;    

    }catch(sql::SQLException &e){

        std::cout<<e.what();

    }

}


Output results:



1>------ Build started: Project: lastTest, Configuration: Debug Win32 ------

1>Build started 30/1/2012 12:50:59 μμ.

1>InitializeBuildStatus:

1>  Touching "Debug\lastTest.unsuccessfulbuild".

1>GenerateTargetFrameworkMonikerAttribute:

1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.

1>ClCompile:

1>  All outputs are up-to-date.

1>  mysqlexample.cpp

1>mysqlexample.cpp(4): fatal error C1083: Cannot open include file: 'cppconn/driver.h': No such file or directory

1>

1>Build FAILED.

1>

1>Time Elapsed 00:00:02.89

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



#12
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Check you file system to verify that the directory cppconn actually exists. If it does, the you probably just need to tell VC++ where its at. Select menu Project --> Properties --> Configuration Properties --> C++ --> General, then in the right pane edit the Additional Include Directories box. Add the full path to that folder to the end of all the others that may be there.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users