当前位置:网站首页>Introduction to HNU database system ODBC
Introduction to HNU database system ODBC
2022-06-25 21:43:00 【HNU Yuelu mountain lady】
What to say
Thanks to my cool friend , Thank him for helping me when I am confused .
I used it this time kingbase and mysql, I remember there were a lot of problems , But now I don't remember what went wrong …
The experiment purpose
Learn to configure ODBC data source . Familiar use ODBC To design database applications , Familiar through ODBC Interface to access and operate heterogeneous databases .
Experimental platform and experimental tools
The experiment platform :KingbaseES Database management system ,KingbaseES ODBC Driver.
adopt C Language to write applications that access the database . Programming tools optional .
The contents and requirements of the experiment
stay KingbaseES Database management system , adopt KingbaseES ODBC Driver, Use ODBC Write applications to perform various data operations on heterogeneous databases .
Configure two different data sources , Write a program to connect two different RDBMS Data source , Operate on heterogeneous databases . for example , take KingbaseES The data in a table of the database is backed up to SQL Server In the table of the database .
Fill in the experiment report carefully , And submit the source program , Ensure that you can compile and run correctly .
Knowledge preparation
What is? ODBC
ODBC(OpenDatabaseConnectivity, Open database Interconnection ) It's Microsoft's open service structure (WOSA,WindowsOpenServicesArchitecture) A part of the database , This technology provides access to different kinds of SQL The database provides a common interface .
ODBC Is a structure based query language (SQL) Of , And use it as a standard for accessing data . This interface provides interoperability , That is, an application can access different through a set of common code DBMS.
To configure ODBC The way of data sources
There are two ways to configure a data source :
Method 1 : Run the data source management tool to configure .
Method 2 : Use Driver Manager Provided ConfigDsn Function to add 、 Modify or delete data source . This approach is especially useful for temporary data sources created in applications .
The experimental requirements
Give the process of configuring two different data sources . Submit application source code , And mark the necessary notes , Explain the function of the program as clearly as possible , Method of implementation , Key data structure 、 Variable 、 Definition of function .
Experimental report
1.MySQL Data source configuration
1-1. install MySQL Of ODBC drive 
1-2. Data source configuration
open 【 Control panel 】, Get into 【 Systems and security 】, choice 【Windows Tools 】, open 【ODBC data source (64 position )】
In user DSN Add 【MySQL ODBC 8.0 ANSI Driver】
( Choose here Unicode Better , In this way, Chinese can be displayed normally )
Enter the corresponding information , Then choose our test database , Click on 【test】
If successful, this interface will be displayed 
2.KingBase Data source configuration
2-1. install KingBase Of ODBC drive 
2-2. Data source configuration
open 【 Control panel 】, Get into 【 Systems and security 】, choice 【Windows Tools 】, open 【ODBC data source (64 position )】
In user DSN Add 【KingbaseES 8.6 ODBC Driver ANSI】
( Choose here Unicode Better , In this way, Chinese can be displayed normally )
Enter the corresponding information , Then choose our test database , Click on 【 test 】
If successful, this interface will be displayed 
3. Experimental code
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
#include<sqltypes.h>
#include<tchar.h>
#define SNO_LEN 30
#define NAME_LEN 50
#define DEPART_LEN 100
#define SSEX_LEN 5
int main() {
/*Step 1 Define handles and variables */
/* With king The beginning indicates connection KingbaseES The variable of */
/* With server The beginning indicates connection MySQL The variable of */
SQLHENV kinghenv, serverhenv; /* Environment handle */
SQLHDBC kinghdbc, serverhdbc; /* Connection handle */
SQLHSTMT kinghstmt, serverhstmt; /* Statement Handle */
SQLRETURN ret;
SQLCHAR sName[NAME_LEN] = { 0 }, sDepart[DEPART_LEN] = { 0 },
sSex[SSEX_LEN] = { 0 }, sSno[SNO_LEN] = { 0 };
SQLINTEGER sAge = 0;
SQLLEN cbAge = 0, cbSno = SQL_NTS, cbSex = SQL_NTS,
cbName = SQL_NTS, cbDepart = SQL_NTS;
/*Step 2 Initialization environment */
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &kinghenv);
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &serverhenv);
ret = SQLSetEnvAttr(kinghenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,
0);
ret = SQLSetEnvAttr(serverhenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,
0);
/*Step 3 Establishing a connection */
ret = SQLAllocHandle(SQL_HANDLE_DBC, kinghenv, &kinghdbc);
ret = SQLAllocHandle(SQL_HANDLE_DBC, serverhenv, &serverhdbc);
ret = SQLConnect(kinghdbc, (SQLWCHAR*)_T("KingBaseES"), SQL_NTS,
(SQLWCHAR*)_T("system"), SQL_NTS, (SQLWCHAR*)_T("123"), SQL_NTS); // data source user name
password
if (!SQL_SUCCEEDED(ret)) /* An error value is returned when the connection fails */
{
printf(" Connect bu success !");
return -1;
}
else printf(" Successful connection !");
ret = SQLConnect(serverhdbc, (SQLWCHAR*)_T("db_lab7"), SQL_NTS,
(SQLWCHAR*)_T("root"), SQL_NTS, (SQLWCHAR*)_T("zhk200176"), SQL_NTS);
if (!SQL_SUCCEEDED(ret)) /* An error value is returned when the connection fails */
{
printf(" Connect bu success !");
return -1;
}
else printf(" Successful connection !");
/*STEP 4 Initialize statement handle */
ret = SQLAllocHandle(SQL_HANDLE_STMT, kinghdbc, &kinghstmt);
ret = SQLSetStmtAttr(kinghstmt, SQL_ATTR_ROW_BIND_TYPE,
(SQLPOINTER)SQL_BIND_BY_COLUMN, SQL_IS_INTEGER);
ret = SQLAllocHandle(SQL_HANDLE_STMT, serverhdbc, &serverhstmt);
/*STEP 5 There are two ways to execute statements */
/* Precompiled statements with parameters */
SQLWCHAR sql[] = _T("INSERT INTO student(sno,sname,ssex,sage,sdept)
VALUES(?,?,?,?,?)");
ret = SQLPrepare(serverhstmt, sql, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLBindParameter(serverhstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, SNO_LEN, 0, sSno, 0, &cbSno);
ret = SQLBindParameter(serverhstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, NAME_LEN, 0, sName, 0, &cbName);
ret = SQLBindParameter(serverhstmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, SSEX_LEN, 0, sSex, 0, &cbSex);
ret = SQLBindParameter(serverhstmt, 4, SQL_PARAM_INPUT, SQL_C_LONG,
SQL_INTEGER, 0, 0, &sAge, 0, &cbAge);
ret = SQLBindParameter(serverhstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, DEPART_LEN, 0, sDepart, 0, &cbDepart);
}
/* perform SQL sentence */
ret = SQLExecDirect(kinghstmt, (SQLWCHAR*)_T("SELECT * FROM student"),
SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLBindCol(kinghstmt, 1, SQL_C_CHAR, sSno, SNO_LEN, &cbSno);
ret = SQLBindCol(kinghstmt, 2, SQL_C_CHAR, sName, NAME_LEN, &cbName);
ret = SQLBindCol(kinghstmt, 3, SQL_C_CHAR, sSex, SSEX_LEN, &cbSex);
ret = SQLBindCol(kinghstmt, 4, SQL_C_LONG, &sAge, 0, &cbAge);
ret = SQLBindCol(kinghstmt, 5, SQL_C_CHAR, sDepart, DEPART_LEN,
&cbDepart);
}
/*Step 6 Process the result set and execute the precompiled statements */
while ((ret = SQLFetch(kinghstmt)) != SQL_NO_DATA_FOUND)
{
if (ret == SQL_ERROR) printf("Fetch error\n");
else ret = SQLExecute(serverhstmt);
}
/*Step 7 Abort processing */
SQLFreeHandle(SQL_HANDLE_STMT, kinghstmt);
SQLDisconnect(kinghdbc);
SQLFreeHandle(SQL_HANDLE_DBC, kinghdbc);
SQLFreeHandle(SQL_HANDLE_ENV, kinghenv);
SQLFreeHandle(SQL_HANDLE_STMT, serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHandle(SQL_HANDLE_DBC, serverhdbc);
SQLFreeHandle(SQL_HANDLE_ENV, serverhenv);
return 0;
}
边栏推荐
- Simple record of fire & spell effects
- Jmeter- (III) create user test cases for interface testing
- Is it safe to open an account with qiniu securities?
- MySQL is slow to add indexes_ Why is your SQL so slow? Why is your MySQL index invalid?
- QT method of exiting application (exe)
- Alicloud disk mounted locally
- 电脑手写板怎么才能连接电脑使用
- Modprobe: fatal: module kvmgt not found, kvmgt has no module, kvmgt has no driver, gvt-g precautions, gvt-g precautions for starting win10 in UEFI mode
- 智云健康上市在即:长期亏损,美年健康俞熔已退出,未来难言乐观
- Canoe learning notes (1)
猜你喜欢
随机推荐
HNU数据库系统概论 ODBC
js禁用浏览器 pdf 打印、下载功能(pdf.js 禁用打印下载、功能)
Website judges network connection disconnection, JS judges network connection disconnection, best practice
Processing of limit operator in Presto
04 disk space management
数字图像处理知识点总结概述
“No bean named ‘UserController‘ available“
Bear market guide | some essential lessons and specific survival rules
OSI notes sorting
org. apache. ibatis. exceptions. PersistenceException:
Win11录屏数据保存在哪里?Win11录屏数据保存的位置
Rounding related calculation
Invalid bound statement (not found): com. qf. mapper. PassengerMapper. findByPassengerId
Pat 1073 scientific notation (20 points) (18 points not finished)
How testers write functional test cases
Understand two major web development patterns
PHP compressed file
Beginner to embedded development
Circular structure and circular keywords
On dynamic programming









