SQLite 3 C ++测验-比较用户对现有数据库的响应

时间:2019-04-01 09:05:12

标签: html c++ sqlite

我正在尝试使用SQLite编写C ++程序。这允许用户选择他们对旅馆房间的偏好。该程序将用户的答案与数据库中现有值的答案进行比较。

用户将在HTML + CSS网页的下拉菜单中进行选择,但是我不确定如何将用户的响应链接到C ++。我也很难找到任何可以帮助我解决我的情况的SQLite教程。我是否需要创建首先保存酒店价值的表格?

尝试使用sqlite3_open命令,也执行语句。但是,我尝试的所有操作都会导致错误消息,并且我感到很困惑。

#include <iostream>
#include <sqlite3.h> 

sqlite_open("HotelProject.db", &db);

sqlite3_exec(db,"CREATE TABLE hotel_tbl (name, location, rating)");
sqlite3_exec(db,"INSERT INTO hotel_tbl (name, location, rating)" \ 
    "VALUES ('x','y','z')");

sqlite3_exec(db, "SELECT * FROM hotel_tbl");
sqlite3_close(db);
return 0;

自从我上次使用C ++以来已经有一段时间了,而且还没有回到我的头上。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

将您的代码与此进行比较:

#include <iostream>
#include "sqlite3.h"

int main()
{
  sqlite3 *db;
  sqlite3_stmt *stmt;

  int rc = sqlite3_open("HotelProject.db", &db);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to open database.";
    return 1;
  }
  rc = sqlite3_exec(db,
                    "CREATE TABLE hotel_tbl (name, location, rating);",
                    NULL, NULL, NULL);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to create table.";
    sqlite3_close(db);
    return 1;
  }
  rc = sqlite3_prepare_v2(db, "INSERT INTO hotel_tbl VALUES (@x, @y, @z);",
                          -1, &stmt, NULL);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to compile query statement.";
    sqlite3_close(db);
    return 1;
  }
  rc = sqlite3_bind_text(stmt, 1, "x", -1, SQLITE_STATIC);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to bind paramter.";
    sqlite3_close(db);
    return 1;
  }
  rc = sqlite3_bind_text(stmt, 2, "y", -1, SQLITE_STATIC);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to bind paramter.";
    sqlite3_close(db);
    return 1;
  }
  rc = sqlite3_bind_text(stmt, 3, "z", -1, SQLITE_STATIC);
  if (rc != SQLITE_OK) {
    std::cout << "Failed to bind paramter.";
    sqlite3_close(db);
    return 1;
  }
  rc = sqlite3_step(stmt);
  if (rc != SQLITE_DONE) {
    std::cout << "Failed to insert row in table.";
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return 1;
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  return 0;
}

我不认为这一切都会“回到你身边”。显然,您需要大量阅读。