This example shows how to insert and retrieve a blob in one chunk.
// ********************************************************************* // Copyright 1994-2008 iAnywhere Solutions, Inc. All rights reserved. // This sample code is provided AS IS, without warranty or liability // of any kind. // // You may use, reproduce, modify and distribute this sample code // without limitation, on the condition that you retain the foregoing // copyright notice and disclaimer as to the original iAnywhere code. // // ********************************************************************* #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include "sacapidll.h" int main( ) { SQLAnywhereInterface api; a_sqlany_connection *conn; a_sqlany_stmt *stmt; unsigned int I; unsigned char *data; size_t size = 1024*1024; // 1MB blob int code; a_sqlany_data_value value; int num_cols; unsigned int max_api_ver; a_sqlany_bind_param param; if( !sqlany_initialize_interface( &api, NULL ) ) { printf( "Could not initialize the interface!\n" ); exit( 0 ); } assert( api.sqlany_init( "my_php_app", SQLANY_CURRENT_API_VERSION, &max_api_ver ) ); conn = api.sqlany_new_connection(); if( !api.sqlany_connect( conn, "uid=dba;pwd=sql" ) ) { char buffer[SACAPI_ERROR_SIZE]; code = api.sqlany_error( conn, buffer, sizeof(buffer) ); printf( "Could not connection[%d]:%s\n", code, buffer ); goto clean; } printf( "Connected successfully!\n" ); api.sqlany_execute_immediate( conn, "drop table my_blob_table" ); assert( api.sqlany_execute_immediate( conn, "create table my_blob_table (size integer, data long binary)" ) != 0); stmt = api.sqlany_prepare( conn, "insert into my_blob_table( size, data ) values( ?, ?)" ); assert( stmt != NULL ); data = (unsigned char *)malloc( size ); // initialize the buffer for( I = 0; I < size; I++ ) { data[i] = I % 256; } // initialize the parameters api.sqlany_describe_bind_param( stmt, 0, ¶m ); param.value.buffer = (char *)&size; param.value.type = A_VAL32; // This needs to be set as the server does not // know what data will be inserting. api.sqlany_bind_param( stmt, 0, ¶m ); api.sqlany_describe_bind_param( stmt, 1, ¶m ); param.value.buffer = (char *)data; param.value.length = &size; param.value.type = A_BINARY; // This needs to be set for the same reason as above. api.sqlany_bind_param( stmt, 1, ¶m ); assert( api.sqlany_execute( stmt ) ); api.sqlany_free_stmt( stmt ); api.sqlany_commit( conn ); stmt = api.sqlany_execute_direct( conn, "select * from my_blob_table" ); assert( stmt != NULL ); assert( api.sqlany_fetch_next( stmt ) == 1 ); num_cols = api.sqlany_num_cols( stmt ); assert( num_cols == 2 ); api.sqlany_get_column( stmt, 0, &value ); assert( *((int*)value.buffer) == size ); assert( value.type == A_VAL32 ); api.sqlany_get_column( stmt, 1, &value ); assert( value.type == A_BINARY ); assert( *(value.length) == size ); for( I = 0; I < (*value.length); I++ ) { assert( (unsigned char)(value.buffer[i]) == data[i]); } assert( api.sqlany_fetch_next( stmt ) == 0 ); api.sqlany_free_stmt( stmt ); api.sqlany_disconnect( conn ); clean: api.sqlany_free_connection( conn ); api.sqlany_fini(); sqlany_finalize_interface( &api ); printf( "Success!\n" ); } |
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |