-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetImage.cpp
More file actions
57 lines (49 loc) · 2.16 KB
/
Copy pathgetImage.cpp
File metadata and controls
57 lines (49 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//-*-c++-*-
//-*- coding: utf-8 -*-
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
/*$C gcc -std=c++20 -DTESTING_GETONE getOne.cpp -o getOne -Isqlite -Lsqlite -l:libsqlitelib.a -lstdc++ */
#include <sqlite3.h>
#include <iostream>
#include <sstream>
#define TABLE_NAME "files"
static std::string tableName(TABLE_NAME);
static sqlite3* db;
#include "mainWindow.h"
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
bool MainWindow::openDatabase(const QString &databasePath)
{
int rc = sqlite3_open(databasePath.toStdString().c_str(), &db);
if (rc != SQLITE_OK) {
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << "\n";
sqlite3_close(db);
return false;
}
return true;
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
unsigned char *MainWindow::getImage(void)
{
char* errmsg = nullptr;
std::stringstream ss;
ss << "SELECT * FROM " << tableName << " ORDER BY RANDOM() LIMIT 1;";
std::string inq = ss.str();
int rc = sqlite3_exec(db, inq.c_str(), 0, 0, &errmsg);
if (rc != SQLITE_OK) {
std::cout << "sqlite: cannot select: " << sqlite3_errmsg(db) << "\n";
sqlite3_close(db);
return (unsigned char *)"";
}
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, inq.c_str(), -1, &stmt, nullptr);
if (rc != SQLITE_OK) {
std::cout << "Error preparing statement: " << sqlite3_errmsg(db) << "\n";
sqlite3_close(db);
return (unsigned char *)"";
}
static unsigned char *name = nullptr;
if ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
name = (unsigned char *)sqlite3_column_text(stmt, 0);
// cout << "name " << name << "\n";
}
return name;
}