[英]SQLite 3 C++ Quiz - Comparing user's response to existing database
我正在嘗試使用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 ++以來已經有一段時間了,而且還沒有全部回復給我......任何幫助都將受到贊賞。
將您的代碼與此進行比較:
#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;
}
我認為這一切都不會“回到你身邊”。 很明顯,你面前有很多閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.