Difference between revisions of "OpenDBX/C API/odbx query"

From Linuxnetworks
< OpenDBX‎ | C API
Jump to: navigation, search
(see also)
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
'''Send a statement to the database server'''
  
  int odbx_query(
+
  #include <opendbx/api.h>
    odbx_t* handle,
+
    const char* query,
+
    unsigned long length )
+
  
= Description: =
+
int '''odbx_query'''(
 +
    odbx_t* '''''handle''''',
 +
    const char* '''''stmt''''',
 +
    unsigned long '''''length''''' )
  
Sends a query string to the database server which must be terminated by a \0 character. Some backends support multiple statements per query, which can be tested by calling [[OpenDBX_get_option|odbx_get_option()]].
+
== Description ==
  
After invoking odbx_query(), the results sent back by the server are available via the [[OpenDBX_result|odbx_result()]] function.
+
Sends the given statement to the database server associated to '''''handle''''' by [[OpenDBX/API/odbx_init|odbx_init]]() for execution. The statement can contain non-displayable characters as arguments in conditions but they have to be escaped via [[OpenDBX/API/odbx_escape|odbx_escape]]() before adding them to the statement. Even more, every argument whose source is not controlled by the programmer must be escaped first to avoid SQL injection attacks to avoid serious harm!
  
= Parameters: =
+
After invoking [[OpenDBX/API/odbx_query|odbx_query]](), one or more result sets will be sent back by the server and are available via [[OpenDBX/API/odbx_result|odbx_result]](). Before these sets aren't processed by calling [[OpenDBX/API/odbx_row_fetch|odbx_row_fetch]](), no other queries can be sent to the database server. Otherwise, [[OpenDBX/API/odbx_query|odbx_query]]() will return a backend error from the native database library.
  
* handle: Connection object created by [[OpenDBX_init|odbx_init()]]
+
The first parameter '''''handle''''' is the connection object created and returned by [[OpenDBX/API/odbx_init|odbx_init]]() which becomes invalid as soon as it was supplied to [[OpenDBX/API/odbx_finish|odbx_finish]]().
* query: String terminated by \0 consisting of one or more statements
+
* length: Length of the query string without the terminating \0 character
+
  
= Return values: =
+
The statement stored in the '''''stmt''''' parameter must be a valid statement understood by the receiving database server and it should be terminated by a \0 character. Some backends support multiple statements per query, which can be tested by calling [[OpenDBX/API/odbx_get_option|odbx_get_option]]().
  
* ODBX_ERR_SUCCESS on success
+
The '''''length''''' parameter must contain the length of the statement in bytes without the terminating \0 character. If variable sized character sets like UTF-8 are used, the same rule applies. This function doesn't support the wide character type (wchar_t) which uses two or four bytes per character. If you feed 0 (zero) as '''''length''''' parameter to the function, it will calculate the size of the statement on its own.
* Less than zero if an error occured
+
  
= Errors: =
+
== Return value ==
  
* -ODBX_ERR_BACKEND: Any error returned by the backend
+
[[OpenDBX/API/odbx_query|odbx_query]]() returns ODBX_ERR_SUCCESS, or an error code whose value is less than zero if one of the operations couldn't be completed successfully. Possible error codes are listed in the error section and they can be feed to [[OpenDBX/API/odbx_error|odbx_error]]() and [[OpenDBX/API/odbx_error_type|odbx_error_type]]() to get further details.
* -ODBX_ERR_PARAM: "handle" or query string are NULL or the structure is invalid
+
* -ODBX_ERR_NOMEM: Allocating new memory failed
+
  
= See also: =
+
== Errors ==
  
* [[OpenDBX_error]]
+
; -ODBX_ERR_BACKEND : The native database library returned an error because it wasn't able to handle the statement correctly
* [[OpenDBX_error_type]]
+
; -ODBX_ERR_PARAM : Either '''''handle''''' or '''''stmt''''' are NULL or '''''handle''''' is invalid
* [[OpenDBX_result]]
+
; -ODBX_ERR_NOMEM : Allocating the required memory for the statement failed
  
 +
== See also ==
  
----
+
* [[OpenDBX/API/odbx_error|odbx_error]]()
Back to [[OpenDBX API|Overview]]
+
* [[OpenDBX/API/odbx_error_type|odbx_error_type]]()
 +
* [[OpenDBX/API/odbx_escape|odbx_escape]]()
 +
* [[OpenDBX/API/odbx_result|odbx_result]]()

Latest revision as of 23:13, 28 April 2009

Send a statement to the database server

#include <opendbx/api.h>
int odbx_query(
    odbx_t* handle,
    const char* stmt,
    unsigned long length )

Description

Sends the given statement to the database server associated to handle by odbx_init() for execution. The statement can contain non-displayable characters as arguments in conditions but they have to be escaped via odbx_escape() before adding them to the statement. Even more, every argument whose source is not controlled by the programmer must be escaped first to avoid SQL injection attacks to avoid serious harm!

After invoking odbx_query(), one or more result sets will be sent back by the server and are available via odbx_result(). Before these sets aren't processed by calling odbx_row_fetch(), no other queries can be sent to the database server. Otherwise, odbx_query() will return a backend error from the native database library.

The first parameter handle is the connection object created and returned by odbx_init() which becomes invalid as soon as it was supplied to odbx_finish().

The statement stored in the stmt parameter must be a valid statement understood by the receiving database server and it should be terminated by a \0 character. Some backends support multiple statements per query, which can be tested by calling odbx_get_option().

The length parameter must contain the length of the statement in bytes without the terminating \0 character. If variable sized character sets like UTF-8 are used, the same rule applies. This function doesn't support the wide character type (wchar_t) which uses two or four bytes per character. If you feed 0 (zero) as length parameter to the function, it will calculate the size of the statement on its own.

Return value

odbx_query() returns ODBX_ERR_SUCCESS, or an error code whose value is less than zero if one of the operations couldn't be completed successfully. Possible error codes are listed in the error section and they can be feed to odbx_error() and odbx_error_type() to get further details.

Errors

-ODBX_ERR_BACKEND 
The native database library returned an error because it wasn't able to handle the statement correctly
-ODBX_ERR_PARAM 
Either handle or stmt are NULL or handle is invalid
-ODBX_ERR_NOMEM 
Allocating the required memory for the statement failed

See also