std::vector<std::vector<std::string> > MysqlDatabase::query(const char* query) { QueryResult all_res; //all rows std::vector<std::string> result; // one row if(mysql_query(&dbsession, query)!=0) { throw DatabaseError(mysql_error(&dbsession)); } if(mysql_field_count(&dbsession) > 0) { MYSQL_RES *res; MYSQL_ROW row; unsigned int i; unsigned int num_fields; res = mysql_store_result(&dbsession); if (mysql_errno(&dbsession) > 0) { throw DatabaseError("Error: Can't fetch result"); } else { num_fields = mysql_num_fields(res); while((row = mysql_fetch_row(res)) != NULL) { //get row for (i = 0; i<num_fields; i++) { result.push_back(row[i]); } all_res.push_back(result); result.clear(); } } } return all_res; }
void MysqlDatabase::connect(const char* host, const char* user, const char* password, const char* database, int port, const char* unix_socket, unsigned long clientflag) { if(mysql_init(&dbsession)==NULL) { throw DatabaseError("Connection to mysql database creating failed"); } if(mysql_real_connect(&dbsession, host, user, password, database, port, unix_socket, clientflag) == NULL) { throw DatabaseError(mysql_error(&dbsession)); } }
void Database::init(const std::string &path, const std::string &conn_name, const std::string &create_script) { if (db.get() != nullptr && db->isOpen()) throw AlreadyOpen(path); QString qconn_name = QString::fromStdString(conn_name); if (QSqlDatabase::contains(qconn_name)) { db.reset(new QSqlDatabase(QSqlDatabase::database(qconn_name))); } else { db.reset(new QSqlDatabase(QSqlDatabase::addDatabase(cfg::db::driver, qconn_name))); } QFileInfo info(QString::fromStdString(path)); if (!info.exists()) { info.setFile(QString::fromStdString(create_script)); if (!info.exists() || !info.isFile()) throw InvalidFile(create_script); db->setDatabaseName(QString::fromStdString(path)); if (!db->open()) throw DatabaseError(db->lastError()); QFile file(QString::fromStdString(create_script)); if (!file.open(QFile::ReadOnly)) throw InvalidFile(create_script); QString query = file.readAll(); auto query_list = query.split(';'); for (auto curr_query: query_list) { curr_query = curr_query.trimmed(); if (curr_query.isEmpty()) continue; execute(curr_query.toStdString()); } } else { db->setDatabaseName(QString::fromStdString(path)); } if (!db->open()) throw DatabaseError(db->lastError()); }
//--------------------------------------------------------------------------- void __fastcall TClientAssistant::ClientsBrowseLeave(bool Next) { if (Next) { if ((IsDataSetEmpty(SearchQuery)) || !(DataSetFindValue(Clients, SearchQuery->FieldByName("ACCT_NBR")->AsString, "ACCT_NBR"))) { DatabaseError("No data found"); } if ((Mode == camDelete) && ConfirmDelete()) { Clients->Delete(); RefreshQuery(SearchQuery); } } else { SearchQuery->Close(); } }
void Database::open() { if (!db->open()) throw DatabaseError(db->lastError()); }