// odbctest.c
// VC++ 2010 Express
// マルチバイト文字セット
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <sqlext.h>
#include <odbcinst.h>
#include <stdio.h>
#include <io.h>
#define DRIVER "Microsoft Access Driver (*.mdb)"
#define DATABASE "C:\\projects\\vc++\\odbctest\\odbctest.mdb"
int main()
{
HENV henv;
HDBC hdbc;
HSTMT hstmt;
SQLCHAR CompliteConnect[255];
SWORD len;
SQLCHAR value[256];
RETCODE rc;
BOOL b;
char c;
int i;
// データベースの作成
if (_access(DATABASE, 0) == 0) {
printf("remove %s (y)", DATABASE
); if (c != 'y') return 1;
}
b = SQLConfigDataSource(NULL, ODBC_ADD_DSN, DRIVER,
"create_db="DATABASE" general\0");
// データベースへの接続
rc = SQLAllocEnv(&henv);
rc = SQLAllocConnect(henv, &hdbc);
rc = SQLDriverConnect(hdbc, NULL,
(SQLCHAR *)"Driver={"DRIVER"}; DBQ="DATABASE,
SQL_NTS, CompliteConnect, _countof(CompliteConnect), &len, SQL_DRIVER_NOPROMPT);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr
, "データベースファイルを開けません\n"); return 1;
}
// テーブルの作成
rc = SQLAllocStmt(hdbc, &hstmt);
rc = SQLExecDirect(hstmt, (SQLCHAR *)
"create table 郵便番号データ ("
"郵便番号 text(7), "
"都道府県名 text, "
"市区町村名 text, "
"町域名 text, "
"町域分割 short, "
"小字起番 short, "
"丁目町域 short, "
"複数町域 short, "
"更新表示 short, "
"変更理由 short)"
, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr
, "テーブルの作成に失敗しました\n"); return 1;
}
// レコードの追加
rc = SQLExecDirect(hstmt, (SQLCHAR *)
"insert into 郵便番号データ values ("
"'5850054','大阪府','南河内郡千早赤阪村','吉年',0,0,0,0,0,0"
")", SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr
, "レコードの追加に失敗しました\n"); return 1;
}
// 検索
rc = SQLExecDirect(hstmt, (SQLCHAR *)"select * from 郵便番号データ", SQL_NTS);
while (1) {
rc = SQLFetch(hstmt);
if (rc == SQL_NO_DATA) break;
if (rc == SQL_ERROR) break;
for (i = 1; i <= 4; i++) {
SQLGetData
(hstmt
, i
, SQL_C_CHAR
, value
, _countof
(value
), &strlen); }
}
rc = SQLFreeStmt(hstmt, SQL_DROP);
rc = SQLDisconnect(hdbc);
rc = SQLFreeConnect(hdbc);
rc = SQLFreeEnv(henv);
return 0;
}
Ly8gb2RiY3Rlc3QuYwovLyBWQysrIDIwMTAgRXhwcmVzcwovLyDjg57jg6vjg4Hjg5DjgqTjg4jmloflrZfjgrvjg4Pjg4gKCiNkZWZpbmUgX0NSVF9TRUNVUkVfTk9fV0FSTklOR1MKCiNpbmNsdWRlIDxXaW5kb3dzLmg+CiNpbmNsdWRlIDxzcWxleHQuaD4KI2luY2x1ZGUgPG9kYmNpbnN0Lmg+CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8aW8uaD4KCiNkZWZpbmUgRFJJVkVSICJNaWNyb3NvZnQgQWNjZXNzIERyaXZlciAoKi5tZGIpIgojZGVmaW5lIERBVEFCQVNFICJDOlxccHJvamVjdHNcXHZjKytcXG9kYmN0ZXN0XFxvZGJjdGVzdC5tZGIiCgppbnQgbWFpbigpCnsKCUhFTlYgaGVudjsKCUhEQkMgaGRiYzsKCUhTVE1UIGhzdG10OwoJU1FMQ0hBUiBDb21wbGl0ZUNvbm5lY3RbMjU1XTsKCVNXT1JEIGxlbjsKCVNRTENIQVIgdmFsdWVbMjU2XTsKCVNRTElOVEVHRVIgc3RybGVuOwoJUkVUQ09ERSByYzsKCUJPT0wgYjsKCWNoYXIgYzsKCWludCBpOwoKCS8vIOODh+ODvOOCv+ODmeODvOOCueOBruS9nOaIkAoJaWYgKF9hY2Nlc3MoREFUQUJBU0UsIDApID09IDApIHsKCQlwcmludGYoInJlb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