본문 바로가기

DataBase

[SQLite] 리눅스에서 sqlite연동 후 컴파일하기

자세한 함수의 사용법은  http://www.sqlite.org/ 를 참조한다.

<기본작업>

※ 아래의 사항들은 sqlite연동시 필수로 작업해야한다.

1) 헤더파일 추가하기
   #include "sqlite3.h"

2) 연결 변수 만들기
   sqlite3 *db = NULL;  //open한 디비의 연결 변수
   sqlite3_stmt *stmt = NULL; //쿼리문 실행 시 필요한 변수

3) 연결 종료시
   sqlite3_finalize(stmt);

   sqlite3_close(db);



1. DB Open

if(sqlite3_open("파일경로/CSqliteTest.sqlite", &db) != SQLITE_OK)
{
  printf("fail to sqlite3_open()\n");
  return 0; 
}
else
{
   printf("success!\n");
}

2. SELECT 쿼리 실행

       char *str = "SELECT * FROM MainTbl"; //SELECT 쿼리문 작성
       
       //쿼리문을 실행시킨다. 

       if(sqlite3_prepare(db, str, strlen(str), &stmt, NULL) != SQLITE_OK)

       {

printf("fail to sqlite3_prepare()\n");

return 0;

}
       //쿼리문 결과
       while(sqlite3_step(stmt) == SQLITE_ROW)  //검색된 결과의 첫번째 레코드부터 마지막 레코드까지 이동한다.
       {
iVal = (int)sqlite3_column_int(stmp,0);  //int형 필드값
strVal = (char*)sqlite3_column_text(stmp,1);  //text형 필드값
printf("%d ----- %s\n", iVal, strVal);
}
 
3. INSERT 쿼리 실행 

※ DELETE, UPDATE의 경우 INSERT의 방법과 동일하게 사용할 수 있다.

       //sqlite3의 경우, 인자값을 '?'로 설정하면, 나중에 개발자 정의값으로 bind할 수 있다. 
       char *str = "INSERT INTO MainTbl(mt_msg) VALUES(?)";  
      

        if(sqlite3_prepare_v2(db, str, -1, &stmt, NULL) == SQLITE_OK)

{
                
 // '?'자리에 텍스트의 값을 bind할 경우 사용하는 함수. 두 번째 인자값은 '?'위치(1~)

sqlite3_bind_text(stmt, 1, "test", -1, SQLITE_TRANSIENT);
               
 /*

     // int형의 경우, sqlite3_bind_int(stmt, 1, i); 와 같이 사용하면 된다. //두 번째 인자값은 '?'의 위치(1~)
    */ 
     
printf("insert~~\n");

}


sqlite3_step(stmp); //SQLITE_DONE  

4. 컴파일 하기

※ 앞의 문장의 경우, 일반 gcc컴파일과 동일하지만, 빨간색으로 표시한 부분을 추가해야, 정상적으로 컴파일 된다. (-L"sqlite lib폴더위치" -lsqlite3)

gcc -g -o sqlitetest test.c -L/usr/local/sqlite/lib -lsqlite3

 

※ result codes
#define SQLITE_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR 1 /* SQL error or missing database */ #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ #define SQLITE_PERM 3 /* Access permission denied */ #define SQLITE_ABORT 4 /* Callback routine requested an abort */ #define SQLITE_BUSY 5 /* The database file is locked */ #define SQLITE_LOCKED 6 /* A table in the database is locked */ #define SQLITE_NOMEM 7 /* A malloc() failed */ #define SQLITE_READONLY 8 /* Attempt to write a readonly database */ #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ #define SQLITE_CORRUPT 11 /* The database disk image is malformed */ #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ #define SQLITE_FULL 13 /* Insertion failed because database is full */ #define SQLITE_CANTOPEN 14 /* Unable to open the database file */ #define SQLITE_PROTOCOL 15 /* Database lock protocol error */ #define SQLITE_EMPTY 16 /* Database is empty */ #define SQLITE_SCHEMA 17 /* The database schema changed */ #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ #define SQLITE_MISMATCH 20 /* Data type mismatch */ #define SQLITE_MISUSE 21 /* Library used incorrectly */ #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ #define SQLITE_AUTH 23 /* Authorization denied */ #define SQLITE_FORMAT 24 /* Auxiliary database format error */ #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ #define SQLITE_NOTADB 26 /* File opened that is not a database file */ #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
/* end-of-error-codes */