sqlite3 -- 查找指定名字中年龄最大的一行所有字段信息

#include 
#include 
#include 

static int callback(void *data, int argc, char **argv, char **azColName){
   int i;
   fprintf(stderr, "%s\n", (const char*)data);
   printf("argc: %d\n", argc);
   for (i = 0; i < argc; i++)
   {
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
      //printf("count = %s\n", argv[i]);

      if (!strcmp(azColName[i], "AGE"))
      {
        printf("cc %s = %s\n", azColName[i], argv[i]);
      }
   }

   //printf("count = %s\n", argv[0]);

   printf("\n");
   return 0;
}

int main(int argc, char* argv[])
{
   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char *sql;
   const char* data = "Callback function called";

   /* Open database */
   rc = sqlite3_open("test.db", &db);
   if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      fprintf(stderr, "Opened database successfully\n");
   }

#if 0
   // insert
   /* Create SQL statement */
   sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
         "VALUES (17, 'Allen', 0, 'California', 11110.00 );";

   /* Execute SQL statement */
   rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout, "Records created successfully\n");
   }
#endif
    // select
   /* Create SQL statement */
   //sql = "SELECT AGE,COUNT(*) from COMPANY where NAME='Allen'";
   sql = "SELECT * from COMPANY where (AGE IN (SELECT MAX(AGE) FROM COMPANY where NAME='Allen'))";

   /* Execute SQL statement */
   rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout, "Operation done successfully\n");
   }
   sqlite3_close(db);
   return 0;
} 

 

你可能感兴趣的:(数据库)