/**********************************************************************
* FILENAME : util.c
*
* DESCRIPTION : Various useful functions
*
* rtrim - right trim string
* getStr - get string from user
* getInt - get integer value from user
* extract_error - get error for handle from driver
* padOut - pad out string to given length
* itoa - create string of integer
* getDataSources - get list of Data Sources
* selectDSN - select data source
* getDescRecCount - get number of records in descriptor
* dumpDescriptorRec - uses SQLGetDescRec and output info
* for descriptor record
* dumpDescriptorHeaderFields - outouts descriptor
* header fields
* dumpDescriptorRecordFields - outputs descriptor
* record fields
* dumpDescriptors - dumps out descriptors in use on handle
* hasBookmarkRecord - returns true/false to indicate
* whether handle has bookmark rec (0)
*
* ODBC USAGE :
* See code.
*/
#define CHECK_ERROR(e, s, h, t) ({\
if (e!=SQL_SUCCESS && e != SQL_SUCCESS_WITH_INFO) {extract_error(s, h, t); goto exit;} \
})
#define MAXDSNS 10
#define MAXDSNLEN 100
char* rtrim(char* string, char junk)
{
char* original = string + strlen(string);
while(*--original == junk);
*(original + 1) = '\0';
return string;
}
// get string from user.
char getStr (char *label, char *retStr, int len, char confirm) {
char reply[3];
char *nl=0;
strcpy (reply, "Y");
if (strlen(label) > 0) {
printf ("%s : ", label);
}
fgets(retStr, len, stdin);
if ( (nl = strchr (retStr, '\n')) != NULL) {*nl='\0';}
if (confirm == 'Y') {
printf ("Confirm Y/N? : ");
fgets(reply, 3, stdin);
}
return reply[0];
}
// gets integer value from user. confirm ignored if number equals quitVal
char getInt (char *label, int *retInt, char confirm, int quitVal) {
char reply[3];
char buff[256];
if (strlen(label)>0) {
printf ("%s : ", label);
}
fgets(buff, sizeof(buff), stdin);
*retInt=atoi(buff);
if (confirm=='Y' && *retInt!=quitVal) {
printf ("Confirm Y/N? : ");
fgets(reply, 3, stdin);
}
return reply[0];
}
void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
SQLINTEGER i = 0;
SQLINTEGER NativeError;
SQLCHAR SQLState[ 7 ];
SQLCHAR MessageText[256];
SQLSMALLINT TextLength;
SQLRETURN ret;
fprintf(stderr, "\nThe driver reported the following error %s\n", fn);
do
{
ret = SQLGetDiagRec(type, handle, ++i, SQLState, &NativeError,
MessageText, sizeof(MessageText), &TextLength);
if (SQL_SUCCEEDED(ret)) {
printf("%s:%ld:%ld:%s\n",
SQLState, (long) i, (long) NativeError, MessageText);
}
}
while( ret == SQL_SUCCESS );
}
void padOut (char* data, char* padding, int max) {
memset (padding, ' ', max);
padding[max]='\0';
//printf ("Data '%s', Len %i\n", data, (int)strlen(data));
//printf ("Padding '%s', Len %i\n", padding, (int)strlen(padding));
//printf ("Max %i\n", max);
if (strlen(data)<strlen(padding)){
padding[strlen(padding)-strlen(data)]='\0';
} else {
padding[0]='\0';
}
return;
}
//implemenation of itoa
char* itoa(int n) {
char* ret = NULL;
int numChars = 0;
// Determine if integer is negative
int isNegative = 0;
if (n < 0) {
n = -n;
isNegative = 1;
numChars++;
}
// Count how much space we will need for the string
int temp = n;
do {
numChars++;
temp /= 10;
} while ( temp );
// Allocate space for the string (1 for negative sign, 1 for each digit,
// and 1 for null terminator)
ret = malloc (numChars + 1);
ret[numChars] = 0;
// Add the negative sign if needed
if (isNegative) ret[0] = '-';
// Copy digits to string in reverse order
int i = numChars - 1;
do {
ret[i--] = n%10 + '0';
n /= 10;
} while (n);
return ret;
}
// Example of passing 2D array ...
int getDataSources (SQLHANDLE env, int maxNo, int maxLen,
char dsns[maxNo][maxLen]) {
SQLRETURN retcode;
SQLCHAR dsnName[256];
SQLSMALLINT dsnNameLenReturned;
SQLCHAR driverDesc[256];
SQLSMALLINT driverDescLenReturned;
SQLUSMALLINT direction;
int count=0;
direction = SQL_FETCH_FIRST;
while (SQL_SUCCEEDED(retcode=SQLDataSources(env, direction, dsnName,
sizeof(dsnName),
&dsnNameLenReturned,
driverDesc,
sizeof(driverDesc),
&driverDescLenReturned))) {
direction = SQL_FETCH_NEXT;
strcpy (&dsns[count][0], dsnName);
count++;
}
return count;
}
int selectDSN (SQLHANDLE henv, char *dsn, char *prompt) {
char dsns[MAXDSNS][MAXDSNLEN];
int count, i;
count = getDataSources(henv, MAXDSNS, MAXDSNLEN, dsns);
printf ("%s\n", prompt);
printf ("0 - Quit\n");
for (i=0;i<count;i++) {
printf ("%i - %s\n", i+1, dsns[i]);
}
getInt ("DSN ? ", &i, 'N', 0);
if (i==0)
return SQL_ERROR;
else {
strcpy (dsn, &dsns[i-1][0]);
return SQL_SUCCESS;
}
}
//
// get number of column/param records in descriptor
// count does not include bookmark record ever
//
SQLSMALLINT getDescRecCount (SQLHDESC descriptor) {
SQLRETURN retcode;
SQLSMALLINT descCount=0;
// get number of fields in the descriptor
retcode = SQLGetDescField(descriptor, 0, SQL_DESC_COUNT, &descCount, 0, 0);
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
extract_error("SQLGetDescField (COUNT)",
descriptor, SQL_HANDLE_DESC);
descCount=0;
}
return descCount;
}
//
// Dump various settings or values of multiple fields of a descriptor record
// using ODBC function SQLGetDescRec ()
//
void dumpDescriptorRec (char *comment,
SQLHDESC descriptor,
int incBookmarkRec) {
int i;
SQLRETURN retcode;
SQLCHAR Name[255];
SQLSMALLINT BufferLength=255;
SQLSMALLINT StringLength;
SQLSMALLINT Type;
SQLSMALLINT SubType;
SQLLEN Length;
SQLSMALLINT Precision;
SQLSMALLINT Scale;
SQLSMALLINT Nullable;
SQLSMALLINT RecNumber;
SQLSMALLINT FieldIdentifier;
SQLSMALLINT descCount=0;
SQLINTEGER Len;
printf ("\n---\n%s\n---\n", comment);
descCount = getDescRecCount(descriptor);
printf ("%i Records\n", (int) descCount);
// Descriptor records are numbered from 0,
// with record number 0 being the bookmark record.
if (incBookmarkRec) {
i=-1;
} else {
i=0;
}
for (;i<descCount;i++) {
retcode = SQLGetDescRec(descriptor, i+1,
Name, BufferLength,
&StringLength, &Type,
&SubType, &Length,
&Precision, &Scale,
没有合适的资源?快使用搜索试试~ 我知道了~
ODBC API Example ODBC 范例程序
共97个文件
c:61个
png:21个
css:6个
需积分: 10 11 下载量 80 浏览量
2018-11-29
22:07:31
上传
评论
收藏 285KB ZIP 举报
温馨提示
easysoft上的ODBC范例,统一保存下来方便复制粘贴。反正会直接使用ODBC API 的人也不多了。
资源推荐
资源详情
资源评论
收起资源包目录
ODBC Example.zip (97个子文件)
ODBC Example
ListEnvironmentAttribute.c 4KB
CallSPInsertRecs.c 11KB
ODBC Code Samples.html 24KB
ListDBTables.c 10KB
ListTableColumns.c 12KB
ListConnectionAttributes.c 8KB
Transactions.c 9KB
CallSPMultipleResultSets.c 5KB
CallSPWithInOutParam.c 7KB
ODBC from C Tutorial Part 1.html 107KB
CallSPFindID.c 8KB
InsertRecs.c 6KB
ReadingDataWithExtendedFetch.c 13KB
BrowseConnect.c 3KB
ListProcedures.c 5KB
BindColumn.c 4KB
BulkOperations.c 26KB
WritingSingleLongTextFields.c 7KB
CreateTableWithID.c 3KB
SelectWhereInSelect.c 4KB
DescribeParamAndCol.c 22KB
CallSPEditRecord.c 16KB
UsingParameterArraysAndKeySetCursors.c 10KB
ListTablePrivileges.c 11KB
ColumnAttributes.c 10KB
CallSPWithParam.c 6KB
ListDSNs.c 2KB
UsingSelectWithParameterArrays.c 7KB
ListDrivers.c 2KB
ReadingMultipleLongTextFields.c 9KB
SQLColumnPrivileges.c 12KB
ReadingSingleLongTextFields .c 5KB
CreateTableNoID.c 3KB
UsingInsertWithParameterArrays.c 6KB
UsingKeysetCursors.c 17KB
ParseNativeSql.c 3KB
CallSPWithParamArray.c 7KB
ShowDescriptorContents.c 13KB
CallSPOutputParameterStreaming.c 7KB
CallSP.c 5KB
CreateTable.c 5KB
ListStatistics.c 13KB
ListDataTypes.c 8KB
TVPInsertRecs.c 14KB
DropTable.c 4KB
CallSPFindName.c 8KB
ODBC Code Samples_files
twitter.png 1KB
dc.js.下载 45KB
googleplus.png 550B
main.css 10KB
linkedin.png 1KB
search.png 437B
jscripts.js.下载 10KB
facebook.png 1KB
easysoft_logo.png 3KB
youtube.png 400B
nav.css 4KB
util.c 34KB
ODBC from C Tutorial Part 2 - Fetching Results.html 69KB
AsyncExecution.c 6KB
DescribeColumn.c 7KB
ListSpecialColumns.c 11KB
ListProcedureColumns.c 11KB
ODBC from C Tutorial Part 2 - Fetching Results_files
twitter.png 1KB
dc.js.下载 45KB
googleplus.png 550B
main.css 10KB
linkedin.png 1KB
search.png 437B
jscripts.js.下载 10KB
facebook.png 1KB
easysoft_logo.png 3KB
youtube.png 400B
nav.css 4KB
TVPCopyTable.c 20KB
TVPAuthorsAndBooks.c 25KB
UsingSingleParameterArray.c 6KB
ListTables.c 4KB
UsingMultipleParamArrays.c 7KB
ListTableKeyStructure.c 10KB
WritingMultipleLongTextFields.c 21KB
CopyDescriptor.c 13KB
CallSPUtil.c 14KB
ODBC from C Tutorial Part 1_files
twitter.png 1KB
dc.js.下载 45KB
googleplus.png 550B
main.css 10KB
linkedin.png 1KB
search.png 437B
jscripts.js.下载 10KB
facebook.png 1KB
easysoft_logo.png 3KB
youtube.png 400B
nav.css 4KB
BindParameter.c 5KB
DescribeAndBindColumns.c 7KB
ListFunctions.c 4KB
共 97 条
- 1
资源评论
pengyue_gg
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功