728x90
728x170

SQLITE 를 C++ 에서 사용하는 방법을 설명합니다.

우선 아래 사이트에서 SQLITE 최신버전(dll,source)을 다운 받습니다.

http://sqlite.org/download.html

글쓰는 시간 기준 최신 파일은 첨부합니다.

sqlite-amalgamation-3220000.zip
다운로드
sqlite-dll-win64-x64-3220000.zip
다운로드

 

위 두파일압축을 풀어

sqlite-amalgamation 쪽에 sqlite3.h 파일을 가져와 특정 폴더나 프로젝트 폴더에 갖다 놓습니다.

sqlite-dll 쪽에 sqlite3.def, sqlite3.dll 이 두파일이 있는데 이 두파일을 가지고 lib 파일을 만들어야합니다.

Developer Command Prompt for VS 2017 를 실행합니다.

(이름으로 검색하시거나 시작 메뉴에서 Visual Stuio 2017 의 하위 메뉴에 있습니다.)

위 두 파일이 있는 폴더로 이동하여 아래처럼 명령어를 실행하면 lib 파일이 만들어 집니다.

lib /def:sqlite3.def /machine:x64

아래는 커멘드 창의 내용입니다.

**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.5.7
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************

C:\Users\kjun\source>cd D:\[00]Window\Desktop\sqlitec++\sqlite

C:\Users\kjun\source>d:

D:\[00]Window\Desktop\sqlitec++\sqlite>lib /def:sqlite3.def /machine:x64
Microsoft (R) Library Manager Version 14.12.25835.0
Copyright (C) Microsoft Corporation.  All rights reserved.

   sqlite3.lib 라이브러리 및 sqlite3.exp 개체를 생성하고 있습니다.

D:\[00]Window\Desktop\sqlitec++\sqlite>

lib 파일을 sqlite.h 있는 쪽에(특정폴더나 프로젝트 폴더) 복사/이동 합니다.

아래 위 작업이 완료된 파일을 첨부합니다.

sqlite.zip
다운로드


Visual Studio 를 실행하고

C++ 프로젝트를 만들어 아래처럼 sqlite3.h 파일을 추가하고 sqlite3.dll, sqlite3.lib 을 추가합니다.

cpp 파일을 하나 추가해 아래 내용을 넣고 코딩을 시작하면 됩니다.

#pragma comment(lib,"sqlite3.lib")
#include <sqlite3.h>

아래는 sqlite db 에 테이블을 만들고 데이터를 insert 하는 코드입니다.

코드는 https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm 여기를 참고 했습니다.

#pragma comment(lib,"sqlite3.lib")
#include "sqlite3.h"
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
 int i;
 for (i = 0; i<argc; i++) {
  printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
 }
 printf("\n");
 return 0;
}

int main(int argc, char* argv[]) {
 sqlite3 *db;
 char *zErrMsg = 0;
 int rc;
 const char *sql;

 /* Open database */
 rc = sqlite3_open("test.db", &db);

 if (rc) {
  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  return(0);
 }
 else {
  fprintf(stderr, "Opened database successfully\n");
 }

 /* Create SQL statement */
 sql = "CREATE TABLE COMPANY("  \
  "ID INT PRIMARY KEY     NOT NULL," \
  "NAME           TEXT    NOT NULL," \
  "AGE            INT     NOT NULL," \
  "ADDRESS        CHAR(50)," \
  "SALARY         REAL );";

 /* Execute SQL statement */
 rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

 // PRAGMA
 // synchronous = OFF journal_mode=OFF TEMP_store = memory cash_SIZE + !))) PAGE_SIZE + @)$* COUNT_changes=OFF case_SENSITIVE_LIKE+Ffalse
 sqlite3_exec(db, "synchronous = OFF", NULL, NULL, NULL);
 sqlite3_exec(db, "journal_mode = OFF", NULL, NULL, NULL);
 sqlite3_exec(db, "temp_store = memory", NULL, NULL, NULL);
 sqlite3_exec(db, "cash_size = 1000", NULL, NULL, NULL);
 sqlite3_exec(db, "count_changes = OFF", NULL, NULL, NULL);
 sqlite3_exec(db, "case_sensitive_like = false", NULL, NULL, NULL);

 clock_t t = clock();

 // 'db' is the pointer you got from sqlite3_open*
 sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL);
 
 for (int i = 0; i < 10; i++)
 {
  char query[700];
  const char *insertsql;

  insertsql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
   "VALUES (%d, 'Paul', 32, 'California', 20000.00 ); ";

  sprintf_s(query, insertsql, i);

  /* Execute SQL statement */
  rc = sqlite3_exec(db, query, callback, 0, &zErrMsg);
 }
 
 // Any (modifying) SQL commands executed here are not committed until at the you call:
 rc = sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);

 t = clock() - t;

 printf("\n Time Esc :: %f ", ((float)t) / CLOCKS_PER_SEC);

 if (rc != SQLITE_OK) {
  fprintf(stderr, "SQL error: %s\n", zErrMsg);
  sqlite3_free(zErrMsg);
 }
 else {
  fprintf(stdout, "Records created successfully\n");
 }
 sqlite3_close(db);
 return 0;
}

 

전체 프로젝트도 소스는 아래 GitHub 에 있습니다.

https://github.com/kjundev/SQLiteTest

728x90
그리드형

'C++' 카테고리의 다른 글

[C++][펌] FILE MAPPING : Memory Mapped File (MMF)  (0) 2021.08.12
[C++] Visual Studio 2019 MFC 설치하기  (0) 2021.08.03
Posted by kjun
,