A ResultSet is created by executing a SQL SELECT statement using the Connection_executeQuery() method or by using a prepared statement's PreparedStatement_executeQuery() method.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The ResultSet_next() method moves the cursor to the next row, and because it returns FALSE when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row.
The ResultSet interface provides getter methods for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1.
Column names used as input to getter methods are case sensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query that generated the result set. For columns that are NOT explicitly named in the query, it is best to use column indices. If column names are used, there is no way for the programmer to guarantee that they actually refer to the intended columns.
The following examples demonstrate how to obtain a ResultSet and get values from the set:
rs = Connection_executeQuery(con, "SELECT ssn, name, picture FROM CUSTOMERS"); if (rs) while(ResultSet_next(rs)) { int ssn = ResultSet_getInt(rs, 1); const char *name = ResultSet_getStringByName(rs, "name"); int blobSize; void *picture = ResultSet_getBlob(rs, 3, &blobSize); ... } else log("Error: %s\n", Connection_getLastError(con));Here is another example where a generated result is selected and printed:
ResultSet_T rs = Connection_executeQuery(con, "SELECT count(*) FROM USERS"); if(rs && ResultSet_next(rs)) printf("Number of users: %s\n", ResultSet_getString(rs, 1));
Defines | |
#define | T ResultSet_T |
Typedefs | |
typedef T * | T |
Functions | |
int | ResultSet_getColumnCount (T R) |
Returns the number of columns in this ResultSet object. | |
const char * | ResultSet_getColumnName (T R, int column) |
Get the designated column's name. | |
int | ResultSet_next (T R) |
Moves the cursor down one row from its current position. | |
long | ResultSet_getColumnSize (T R, int columnIndex) |
Returns column size in bytes. | |
const char * | ResultSet_getString (T R, int columnIndex) |
Retrieves the value of the designated column in the current row of this ResultSet object as a C-string. | |
const char * | ResultSet_getStringByName (T R, const char *columnName) |
Retrieves the value of the designated column in the current row of this ResultSet object as a C-string. | |
int | ResultSet_getInt (T R, int columnIndex) |
Retrieves the value of the designated column in the current row of this ResultSet object as an int. | |
int | ResultSet_getIntByName (T R, const char *columnName) |
Retrieves the value of the designated column in the current row of this ResultSet object as an int. | |
long long int | ResultSet_getLLong (T R, int columnIndex) |
Retrieves the value of the designated column in the current row of this ResultSet object as a long long. | |
long long int | ResultSet_getLLongByName (T R, const char *columnName) |
Retrieves the value of the designated column in the current row of this ResultSet object as a long long. | |
double | ResultSet_getDouble (T R, int columnIndex) |
Retrieves the value of the designated column in the current row of this ResultSet object as a double. | |
double | ResultSet_getDoubleByName (T R, const char *columnName) |
Retrieves the value of the designated column in the current row of this ResultSet object as a double. | |
const void * | ResultSet_getBlob (T R, int columnIndex, int *size) |
Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer. | |
const void * | ResultSet_getBlobByName (T R, const char *columnName, int *size) |
Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer. | |
int | ResultSet_readData (T R, int columnIndex, void *b, int length, long off) |
Reads length bytes from a blob or text field and stores them into the byte buffer pointed to by b . |
|
|
|
|
|
Returns the number of columns in this ResultSet object.
|
|
Get the designated column's name.
|
|
Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to this method makes the first row the current row; the second call makes the second row the current row, and so on. When there are not more available rows FALSE is returned. An empty ResultSet will return FALSE on the first call to ResultSet_next().
|
|
Returns column size in bytes.
If the column is a blob then this methtod returns the number of bytes in that blob. No type conversions occur. If the result is a string (or a number since a number can be converted into a string) then return the number of bytes in the resulting string. If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a C-string.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a C-string.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as an int.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as an int.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a long long.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a long long.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a double.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a double.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer.
If
|
|
Retrieves the value of the designated column in the current row of this ResultSet object as a void pointer.
If
|
|
Reads
Reading stops when rs= Connection_executeQuery(con, "select text from books;"); while(ResultSet_next(rs)) { int n= 0; long off= 0; unsigned char buf[8193]; while((n= ResultSet_readData(rs, 1, buf, 8192, off))>0) { buf[n]= 0; printf("%s", buf); off+= n; } }It is a checked runtime error for b to be NULL
|
Copyright © 2006 Tildeslash Ltd. All rights reserved.