#include "KVConfig.h"
#ifdef WITH_RSQLITE
#include "SQLiteDB.h"
#include <KVNumberList.h>
void sqlite_example()
{
TString names[] = {
"Audi",
"Mercedes",
"Citroen",
"Volvo",
"Bentley",
"Citroen",
"Audi",
"Volkswagen",
"-"};
int prices[] = {52642, 57127, 9000, 29000, 350000, 21000, 41400, 21600};
int row = 0;
while (names[row] != "-") {
db["Cars"]["Model"].set_data(names[row]);
db["Cars"]["Price"] = prices[row];
++row;
}
int numbers[] = {221, 222, 245, 250, 321, 333, -1};
TString wrks[] = {
"WRK01",
"",
"WRK02",
"WRK01",
"",
"WRK03"};
row = 0;
while (numbers[row] > 0) {
db["Tasks"]["Number"] = numbers[row];
if (wrks[row] != "") {
db["Tasks"]["Worker"] = wrks[row];
}
else {
db["Tasks"]["Worker"].set_null();
}
++row;
}
tt.add_primary_key(
"ownerid");
tt.add_foreign_key(
"Cars",
"Id");
int car_id[] = {2, 8, 1, 6, 7, 3, 5, 4};
TString owner[] = {
"Jeremy",
"Hamster",
"Jeremy",
"James",
"James",
"Hamster",
"Jeremy",
"Jeremy"};
db[
"CarOwners"][
"Owner"].set_data(owner[
N]);
db[
"CarOwners"][
"Id"].set_data(car_id[
N]);
}
}
std::cout << "SELECT * FROM Cars\n" << std::endl;
for (int i = 0; i < db["Cars"].number_of_columns(); ++i)
std::cout << db[
"Cars"][i].
name() <<
"\t\t\t";
std::cout << std::endl;
std::cout << db["Cars"]["Id"].get_data<int>()
<< "\t\t\t" << db["Cars"]["Model"].get_data<TString>()
<< "\t\t\t" << db["Cars"]["Price"].get_data<int>()
<< std::endl;
}
std::cout << std::endl << std::endl;
std::cout << "SELECT * FROM Cars ORDER BY Price\n" << std::endl;
db.
select_data(
"Cars",
"*",
"",
false,
"ORDER BY Price");
std::cout << Cars["Id"].get_data<int>()
<< "\t\t\t" << Cars["Model"].get_data<TString>()
<< "\t\t\t" << Cars["Price"].get_data<int>()
<< std::endl;
}
std::cout << std::endl << std::endl;
std::cout << "SELECT DISTINCT Model FROM Cars\n" << std::endl;
std::cout << Cars[
"Model"].get_data<
TString>() <<
", ";
}
std::cout << std::endl << std::endl;
std::cout << "Task numbers for worker WRK01: ";
nl.
Add(db[
"Tasks"][
"Number"].get_data<int>());
}
std::cout << nl.
AsString() << std::endl << std::endl;
std::cout << "Number=" << db["Tasks"]["Number"].get_data<int>() <<
" Worker=" << db["Tasks"]["Worker"].get_data<TString>() << std::endl;
}
std::cout << "Task numbers with no worker: ";
nl.
Add(db[
"Tasks"][
"Number"].get_data<int>());
}
std::cout << nl.
AsString() << std::endl << std::endl;
std::cout << "The number of unique Worker names in table Tasks is " <<
db.
count(
"Tasks",
"Worker",
"",
true)
<< std::endl;
std::cout << "The number of rows with no Worker name in table Tasks is " <<
db.
count(
"Tasks",
"*",
"Worker IS NULL")
<< std::endl;
db["Tasks"]["Worker"].set_data("WRK04");
db.
update(
"Tasks",
"Worker",
"Worker IS NULL");
std::cout << "The number of unique Worker names in table Tasks is " <<
db.
count(
"Tasks",
"Worker",
"",
true)
<< std::endl;
std::cout << "The number of rows with no Worker name in table Tasks is " <<
db.
count(
"Tasks",
"*",
"Worker IS NULL")
<< std::endl;
std::cout << "Owner"
<< "\t\t\t" << "Model"
<< "\t\t\t" << "Price"
<< std::endl;
db.
select_data(
"Cars,CarOwners",
"Owner,Model,Price",
"",
false,
"ORDER BY Owner");
std::cout << db[
"CarOwners"][
"Owner"].get_data<
TString>()
<< "\t\t\t" << db["Cars"]["Model"].get_data<TString>()
<< "\t\t\t" << db["Cars"]["Price"].get_data<int>()
<< std::endl;
}
}
#endif
Strings used to represent a set of ranges of values.
const Char_t * AsString(Int_t maxchars=0) const
void Add(Int_t)
Add value 'n' to the list.
void Clear(Option_t *="")
Empty number list, reset it to initial state.
TString GetSQL(const Char_t *column) const
Interface to ROOT SQLite database backend.
int get_number_of_tables() const
int count(const TString &table, const TString &column="*", const TString &selection="", bool distinct=false) const
bool update(const TString &table, const TString &columns, const TString &selection="")
void end_data_insertion()
bool select_data(const TString &tables, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="") const
bool get_next_result() const
void add_table(const table &)
bool prepare_data_insertion(const TString &)
const column & add_primary_key(const TString &name)
column & add_column(const KVSQLite::column &c)