942,956 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 832
  • C++ RSS
Aug 30th, 2010
0

using mysql and cUrl in C++ program a simple example please

Expand Post »
hello all

i'm using cUrl and mysql in PHP but new to c++

for connecting to remote mysql database and run query what i need?
should i install mysql connector? where are mysql libraries?

may i download a large file for example 400 mb in c++ using cUrl ?

i found these example codes :
C++ Syntax (Toggle Plain Text)
  1. /*****************************************************************************
  2.  * _ _ ____ _
  3.  * Project ___| | | | _ \| |
  4.  * / __| | | | |_) | |
  5.  * | (__| |_| | _ <| |___
  6.  * \___|\___/|_| \_\_____|
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11.  
  12. #include <curl/curl.h>
  13. #include <curl/types.h>
  14. #include <curl/easy.h>
  15.  
  16. /*
  17.  * This is an example showing how to get a single file from an FTP server.
  18.  * It delays the actual destination file creation until the first write
  19.  * callback so that it won't create an empty file in case the remote file
  20.  * doesn't exist or something else fails.
  21.  */
  22.  
  23. struct FtpFile {
  24. const char *filename;
  25. FILE *stream;
  26. };
  27.  
  28. static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
  29. {
  30. struct FtpFile *out=(struct FtpFile *)stream;
  31. if(out && !out->stream) {
  32. /* open file for writing */
  33. out->stream=fopen(out->filename, "wb");
  34. if(!out->stream)
  35. return -1; /* failure, can't open file to write */
  36. }
  37. return fwrite(buffer, size, nmemb, out->stream);
  38. }
  39.  
  40.  
  41. int main(void)
  42. {
  43. CURL *curl;
  44. CURLcode res;
  45. struct FtpFile ftpfile={
  46. "curl.tar.gz", /* name to store the file as if succesful */
  47. NULL
  48. };
  49.  
  50. curl_global_init(CURL_GLOBAL_DEFAULT);
  51.  
  52. curl = curl_easy_init();
  53. if(curl) {
  54. /*
  55.   * Get curl 7.9.2 from sunet.se's FTP site. curl 7.9.2 is most likely not
  56.   * present there by the time you read this, so you'd better replace the
  57.   * URL with one that works!
  58.   */
  59. curl_easy_setopt(curl, CURLOPT_URL,
  60. "ftp://ftp.sunet.se/pub/www/utilities/curl/curl-7.9.2.tar.gz");
  61. /* Define our callback to get called when there's data to be written */
  62. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
  63. /* Set a pointer to our struct to pass to the callback */
  64. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
  65.  
  66. /* Switch on full protocol/debug output */
  67. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  68.  
  69. res = curl_easy_perform(curl);
  70.  
  71. /* always cleanup */
  72. curl_easy_cleanup(curl);
  73.  
  74. if(CURLE_OK != res) {
  75. /* we failed */
  76. fprintf(stderr, "curl told us %d\n", res);
  77. }
  78. }
  79.  
  80. if(ftpfile.stream)
  81. fclose(ftpfile.stream); /* close the local file */
  82.  
  83. curl_global_cleanup();
  84.  
  85. return 0;
  86. }
C++ Syntax (Toggle Plain Text)
  1. #include <mysql++.h>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5. using namespace mysqlpp;
  6.  
  7. int main() {
  8. try {
  9. Connection conn(false);
  10. conn.connect("DB NAME", "DB HOST probably localhost", "DB USER", "DB PASS");
  11. Query query = conn.query();
  12. } catch (BadQuery er) { // handle any connection or
  13. // query errors that may come up
  14. cerr << "Error: " << er.what() << endl;
  15. return -1;
  16. } catch (const BadConversion& er) {
  17. // Handle bad conversions
  18. cerr << "Conversion error: " << er.what() << endl <<
  19. "\tretrieved data size: " << er.retrieved <<
  20. ", actual size: " << er.actual_size << endl;
  21. return -1;
  22. } catch (const Exception& er) {
  23. // Catch-all for any other MySQL++ exceptions
  24. cerr << "Error: " << er.what() << endl;
  25. return -1;
  26. }
  27.  
  28. return (EXIT_SUCCESS);
  29. }

in curl example , how to use it? which function to declare? where to enter url?
how to get download progress and show it?

please help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ataomega is offline Offline
3 posts
since Aug 2010
Aug 30th, 2010
0
Re: using mysql and cUrl in C++ program a simple example please
I don't know anything about CURL, but you can use SQL through VTK:
http://vtk.org/Wiki/VTK/Examples/Cxx#Databases
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 30th, 2010
0
Re: using mysql and cUrl in C++ program a simple example please
thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ataomega is offline Offline
3 posts
since Aug 2010
Aug 31st, 2010
0
Re: using mysql and cUrl in C++ program a simple example please
when retrieving a file with cUrl , how to show progress?

thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ataomega is offline Offline
3 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: writing into binary file
Next Thread in C++ Forum Timeline: Odd screen capture on window update





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC