ODBC installation/Use/programming

Source: Internet
Author: User
Tags assert driver manager dsn sql error stmt mysql odbc driver

Objective:

The main explanation of ODBC API, take MySQL as an example, from configuration to installation, and then to specific programming, with a view to ODBC have a preliminary understanding.

*) Download MySQL, select Community Edition MySQL, and install
http://dev.mysql.com/downloads/mysql/

*) Download MYSQL-ODBC driver driver, remember to download version 32
http://dev.mysql.com/downloads/connector/odbc/

*) using the ODBC Data Source Manager, the recommended Odbcad32.exe
Http://support.microsoft.com/kb/942976/zh-cn

*) Drive in the registry
In the registry directory entry Hkey_local_machine\software\odbc\odbcinst. INI, you can see the specifics of MySQL ODBC driver. Catalog Item ODBC drivers holds all ODBC driver information that is already installed on the system. Catalog items MySQL ODBC 5.3 Unicode driver saves specific MySQL driver information.
What is the difference between key driver and key setup? Driver corresponds to the DLL, is the implementation of the ODBC Driver, and the corresponding DLL for the setup, just applies to the System ODBC Driver Manager (Odbc.exe, Odbcad32.exe), used to configure the DNS data source. For a 32-bit driver, its corresponding registry directory entry is Hkey_local_machine\software\wow6432node\odbc\odbcinst. INI, this is also a difference point.

*) in the user/System DSN, add DSN, select Mysql-odbc,:

To edit a DSN:

In the Windows Registry directory entry Hkey_current_user\software\odbc\odbc. INI, the DSN created for the specific.

The data source Mysql_test is previously created, specifically documenting detailed configuration information, where driver specifically corresponds to C:\Program Files (x86) \mysql\connector ODBC 5.3\myodbc5w.dll, Database corresponding to test. Each data source has a different specific item. The ODBC Data Sources catalog entry, which records all other data source items, makes it easy to tell the driver manager the data source type of the specific ODBC drive.

The difference between a user DSN and a System DSN is that the user DSN is valid only for the current user, and the location information for the registry where it resides is generally hkey_current_user\software\odbc\odbc. INI directory entry, and the System DSN is valid for all users in HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC. INI directory entry.

*) under Database test, create a

*) Odbc-api Example
Http://wenku.baidu.com/link?url=Reerd4fWwMOoSLW1gmCQgluaMFEMUBq1Wq-o6e3Iu13UbZaaqKYpwNPnX0Ik_ Rxebvhtng380ghmzbjxfv65k0f3410slf-5wwd3lluwte7
Http://wenku.baidu.com/view/7cefbf1ec5da50e2524d7fbc.html

When the compiler appears with the following error:
Error C2146:syntax error:missing '; ' before identifier ' Sqlhwnd '
Need to include windows.h before containing sql.h, sqlext.h header file
Refer to the following solutions: http://bbs.csdn.net/topics/60260260

Error occurred: cannot convert parameter 2 from ' SQLCHAR * ' to ' Sqlwchar * '
Refer to the following solutions: http://blog.sina.com.cn/s/blog_6d2a0a1a01019k9r.html

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 /* *   *   for example: *       odbc example * * *   本例子的目的: 通过odbc编程, 来对odbc的接口有个感性而深入的认识 * * */#include <stdio.h>#include <assert.h>#include <windows.h> #include <sql.h> #include <sqlext.h> int main() {    HENV henv;    // *) 申请环境句柄     SQLRETURN rcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 设置ODBC版本的环境属性     rcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 分配连接句柄     SQLHDBC hdbc;     rcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 连接数据源     rcode = SQLConnect(hdbc, (SQLCHAR *)"mysql_test", SQL_NTS,         (SQLCHAR *)"root", SQL_NTS, (SQLCHAR *)"123456", SQL_NTS);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 创建SQL语句句柄     SQLHSTMT stmt;     rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc,&stmt);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 执行具体的sql     rcode = SQLExecDirect(stmt, (SQLCHAR*)"select * from tb_student", SQL_NTS);     assert(!(rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO));    // *) 绑定和获取具体的数据项     SQLINTEGER res = SQL_NTS;     SQLCHAR name[128];     SQLINTEGER age;     SQLBindCol(stmt, 2, SQL_C_CHAR, name, sizeof(name), &res);     SQLBindCol(stmt, 3, SQL_C_SLONG, &age, sizeof(age), &res);     while ((rcode=SQLFetch(stmt))!=SQL_NO_DATA_FOUND) {         if( rcode == SQL_ERROR) {             printf("sql error!\n");         } else {             printf("name:%s, age:%ld\n",name, age);         }     }    // *) 清理工作, 释放具体的资源句柄     SQLFreeHandle(SQL_HANDLE_STMT, stmt);     SQLDisconnect(hdbc);     SQLFreeHandle(SQL_HANDLE_DBC, hdbc);     SQLFreeHandle(SQL_HANDLE_ENV, henv);    return 0;}<span style="line-height: 1.5;"> </span>
Data type conversion list with ODBC

C Language Data type name

ODBC data type definition

Actual type of C language

Sql_c_char

SQLCHAR *

unsigned char *

SQL_C_SSHORT[J]

Sqlsmallint

Short int

SQL_C_USHORT[J]

Sqlusmallint

unsigned short int

SQL_C_SLONG[J]

Sqlinteger

Long int

SQL_C_ULONG[J]

Sqluinteger

unsigned long int

Sql_c_float

Sqlreal

Float

Sql_c_double

SqlDouble, Sqlfloat

Double

Sql_c_bit

SQLCHAR

unsigned char

SQL_C_STINYINT[J]

Sqlschar

Signed Char

SQL_C_UTINYINT[J]

SQLCHAR

unsigned char

Sql_c_sbigint

Sqlbigint

_INT64[H]

Sql_c_ubigint

Sqlubigint

unsigned _int64[h]

Sql_c_binary

SQLCHAR *

unsigned char *

Sql_c_bookmark[i]

BOOKMARK

unsigned long int[d]

Sql_c_varbookmark

SQLCHAR *

unsigned char *

SQL_C_TYPE_DATE[C]

Sql_date_struct

struct Tagdate_struct {
Sqlsmallint year;
Sqlusmallint month;
Sqlusmallint Day;
} date_struct; A

SQL_C_TYPE_TIME[C]

Sql_time_struct

struct Tagtime_struct {
Sqlusmallint hour;
Sqlusmallint minute;
Sqlusmallint second;
} time_struct; A

SQL_C_TYPE_TIMESTAMP[C]

Sql_timestamp_struct

struct Tagtimestamp_struct {
Sqlsmallint year;
Sqlusmallint month;
Sqlusmallint Day;
Sqlusmallint hour;
Sqlusmallint minute;
Sqlusmallint second;
Sqluinteger fraction; [b]
} timestamp_struct; A

Sql_c_numeric

Sql_numeric_struct

struct Tagsql_numeric_struct {
SQLCHAR Precision;
Sqlschar scale;
SQLCHAR Sign[g];
SQLCHAR Val[sql_max_numeric_len]; [E], [f]
} sql_numeric_struct;

Sql_c_guid

SqlGuid

struct Tagsqlguid {
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} SqlGuid; K

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.