Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44544
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 106
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-11 11:04
文章分类

全部博文(17)

文章存档

2014年(17)

我的朋友

分类: LINUX

2014-11-21 14:56:20

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<sqlite3.h>
  3. #include<stdlib.h>

  4. int main(int argc, char** args)
  5. {
  6.     // Create an int variable for storing the return code for each call
  7.     int retval;
  8.     
  9.     // The number of queries to be handled,size of each query and pointer
  10.     int q_cnt = 5,q_size = 150,ind = 0;
  11.     char **queries = malloc(sizeof(char) * q_cnt * q_size);
  12.         
  13.     // A prepered statement for fetching tables
  14.     sqlite3_stmt *stmt;
  15.     
  16.     // Create a handle for database connection, create a pointer to sqlite3
  17.     sqlite3 *handle;
  18.     
  19.     // try to create the database. If it doesnt exist, it would be created
  20.     // pass a pointer to the pointer to sqlite3, in short sqlite3**
  21.     retval = sqlite3_open("sampledb.sqlite3",&handle);
  22.     // If connection failed, handle returns NULL
  23.     if(retval)
  24.     {
  25.         printf("Database connection failed\n");
  26.         return -1;
  27.     }
  28.     printf("Connection successful\n");
  29.     
  30.     // Create the SQL query for creating a table
  31.     char create_table[100] = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)";
  32.     
  33.     // Execute the query for creating the table
  34.     retval = sqlite3_exec(handle,create_table,0,0,0);
  35.     
  36.     // Insert first row and second row
  37.     queries[ind++] = "INSERT INTO users VALUES('manish','mani',1)";
  38.     retval = sqlite3_exec(handle,queries[ind-1],0,0,0);
  39.     queries[ind++] = "INSERT INTO users VALUES('mehul','pulsar',0)";
  40.     retval = sqlite3_exec(handle,queries[ind-1],0,0,0);
  41.     
  42.     // select those rows from the table
  43.     queries[ind++] = "SELECT * from users";
  44.     retval = sqlite3_prepare_v2(handle,queries[ind-1],-1,&stmt,0);
  45.     if(retval)
  46.     {
  47.         printf("Selecting data from DB Failed\n");
  48.         return -1;
  49.     }
  50.     
  51.     // Read the number of rows fetched
  52.     int cols = sqlite3_column_count(stmt);
  53.         
  54.     while(1)
  55.     {
  56.         // fetch a row's status
  57.         retval = sqlite3_step(stmt);
  58.         
  59.         if(retval == SQLITE_ROW)
  60.         {
  61.             // SQLITE_ROW means fetched a row
  62.             
  63.             // sqlite3_column_text returns a const void* , typecast it to const char*
  64.             for(int col=0 ; col<cols;col++)
  65.             {
  66.                 const char *val = (const char*)sqlite3_column_text(stmt,col);
  67.                 printf("%s = %s\t",sqlite3_column_name(stmt,col),val);
  68.             }
  69.             printf("\n");
  70.         }
  71.         else if(retval == SQLITE_DONE)
  72.         {
  73.             // All rows finished
  74.             printf("All rows fetched\n");
  75.             break;
  76.         }
  77.         else
  78.         {
  79.             // Some error encountered
  80.             printf("Some error encountered\n");
  81.             return -1;
  82.         }
  83.     }
  84.     
  85.     // Close the handle to free memory
  86.     sqlite3_close(handle);
  87.     return 0;
  88. Save the code in a file named, say dataman.c , compile it using the command
gcc dataman.c -o dataman -l sqlite3 –std=c99

阅读(920) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~