Connect to database: Client side or server side

Hello everyone,

I’m trying to connect my game to database not sure which one but that really doesn’t matter now. My question is: Which is better way to connect to database? With C++ code directly or send POST requests to web server. I searched few days to connect to database with c++ code but I had no success. And now I’m thinking of making C# ASP.NET web server and send variables from the game to that server and from C# code save it in database. But I’m afraid that it will be quite slow… Any better ideas?

you NEVER want to connect to a database directly client side it leaves a lot of room for breaches, better to use a webserver for requests, also it you can keep it server side i would go that route anything client side has a higher risk

best bet is to probably have it server side and have the server send the info directly to the database (please note im still learning this myself so best to research it)

Defenetly keep connection server side and have client recuest data from server.
Its worth to note that UE 4 has SQLite support so you can get alot done with that.

But if it is something external then here are some tutorials/ threads on the forum.
And you also may want to checkout the docs on Database Support.

And to save you some time here is the API on Sockets :wink:

Hope it helps.

Never trust the client.

Here’s how I would design the database interaction:

Client <—> Server <—> Database

Assuming you’re using a SQL database, where do the queries get created and run? On the server. A client shouldn’t even know that a backend database exists. It just interacts with the server and sends requests to the server and the server sends back responses.

Example query:
Client to server: What is the current high score leaderboard?
Server to client: Hang on, let me give you a list of names and scores!
Server to database: Run this SQL query and give me the results.
Server: Lets translate the database response into something the client can understand…
Server to client: Here’s a list of names and associated scores.
Client: Thanks! Let me sort these and display them to the user.

If you hide your database server behind a DMZ, it will be untouchable by the outside world. No SQL injection attacks from clients :slight_smile:

As far as doing the server to database connection, I’m sure there are plenty of online resources for how to do that in C++. The general gist is that you create a connection string on the server, open up a database connection with that connection string, connect to a database, and start running queries. Since its a server, the connection will probably be pretty persistent so you can set a really high timeout value. Once the server is done (ie, rebooting or shutting down), you close the database connection.

Okay guys I get everything you say. But I tried using MySQL++/MySQL C++ Connector/QSqlDatabase and i had no luck connecthing to it with C++…

Probably there’s something wrong I’m doing… but I’ll do bigger research

@WCode are those classes used to connect to Database ?

@CNKIT @Slayemin - well if I don’t figure out a way to connect to Microsoft SQL Server or MySQL in C++ can I make the Server send post requests to web server and web server fetch data from database? Will that make it slow?

I’ve never tried it and I can only speculate on the performance cost. If you have an internal webserver which is not publicly accessible, you may not have to worry as much about security vulnerabilities. The performance would probably be okay since the latency isn’t an issue. Performance may not be your biggest concern though. I think the biggest issue you’ll run into is just the usability of your webserver solution. So, you don’t know how to do a SQL query in C++, but you do know how to do it in a web environment. So you decide to have a premade query in the web environment and you send the query parameters via a get request to a webserver. The webserver runs the query… but how does it get the query results back to your program? Maybe you have an idea for that, but its probably going to be hackish. The other issue is that you’re adding another point of failure / dependency on your game. What happens to players if the webserver goes down? Personally, I think its an unnecessary and unwieldy layer of abstraction which is just a bad work-around for not figuring out how to access a database via C++. I know it can be done in C++, though I’ve only accessed MSSQL databases using C#.

No problem with reading the data back to the program, and no it’s not hackish way, basicly you read the stream, and get the output of the page as string returned to C++. But when you said: “What happens to the players if the webserver goes down?” started concerning me a lot. I guess this mean I’d better try to find a way to connect C++ to SQL Server… But I tried almost if not all libraries for that… and everywhere I get the same errors… Do you know any other libraries other than: MySQL C++ Connector/ MySQL++ / SQL Native Client / QSqlDatabase

Just tried again with Qt here’s what I get:

I get exactly the same errors when I try with other libraries…

Did you add .lib files to your additional depedencies list?

Yes I did put them