add some utility db functions

This commit is contained in:
Isaac Connor 2015-07-16 13:04:28 -04:00
parent 01518d6aa9
commit 48fa0c3d42
2 changed files with 38 additions and 0 deletions

View File

@ -75,3 +75,38 @@ void zmDbClose()
zmDbConnected = false; zmDbConnected = false;
} }
} }
MYSQL_RES * zmDbFetch( const char * query ) {
if ( ! zmDbConnected ) {
Error( "Not connected." );
return NULL;
}
if ( mysql_query( &dbconn, query ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
return NULL;
}
Debug( 4, "Success running query: %s", query );
MYSQL_RES *result = mysql_store_result( &dbconn );
if ( !result ) {
Error( "Can't use query result: %s for query %s", mysql_error( &dbconn ), query );
return NULL;
}
return result;
} // end MYSQL_RES * zmDbFetch( const char * query );
MYSQL_ROW zmDBFetchOne( const char *query ) {
MYSQL_RES *result = zmDbFetch( query );
int n_rows = mysql_num_rows( result );
if ( n_rows != 1 ) {
Error( "Bogus number of lines return from query, %d returned for query %s.", n_rows, query );
return NULL;
}
MYSQL_ROW dbrow = mysql_fetch_row( result );
if ( ! dbrow ) {
Error("Error getting row from query %s. Error is %s", query, mysql_error( &dbconn ) );
return NULL;
}
return dbrow;
}

View File

@ -32,6 +32,9 @@ extern int zmDbConnected;
void zmDbConnect(); void zmDbConnect();
void zmDbClose(); void zmDbClose();
MYSQL_RES * zmDbFetch( const char *query );
MYSQL_ROW zmDBFetchOne( const char *query );
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif