inne

mail@pastecode.io avatar
unknown
c_cpp
13 days ago
3.1 kB
2
Indexable
Never
#include <stdio.h>
#include <iostream>
#include <sqlite3.h>
#include "../include/createEntries.h"

static int callback(void* NotUsed, int argc, char** argv, char** azColName) {
    for (int i = 0; i < argc; i++) {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int createEntry(const int& id, const std::string& name, const std::string& lastName, 
    const std::string& address, const double& balance, const int& age, const double& limit, 
    const std::string& login, const std::string& password) {

    sqlite3* db;
    const char* tableSQLite =
        "CREATE TABLE IF NOT EXISTS STANDARD_ACCOUNT ("
        "  ID INT PRIMARY KEY   NOT NULL, "
        "  NAME TEXT            NOT NULL, "
        "  LAST_NAME TEXT       NOT NULL, "
        "  ADDRESS TEXT         NOT NULL, "
        "  BALANCE NUMERIC      NOT NULL, "
        "  AGE NUMERIC          NOT NULL, "
        "  LIMIT NUMERIC        NOT NULL, "
        "  LOGIN TEXT           NOT NULL, "
        "  PASSWORD TEXT        NOT NULL"
        ");";

    const char* insertSQL =
        "INSERT INTO STANDARD_ACCOUNT (ID, NAME, LAST_NAME, ADDRESS, BALANCE, AGE, LIMIT, LOGIN, PASSWORD) ";

    char* zErrMsg = 0;
    int rc;

    rc = sqlite3_open(DATABASE_PATH, &db);
    if (rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return 1;
    }

    // Execute table creation statement
    rc = sqlite3_exec(db, tableSQLite, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 1;
    }

    // Execute insert statement
    rc = sqlite3_exec(db, insertSQL, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 1;
    }

    const char* insertSQL =
        "INSERT INTO STANDARD_ACCOUNT (ID, NAME, LAST_NAME, ADDRESS, BALANCE, AGE, LIMIT, LOGIN, PASSWORD) "
        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";

    sqlite3_stmt* stmt;
    rc = sqlite3_prepare_v2(db, insertSQL, -1, &stmt, 0);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }

    sqlite3_bind_int(stmt, 1, id);
    sqlite3_bind_text(stmt, 2, name.c_str(), -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 3, lastName.c_str(), -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 4, address.c_str(), -1, SQLITE_STATIC);
    sqlite3_bind_double(stmt, 5, balance);
    sqlite3_bind_int(stmt, 6, age);
    sqlite3_bind_double(stmt, 7, limit);
    sqlite3_bind_text(stmt, 8, login.c_str(), -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 9, password.c_str(), -1, SQLITE_STATIC);

    // Execute the statement
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);
    sqlite3_close(db);
    return 0;
}
Leave a Comment