Jump to content
  • Advertisement
Sign in to follow this  
SymLinked

How to "handle" database queries in code?

This topic is 1424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

To begin with I want to excuse my English. It's not my native tongue. I find this difficult to explain but I'll try.

Background: I'm designing a tool which sends incremental updates to a server. I want to store all information for each upload in a SQLite database.

 

Problem: I don't know how to handle database queries with minimal fuss. I would love to just make the query, block until it's complete and then return the results to the client. Instead, I create a asyncronous task which runs the query and then calls a delegate when it's done. Or I poll the task (without blocking) to see if it's done.

 

But this forces me to create a lot of extra code just for handling the results, invalidate the delegate and stop the task if the client disconnects, etc. Is this the right way to go? I supose you can't tell from the limited information I've given, but does it sound totally wrong?

 

Thanks!

 

Share this post


Link to post
Share on other sites
Advertisement
There's a few different pieces to this question.

The first is the handling of queries itself. C++ does not have the best interfaces for such things and SQLite's C API is also a bit lacking. You want to make sure you're very careful to use the placeholder syntax and never ever construct a raw SQL query using user input, though; that's the surest way to get a SQL injection security hole!

The second bit is how you're handling the request and response. This is not an easy topic and C++ by itself does not provide a great answer here. The short version is that your approach is "correct." The long version is that you should use a framework/library that makes getting all the fiddly details correct since C++ doesn't.

I highly recommend you look at things like NodeJS, Python's Tornado, or other event-driven IO-oriented application server frameworks to at least see the structure of how they deal with request-response scenarios like this; you want to emulate that kind of structure. The upcoming version of C++ should offer a lot more language and library utilities to make this kind of application much easier to develop, reason about, and debug, but for now you'll have to rely on a third-party library (or roll your own).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!