C++ write and read from mysql database - c++

I'm working on a project where we want to write to a mysql database, I've googled around and tried a few implementations but they all fail.
For example I tried this: http://markalexanderbain.suite101.com/using-a-mysql-databases-with-c-a70097
#include "StdAfx.h"
#include <iostream>
#include <my_global.h>
#include <mysql.h>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main()
{mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL)
cout << mysql_error(&mysql) << endl;
return 1;
}
It compiles and generates an exe file, but it closes every time I try to run it, I've added a cin.get at the end so it wouldn't close.
But the program still closes and Visual Studio 2010 gives me the following error message: The program '[32856] mysql test.exe: Native' has exited with code -1073741515 (0xc0000135).
The only thing that is common in all these implementations is the Include files and the #pragma comment(lib, "libmysql") and #pragma comment(lib, "mysqlclient")
I'm using these files which are included with mysql ga 5.5.16 32 bit.
So I'm looking for a way to write to a mysql database, if any one knows how to fix this to make it work or knows another way with a tutorial please let me know.

Error code 0xc0000135 means "The Application Failed To Initialize Properly". This is almost certainly due to a missing DLL (or two); probably the ones relating to MySQL mentioned in your #pragma lib statements.
Either add them to your path or copy them to the same directory as the .exe.
EDIT: Follow the instructions from here, in order to compile with the correct options. The file mysqlclient.dll should be included with the MySQL installation (do a system file find if you cannot find it).

Related

Unable to use libpqxx library with header <pqxx/pqxx> on XCode

I am trying to use libpqxx library.
I have installed the library and it's under the below path on my machine-
/opt/homebrew/Cellar/libpqxx/
I have also updated search paths as below under Build Settings on Xcode -
Header Search Paths - /opt/homebrew/Cellar/libpqxx/**
Library Search Paths - /opt/homebrew/Cellar/libpqxx/**
For more details check below snapshot -
Search Paths
Have updated Linker Flags under build settings as -
Other Linker Flags -> -lpqxx
For more details check below snapshot -
Linker Flags
However when I compile main.cpp file using header -
#include <pqxx/pqxx>
It throws below error, count of errors = 50, reports below error for all namespaces or classes in library libpqxx-
error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
Need advice to overcome this.
Sample Program -
#include <iostream>
#include <pqxx/pqxx>
#include <string>
using namespace std;
int main() {
try {
// Connect to the database
pqxx::connection; c("user=postgres password=xxxx#123 host=localhost port=5432 dbname=postgres target_session_attrs=read-write");
}
catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
}
Explanation -
I see compiler reported almost 50 errors, i believe for each class or namespace under libpqxx library. I certainly can't add every header individually, I need help in understanding how to make the header alone when used like # include<pqxx/pqxx> to compile successfully
Any inputs on this will be highly appreciated.

fstream constructor causing access violation

I'm having trouble reading from a file. I've cut my program down and down until all I'm left with is not much more than Hello World - and I'm still getting an error of 'access violation'.
The error is occuring on the line fstream f(..); - it never gets as far as f.good(). The only changes to this project have been to include ozz-animation header files and .lib's (easily found on Google), as well as setting Runtime Library mode to /MT rather than /MD. Ozz was throwing the same error when attempting to read a file (hence the cutting down until I'm left with what you see). The file is present, although I don't believe it should matter. Code is as follows:
#pragma once
#include <Windows.h>
#include <fstream>
using namespace std;
int main()
{
fstream f("C:\\Users\\thoma_000\\Desktop\\Limitless\\Bin\\Asset\\skin_example\\A.txt");
if (f.good())
{
OutputDebugString(L"\n\n\nGOOD.\n\n\n");
}
else
{
OutputDebugString(L"\n\n\nBAD.\n\n\n");
}
return 0;
}
I managed to resolve this by creating the project initially as an empty win32 project rather than a Windows project. I have been through the project settings and I currently can't tell what setting is causing the problem - but at least I have a resolution.

Linking .lib of MySQL in C/C

So, after whole day of trial and effort, I still couldn't figure answer to this question.
I'm perfectly aware of
this thread and this one
but they don't quite give answer to the question.
So, the problem is when compiling program, let's say, like this one:
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
mysql_init(&mysql);
}
and I get linker error
undefined reference to `mysql_init#4'
I tried adding to input section libmesql.lib and mysqclient.lib, both simultaneously and separately. Tried copying .lib files to Visual Studio default folder, than attempted compiling it by setting path in Linker->General->Additional Library Directories. Tried #pragma comment as well - still to no avail.
So if anyone out there could explain what am I doing wrong (and possibly a way to solve this problem) it would be much appreciated.
p.s. And please, don't answer with links to MySQL documentation - I got them here.
Link your code with mysqlclient.lib.
Properties->Linker->General->Additional Libraries
Add the path to your library here.

Static linking of MySQL in C/C++

I am trying to develop an application that uses MySQL using C++. I downloaded the library from their website and I have attempted to compile the following code:
#include <iostream>
#include <windows.h>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
mysql_init(&mysql);
}
The line that has mysql_init(&mysql); gives me a compilation error
undefined reference to `mysql_init#4'
I am guessing this is due to a library error. I am linking mysqlclient.lib and libmysql.lib in that order. What do I need to do to make this compile without requiring a dll file? Is that possible? Thank you.
Note: I am using mingw32 on Windows 7 x64 to develop an application for Windows.
Maybe this link will help. It states that mysqlclient.lib is the static library and libmysql.lib is the dynamic library, so I don't think you should be linking both.
http://dev.mysql.com/doc/refman/5.0/en/building-clients.html

Berkeley DB(Unable to Locate Component)

I have a problem with berkeley DB. I get a dialog titled "Unable To Locate Componenent" saying "This application has failed to start because libdb48.dll was not found. Re-installing the application may fix this problem", then it crashes after clicking ok. I got the error message when running these simple code below:
#include <iostream>
#include <string>
#include <db_cxx.h>
using namespace std;
int main()
{
Db db(0, 0);
}
I already set the Additional include directories to the "build_windows" directory and I have linked to the "libdb48.lib". I honestly do not know what to do here. The funny part is, I googled and I had 0 pages returned.
I am using visual studio c++ 2008 and Berkeley DB 4.8.24
Thanks
Where is libdb48.dll? Is it installed? Where? A hackish solution that should make it work is to copy libdb48.dll into c:\windows\system32. If that solves the problem, then you know that the DLL just wasn't on the path. Then you can find a more appropriate place to put it.