Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to "handle" database queries in code?

Started by SymLinked Sep 22, 2014 at 9:28 PM 3 replies 1.4k views
Original Post
SymLinked
SymLinked

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!

Madhed
Madhed

What programming language and (if any) database library are you using?

SymLinked
SymLinked

I'm using C++ and SQLite's C implementation.

I guess I'm just unused to all this.

SeanMiddleditch
SeanMiddleditch
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).
Sean Middleditch – Game Systems Engineer – Join my team!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.