sqlite3 C简单编程实例

来源:互联网 发布:单片机好学吗 编辑:程序博客网 时间:2024/05/17 07:22

#include <stdio.h>
#include <string.h>
#include "sqlite3.h"
#define _WIN32_

#if defined _WIN32_
#include <windows.h>
LARGE_INTEGER start,end,hz; 
#else
#include <sys/time.h>
timeval *tv=NULL;
timezone *tz=NULL;

double start=0.0;
double end=0.0;
#endif

sqlite3 *db=NULL;
char *errmsg=NULL;
char **strTable;
FILE *pFile;

void error(const char *error)
{
// fprintf(stderr,"%s",error);
   printf("%s\n",error);
}
void create_database(char *name)
{
     int handle;
     handle=sqlite3_open(name,&db);
     if(handle)
     {
        error(sqlite3_errmsg(db));
        exit(0);
     }
     else
     printf("create database:%s successfully!\n",name);
}
void optimize_database(char *name)
{
    sqlite3_exec(db,"PRAGMA synchronous = OFF",0,0,&errmsg);//如果有定期备份的机制,而且少量数据丢失可接受,用OFF
    sqlite3_exec(db,"PRAGMA page_size = 4096",0,0,&errmsg);//只有在未创建数据库时才能设置
    sqlite3_exec(db,"PRAGMA cache_size = 8000",0,0,&errmsg); //建议改为8000
    sqlite3_exec(db,"PRAGMA case_sensitive_like=1",0,0,&errmsg);//搜索中文字串

    if(NULL != errmsg)
      error(errmsg);
}
void create_table(const char *table)
{
      char create[128]="create table ";
      char *section="(name varchar(20) primary key,age integer,sex bool);";
      strcat(create,table);
      strcat(create,section);
      printf("%s\n",create);
      sqlite3_exec(db,create,0,0,&errmsg);
   // printf("%s",errmsg);
      if(NULL != errmsg)
      error(errmsg);
}
void insert_into_table(char *table,char *record)
{
     char insert[128]="insert into \"";
     char *section=" values(";
     char *semi=");";
     strcat(insert,table);
     strcat(insert,"\"");
     strcat(insert,section);
     strcat(insert,record);
     strcat(insert,semi);
     sqlite3_exec(db,insert,0,0,&errmsg);
//     printf("%s\n",insert);
     if(NULL!=errmsg)
     error(errmsg);
}
void print_table(char *table)
{
   char sql[64]="SELECT * FROM ";
   char *semi=";";
   strcat(sql,table);
   strcat(sql,semi);

   int row=0;
   int column=0;
   
   sqlite3_get_table(db,sql,&strTable,&row,&column,&errmsg);
   if(NULL!=errmsg)
    error(errmsg);
   printf("row=%d,column=%d\n",row,column);
   int i=0;
   for(i=0;i<(row+1)*column;i++) 
   { 
      printf("%-15s",strTable[i]);
      if(0==(i+1)%3)
      printf("\n");
   } 
}
void delete_record(char *table,char *expression)
{
   char delete[64]="delete from ";
   char where[32]=" where ";
   strcat(delete,table);
   strcat(where,expression);
   strcat(where,";");
   if(NULL!=expression)
   strcat(delete,where);
   sqlite3_exec(db,delete,0,0,&errmsg);
   if(NULL!=errmsg)
   error(errmsg);
}
void close_database(char *tablename)
{
   sqlite3_free_table(strTable);
   sqlite3_close(db); 
}
void begin_transaction(void)
{
   sqlite3_exec(db,"begin;",0,0,&errmsg);
   if(NULL!=errmsg)
   error(errmsg);
}
void commit_transaction(void)
{
   sqlite3_exec(db,"commit;",0,0,&errmsg);
   sqlite3_exec(db,"rollback;",0,0,&errmsg);
   if(NULL!=errmsg)
   error(errmsg);
}
void begin_watch(void)
{
#if defined _WIN32_
     //start=GetTickCount();
     QueryPerformanceFrequency(&hz); 
     QueryPerformanceCounter(&start); 
#else
     gettimeofday(tv,tz);
     start=tv->tv_sec*1000+(double)tv->tv_usec/1000000;
#endif 
}
void stop_watch(int type)
{
     char *prefix=(char *)malloc(64);
#if defined _WIN32_
   //end=GetTickCount();
   QueryPerformanceCounter(&end); 
#else
   gettimeofday(tv,tz);
   end=tv->tv_sec*1000+(double)tv->tv_usec/1000000;
#endif

    switch(type)
    {
    case 1:
       prefix="create databse";
       break;
    case 2:
       prefix="create table";
       break;
    default:
       sprintf(prefix,"insert %d records",type);
       break;
    }
#if defined _WIN32_    
    fprintf(pFile,"%-25s 耗时:%lfms\n",prefix,((float)(end.QuadPart-start.QuadPart)/hz.QuadPart)*1000);
    if(2<type)
    fprintf(pFile,"\n\n"); 
    fflush(pFile);
    printf("耗时:%lfms\n",((float)(end.QuadPart-start.QuadPart)/hz.QuadPart)*1000);

#else

    fprintf(pFile,"%-25s 耗时:%lfms\n",prefix,end-start);
    if(2<type)
    fprintf(pFile,"\n\n"); 
    fflush(pFile);
     printf("耗时:%fms\n",end-start);

#endif
}


void main()
{

   int i=0;
   float start=0,end=0;
   char database[64];
   char tablename[64];
   int count=0;
   
    pFile=fopen("log.txt","w+");
    if(!pFile)
    {
        exit(0);
    }
while(1)
{

     printf("input the databse name:");
     scanf("%s",&database);
     begin_watch();
     create_database(database);
     stop_watch(1);
     
     optimize_database(database);
     
     printf("input the table name:");
     scanf("%s",&tablename);
     begin_watch();
   create_table(tablename);
   stop_watch(2);
   
   printf("input the record numbers:");
     scanf("%d",&count);
   begin_watch();
   begin_transaction();
   for(i=0;i<count;i++)
       insert_into_table(tablename,"NULL,23,1");
   commit_transaction();
     stop_watch(count);
     
//   print_table("student");
   //delete_record("student","age=24");
     //print_table("student");
     close_database("student");
   }
   fcolse(pFile);
}

0 0
原创粉丝点击