C 언어 sqlite 3 의 생 성,찾기,추가 사용

컴 파일 에 연결 라 이브 러 리-lsqlite 3
#include 
#include 
#include 
#include 

#define DB_NAME "zb.db"
#define TRUE        1
#define FALSE       0


sqlite3 *pdb = NULL;
char *Errmsg = NULL;

//      zb.db
int open_db()
{
	int rc;
    rc = sqlite3_open(DB_NAME , &pdb);
    if( rc != SQLITE_OK)
    {
        printf("can't open database:%s
",sqlite3_errmsg(pdb)); sqlite3_close(pdb) ; exit(1); } return 0; } // student int creat_table() { int rc; char *strsql = "create table if not exists student(id integer primary key, name text, age integer);"; //if not exists student , ( , )。 rc = sqlite3_exec(pdb , strsql , 0 , 0 ,&Errmsg); if(rc != 0) { fprintf(stderr , "can't open database: %s
",sqlite3_errmsg(pdb)); sqlite3_close(pdb); exit(1); } return 0; } /* */ int insert_data() { int flag = TRUE; char buff[1024]; char *strsql; int rc = 0 ; char temp[3][20]; strsql = buff; if(open_db != 0)// { open_db(); }//open_db(); creat_table(); strcpy(strsql, "insert into student"); strcat(strsql , " values(? , ? , ?);"); sqlite3_stmt *stmt = NULL; rc = sqlite3_prepare_v2(pdb , strsql , strlen(strsql) , &stmt , NULL); if(rc != SQLITE_OK) { if(stmt) { sqlite3_finalize(stmt); } sqlite3_close(pdb); return -1; } while(flag) { memset(temp,0,sizeof(temp)); printf(" :"); scanf("%s", temp[0]); printf(" :"); scanf("%s", temp[1]); printf(" :"); scanf("%s", temp[2]); sqlite3_bind_text( stmt , 1 , temp[0] , strlen(temp[0]), NULL); sqlite3_bind_text( stmt , 2 , temp[1] , strlen(temp[1]), NULL); sqlite3_bind_text( stmt , 3 , temp[2] , strlen(temp[2]), NULL); /* SQL ( ), : int sqlite3_step(sqlite3_stmt*); SQL ,sqlite3_step() SQLITE_ROW , SQL SQLITE_DONE , . SQLITE_BUSY . SQLITE_ROW. */ if(sqlite3_step(stmt) != SQLITE_DONE) { sqlite3_finalize(stmt); sqlite3_close(pdb); printf(" faile
" ); return 0; } sqlite3_reset(stmt); printf("Do you want to do insert ?(0 break, 1 insert):
"); scanf("%d", &flag); } sqlite3_finalize(stmt); printf("insert success!
"); sqlite3_close(pdb); return 0; } /* */ int search_data() { int rc; if(open_db != 0) { open_db(); } char *strsql = "select * from student" ; sqlite3_stmt *stmt = NULL; rc = sqlite3_prepare_v2(pdb , strsql , strlen(strsql) , &stmt , NULL); if(rc != SQLITE_OK) { if(stmt) { sqlite3_finalize(stmt); } sqlite3_close(pdb); return -1; } /* sqlite3_column_count() . sqlite3_column_count() sqlite3_prepare() . sqlite3_data_count() sqlite3_step() , sqlite3_column_count() . sqlite3_step() SQLITE_DONE , sqlite3_data_count() 0 , sqlite3_column_count() . sqlite3_column_type() N . : #define SQLITE_INTEGER 1 #define SQLITE_FLOAT 2 #define SQLITE_TEXT 3 #define SQLITE_BLOB 4 #define SQLITE_NULL 5 sqlite3_setp() sqlite3_prepare 。 , 。 ( ) , sqlite3_setp()。 sqlite3_setp() 。 ( :INSERT,UPDATE, DELETE),sqlite3_step() 。 */ int nColumn = sqlite3_column_count(stmt); //ncolumn int vtype , i; do{ rc = sqlite3_step(stmt); if(rc == SQLITE_ROW) { for(i = 0 ; i < nColumn ; i++ ) // { vtype = sqlite3_column_type(stmt , i); //sqlite3_column_type() if(vtype == SQLITE_INTEGER) { printf("%s:%d
" , sqlite3_column_name(stmt , i) , sqlite3_column_int(stmt , i)); } else if(vtype == SQLITE_TEXT) { printf("%s:%s
" , sqlite3_column_name(stmt , i) , sqlite3_column_text(stmt , i)); } else if(vtype == SQLITE_NULL) { printf("no values
"); } } printf("---------------
"); }else if(rc == SQLITE_DONE) { //printf("Select finish
"); printf("
"); break; }else { printf("Select faile
"); sqlite3_finalize(stmt); break; } }while(1); sqlite3_finalize(stmt); sqlite3_close(pdb); return 0; } int main(int argc , char **argv) { int n; printf("1. 2. 3. 4.
"); printf("choose[1 - 4]:"); scanf("%d" , &n); getchar(); switch(n) { case 1: insert_data(); // printf("
"); break; case 2: printf("
"); break; case 3: printf("
"); break; case 4: search_data(); break; default : printf("error
"); } return 0; }

좋은 웹페이지 즐겨찾기