// create_db.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"
#define CSVFILE "osaka.csv"
int main()
{
HENV henv;
HDBC hdbc;
HSTMT hstmt;
SQLCHAR CompliteConnect[255];
SWORD len;
SQLCHAR sqlchar[256];
RETCODE rc;
BOOL b;
FILE *fp;
char line[1024];
char number[7+1];
char todofuken[20];
char sikutyoson[100];
char tyoiki[100];
char addr[200];
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"\0");
if (b == FALSE) {
fprintf(stderr
, "データベースの作成に失敗しました\n"); return 1;
}
// データベースへの接続
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)"
, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr
, "テーブルの作成に失敗しました\n"); return 1;
}
// レコードの追加
fp
= fopen(CSVFILE
, "r"); if (fp == NULL) {
fprintf(stderr
, "%sがありません\n", CSVFILE
); return 1;
}
for (;;) {
i
= fscanf(fp
, "%[^,],%[^,],%[^,],%[^,],%s\n", number, todofuken, sikutyoson, tyoiki, line);
if (i == EOF) break;
sprintf(addr
, "%s%s%s", todofuken
, sikutyoson
, tyoiki
); "insert into 郵便番号データ values ('%s','%s')",
number, addr);
rc = SQLExecDirect(hstmt, sqlchar, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
fprintf(stderr
, "レコードの追加に失敗しました\n"); return 1;
}
}
rc = SQLEndTran(SQL_HANDLE_ENV, henv, SQL_COMMIT);
// 検索
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 <= 2; i++) {
SQLGetData
(hstmt
, i
, SQL_C_CHAR
, sqlchar
, _countof
(sqlchar
), &strlen); }
}
rc = SQLFreeStmt(hstmt, SQL_DROP);
rc = SQLDisconnect(hdbc);
rc = SQLFreeConnect(hdbc);
rc = SQLFreeEnv(henv);
return 0;
}
Ly8gY3JlYXRlX2RiLmMKLy8gVkMrKyAyMDEwIEV4cHJlc3MKLy8g44Oe44Or44OB44OQ44Kk44OI5paH5a2X44K744OD44OICgojZGVmaW5lIF9DUlRfU0VDVVJFX05PX1dBUk5JTkdTCgojaW5jbHVkZSA8V2luZG93cy5oPgojaW5jbHVkZSA8c3FsZXh0Lmg+CiNpbmNsdWRlIDxvZGJjaW5zdC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPGlvLmg+CgojZGVmaW5lIERSSVZFUiAiTWljcm9zb2Z0IEFjY2VzcyBEcml2ZXIgKCoubWRiKSIKI2RlZmluZSBEQVRBQkFTRSAiQzpcXHByb2plY3RzXFx2YysrXFxvZGJjdGVzdFxcb2RiY3Rlc3QubWRiIgojZGVmaW5lIENTVkZJTEUgIm9zYWthLmNzdiIKCmludCBtYWluKCkKewoJSEVOViBoZW52OwoJSERCQyBoZGJjOwoJSFNUTVQgaHN0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