Using SQLite in a Native Visual C++ Application

Using SQLite in a Native Visual C++ Application

As promised, here is the Native Visual C++ followup to my previous blog post.  I hope you find this helpful!

  1. Create a new C++ Win32 Console application.
  2. Download the native SQLite DLL from: http://sqlite.org/sqlite-dll-win32-x86-3070400.zip
  3. Unzip the DLL and DEF files and place the contents in your project’s source folder (an easy way to find this is to right click on the tab and click the “Open Containing Folder” menu item.
  4. Open a “Visual Studio Command Prompt (2010)” and navigate to your source folder.
  5. Create an import library using the following command line: LIB /DEF:sqlite3.def
  6. Add the library (i.e. sqlite3.lib) to your Project Properties -> Configuration Properties -> Linker -> Additional Dependencies.
  7. Download http://sqlite.org/sqlite-amalgamation-3070400.zip
  8. Unzip the sqlite3.h header file and place into your source directory.
  9. Include the the sqlite3.h header file in your source code.
  10. You will need to include the sqlite3.dll in the same directory as your program (or in a System Folder).
  11. Alternatively you can add the sqlite3.c file in your project,  and the SQLite database engine will be included in your exe.  After you add sqlite3.c to your project (right click the project and choose to add an existing file), you will need to right click on the sqlite3.c file in the Solution Explorer and change: Properties->Configuration Properties->C/C++->Precompiled Headers->Precompiled Header = Not Using Precompiled Headers

Here is some example code to get you started:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111     
#include "stdafx.h"
#include <ios>
#include <iostream>
#include "sqlite3.h"
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
   int rc;
   char *error;
 
   // Open Database
   cout << "Opening MyDb.db ..." << endl;
   sqlite3 *db;
   rc = sqlite3_open("MyDb.db", &db);
   if (rc)
   {
      cerr << "Error opening SQLite3 database: " << sqlite3_errmsg(db) << endl << endl;
      sqlite3_close(db);
      return 1;
   }
   else
   {
      cout << "Opened MyDb.db." << endl << endl;
   }
 
   // Execute SQL
   cout << "Creating MyTable ..." << endl;
   const char *sqlCreateTable = "CREATE TABLE MyTable (id INTEGER PRIMARY KEY, value STRING);";
   rc = sqlite3_exec(db, sqlCreateTable, NULL, NULL, &error);
   if (rc)
   {
      cerr << "Error executing SQLite3 statement: " << sqlite3_errmsg(db) << endl << endl;
      sqlite3_free(error);
   }
   else
   {
      cout << "Created MyTable." << endl << endl;
   }
 
   // Execute SQL
   cout << "Inserting a value into MyTable ..." << endl;
   const char *sqlInsert = "INSERT INTO MyTable VALUES(NULL, 'A Value');";
   rc = sqlite3_exec(db, sqlInsert, NULL, NULL, &error);
   if (rc)
   {
      cerr << "Error executing SQLite3 statement: " << sqlite3_errmsg(db) << endl << endl;
      sqlite3_free(error);
   }
   else
   {
      cout << "Inserted a value into MyTable." << endl << endl;
   }
 
   // Display MyTable
   cout << "Retrieving values in MyTable ..." << endl;
   const char *sqlSelect = "SELECT * FROM MyTable;";
   char **results = NULL;
   int rows, columns;
   sqlite3_get_table(db, sqlSelect, &results, &rows, &columns, &error);
   if (rc)
   {
      cerr << "Error executing SQLite3 query: " << sqlite3_errmsg(db) << endl << endl;
      sqlite3_free(error);
   }
   else
   {
      // Display Table
      for (int rowCtr = 0; rowCtr <= rows; ++rowCtr)
      {
         for (int colCtr = 0; colCtr < columns; ++colCtr)
         {
            // Determine Cell Position
            int cellPosition = (rowCtr * columns) + colCtr;
 
            // Display Cell Value
            cout.width(12);
            cout.setf(ios::left);
            cout << results[cellPosition] << " ";
         }
 
         // End Line
         cout << endl;
 
         // Display Separator For Header
         if (0 == rowCtr)
         {
            for (int colCtr = 0; colCtr < columns; ++colCtr)
            {
               cout.width(12);
               cout.setf(ios::left);
               cout << "~~~~~~~~~~~~ ";
            }
            cout << endl;
         }
      }
   }
   sqlite3_free_table(results);
 
   // Close Database
   cout << "Closing MyDb.db ..." << endl;
   sqlite3_close(db);
   cout << "Closed MyDb.db" << endl << endl;
 
   // Wait For User To Close Program
   cout << "Please press any key to exit the program ..." << endl;
   cin.get();
 
   return 0;
}

[출처] https://dcravey.wordpress.com/2011/03/21/using-sqlite-in-a-visual-c-application/

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
22 SQLite on Visual Studio with NuGet and Easy Instructions file 졸리운_곰 2016.08.11 329
» Using SQLite in a Native Visual C++ Application 졸리운_곰 2016.08.11 276
20 How to: Ionic in Visual Studio 졸리운_곰 2016.05.09 594
19 Get started with Ionic apps in Visual Studio : 비주얼 스투이오로 모바일 앱 생성 ionic 프레임워크 졸리운_곰 2016.05.09 329
18 Build cross-platform apps with Xamarin in Visual Studio 졸리운_곰 2015.08.16 319
17 Visual C++를 사용하여 플랫폼 간 앱 제작 file 졸리운_곰 2015.08.16 645
16 Hooking 이란? file 졸리운_곰 2015.06.16 525
15 윈도우즈 후킹 기초 file 졸리운_곰 2015.06.16 318
14 윈도우 메시지 후킹 file 졸리운_곰 2015.06.16 2284
13 C# Obfuscation 난독화하기 file 졸리운_곰 2015.05.31 686
12 [학습정리] 민대규님의 학습정리 프로그램 "대굴이의 학습정리" file 졸리운_곰 2015.05.11 445
11 [일정관리] 민대규님 제작 "대굴이의 일정관리" file 졸리운_곰 2015.05.11 1841
10 webdav c++ lib 라이브러러 file 졸리운_곰 2015.05.11 395
9 Use Visual Studio C++ 6.0 on Windows 8.1 64bit : 윈도우 8.1에서 비주얼 스투디오 6.0 사용하기 졸리운_곰 2015.05.09 632
8 Visual Studio 2013을 사용하여 앱 개발 file 졸리운_곰 2014.05.30 842
7 C++로 작성한 Windows 스토어 앱용 로드맵 file 졸리운_곰 2014.05.30 570
6 블로그 뷰어 앱 만들기(C++) file 졸리운_곰 2014.05.30 3956
5 C++ Windows 스토어 앱에서 파일 선택기 사용(자습서 4/4) file 졸리운_곰 2014.05.30 762
4 C++ Windows 스토어 앱에서 탐색 및 보기 추가(자습서 3/4) file 졸리운_곰 2014.05.30 594
3 C++ Windows 스토어 앱의 수명 주기 및 상태 관리(자습서 2/4) file 졸리운_곰 2014.05.30 496
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED