int main() {
  sqlite3_open("./countries.db", &db);

  if(db == 0) {
    printf("Could not open database.");
    return 1;
  }

  sql_stmt("create table countries (country_name, size_sq_miles, population)");

  insert_countries();

  printf("\n\nSelecting the countries with the largest population:\n\n");
  select_stmt("select * from countries order by population desc limit 5");

  printf("\n\nSelecting the countries with the smallest population:\n\n");
  select_stmt("select * from countries order by population  asc limit 5");

  printf("\n\nSelecting the countries with the largest area:\n\n");
  select_stmt("select * from countries order by size_sq_miles desc limit 5");

  printf("\n\nSelecting the countries with the smallest area:\n\n");
  select_stmt("select * from countries order by size_sq_miles  asc limit 5");

  sqlite3_close(db);
  return 0;
}
Exemple #2
0
void Parser::view_stmt(string id) {
    if (tokens[look].type == SELECT) {
        select_stmt(id);
    } else {
        extract_stmt(id);
    }
}
Exemple #3
0
int main() {
  sqlite3_open("./bind_insert.db", &db);

  if(db == 0) {
    printf("\nCould not open database.");
    return 1;
  }

  sql_stmt("create table foo (bar, baz)");

#if 0
  sqlite3_stmt *stmt;

  if ( sqlite3_prepare(
         db, 
         "insert into foo values (?,?)",  // stmt
        -1, // If than zero, then stmt is read up to the first nul terminator
        &stmt,
         0  // Pointer to unused portion of stmt
       )
       != SQLITE_OK) {
    printf("\nCould not prepare statement.");
    return 1;
  }

  printf("\nThe statement has %d wildcards\n", sqlite3_bind_parameter_count(stmt));

  if (sqlite3_bind_double(
        stmt,
        1,  // Index of wildcard
        4.2
        )
      != SQLITE_OK) {
    printf("\nCould not bind double.\n");
    return 1;
  }

  if (sqlite3_bind_int(
        stmt,
        2,  // Index of wildcard
        42
        )
      != SQLITE_OK) {
    printf("\nCould not bind int.\n");
    return 1;
  }

  if (sqlite3_step(stmt) != SQLITE_DONE) {
    printf("\nCould not step (execute) stmt.\n");
    return 1;
  }

  sqlite3_reset(stmt);

  if (sqlite3_bind_null(
        stmt,
        1  // Index of wildcard
        )
      != SQLITE_OK) {
    printf("\nCould not bind double.\n");
    return 1;
  }

  if (sqlite3_bind_text (
        stmt,
        2,  // Index of wildcard
        "hello",
        5,  // length of text
        SQLITE_STATIC
        )
      != SQLITE_OK) {
    printf("\nCould not bind int.\n");
    return 1;
  }

  if (sqlite3_step(stmt) != SQLITE_DONE) {
    printf("\nCould not step (execute) stmt.\n");
    return 1;
  }

  printf("\n");
  select_stmt("select * from foo");
#endif  
  printf("\n");
  select_bind();

  sqlite3_close(db);
  return 0;
}