没有返回数字 [英] Not Getting a Number Returned

查看:69
本文介绍了没有返回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到Access 2010数据库.问题是我的查询没有返回数字.任何帮助表示赞赏.谢谢.


I''m connecting to an Access 2010 database. The problem is that my query is not returning a number. Any help is appreciated. Thank you.


/* Connect to the 'Fileber.accdb' database */
rc = SQLDriverConnect(hDbc, NULL, szDSN,  _countof(szDSN), 
szConnStrOut, 255, &iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc)) 
	{
	printf("%s: Successfully connected to database. Data source name: \n  %s\n",					DAM, szConnStrOut);
	const char* TOTL;
	TOTL = "SELECT COUNT(*) FROM tblClients";
	printf("%s: Total of Clients %s.\n", TOTL);
		}
		else
		{
		printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
		}

推荐答案

我所看到的就是将看起来像SQL查询的可疑字符串"粘贴到字符串变量中并打印出来.

我什么也没发现对字符串本身执行SQL查询,所以为什么要期待结果呢?
All I see is you sticking a "string of characters that looks suspiciously like an SQL Query" into a string variable and printing that out.

I see nothing that issues an SQL Query on the string itself so why would you expect a result?


摆脱困境,将其键入所需的数据类型

SQLCHAR *查询= (SQLCHAR *) 选择tblIP.[IPAddress],tblIP.[IPStatus],tblIP.[IPType]从tblIP订购tblIP.[IPAddress] ASC;;
Get out the big hammer and typcast it to the desired datatype

SQLCHAR* query = (SQLCHAR *)"SELECT tblIP.[IPAddress], tblIP.[IPStatus], tblIP.[IPType] FROM tblIP ORDER BY tblIP.[IPAddress] ASC;";


此方法有效,但是我遇到一个错误;它仍然会编译并返回信息.代码然后出现错误.

This works except that I am getting one error, however; it does still compile and returns info. The code then the error.

#include <windows.h>
#include <stdio.h>
//#include <sql.h>
#include <sqlext.h>
//#include <odbcss.h>
//#include <string>
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";

/* Connection string for Direct ODBC */
SQLCHAR szDSN[256] = 
    "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:\\FILEBLOCK\\Fileblocker.accdb;";

main()
{
    HENV    hEnv;
    HDBC    hDbc;

    /* ODBC API return status */
    SQLRETURN  rc;

    SQLSMALLINT  iConnStrLength2Ptr;
    SQLCHAR      szConnStrOut[255];
	
    
	SQLCHAR* query = "SELECT tblIP.[IPAddress], tblIP.[IPStatus], tblIP.[IPType] FROM tblIP ORDER BY tblIP.[IPAddress] ASC;";
    
    SQLCHAR         chval1[128], chval2[128], chval3[128], colName[128];
    SQLINTEGER      ret1, ret2, ret3;

    /* Number of rows and columns in result set */
    SQLINTEGER      rowCount = 0;
    SQLSMALLINT     fieldCount = 0, column = 0;
    HSTMT           hStmt;

    /* Allocate an environment handle */
    rc = SQLAllocEnv(&hEnv);
    /* Allocate a connection handle */
    rc = SQLAllocConnect(hEnv, &hDbc);

    /* Connect to the 'Fileblocker.accdb' database */
    rc = SQLDriverConnect(hDbc, NULL, szDSN,  _countof(szDSN), 
		szConnStrOut, 255, &iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
    if (SQL_SUCCEEDED(rc)) 
    {
        printf("%s: Successfully connected to database. Data source name: \n  %s\n", 
           DAM, szConnStrOut);

        /* Prepare SQL query */
        printf("%s: SQL query:\n  %s\n", DAM, query);
		rc = SQLAllocStmt(hDbc,&hStmt);
        rc = SQLPrepare(hStmt, query, SQL_NTS);
       
        /* Bind result set columns to the local buffers */ 
        rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, &ret1);
        rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, &ret2);
		rc = SQLBindCol(hStmt, 3, SQL_C_CHAR, chval3, 128, &ret3);

        /* Execute the query and create a record set */
        rc = SQLExecute(hStmt); 
        if (SQL_SUCCEEDED(rc)) 
        {
            printf("%s: Retrieve schema info for the given result set:\n", DAM);
            SQLNumResultCols(hStmt, &fieldCount);
            if (fieldCount > 0)
            {
                for (column = 1; column <= fieldCount; column++)
                {
                    SQLDescribeCol(hStmt, column,
                        colName, sizeof(colName), 0, 0, 0, 0, 0);
                    printf(" | %s", colName);    
                }
                printf("\n");
            }
            else
            {
                printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
            }

            printf("%s: Fetch the actual data:\n", DAM);
            /* Loop through the rows in the result set */
            rc = SQLFetch(hStmt);
            while (SQL_SUCCEEDED(rc)) 
            {
                printf(" | %s | %s\n", chval1, chval2, chval3);
                rc = SQLFetch(hStmt);
                rowCount++;
            };

            printf("%s: Total Row Count: %d\n", DAM, rowCount);
            rc = SQLFreeStmt(hStmt, SQL_DROP);
        }
    }
    else
    {
        printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
    }

    /* Disconnect and free up allocated handles */
    SQLDisconnect(hDbc);
    SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

    printf("%s: Cleanup. Done.\n", DAM);
}</string></odbcss.h></sqlext.h></sql.h></stdio.h></windows.h>



错误,它在SELECT Statemnet上.
1 IntelliSense:无法使用类型为"const char *"的值来初始化类型为"SQLCHAR *"的实体



The Error, It''s on the SELECT Statemnet.
1 IntelliSense: a value of type "const char *" cannot be used to initialize an entity of type "SQLCHAR *"


这篇关于没有返回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆