Wednesday, September 16, 2009

Big Breasted Hair Stylist

Sqlite - exercise

recently became interested in another topic, namely SQL. So far I have heard that it is a powerful tool for database management, but somehow never took up be yourself and do not sit at this. Recently I have some free time, so I looked on the website:
www.w3schools.com
where there are lots of materials about SQL and it is very accessible form. After crossing the grounds had the opportunity to send a few queries to the training database. This proved easier than I thought. serving as a library database, chose a Sqlite3. It is quite simple and do not need to place any SQL servers or something. Shall be installed using:

sudo apt-get install libsqlite3-0 libsqlite3-dev Very likely that libsqlite3-0 is already in the system because it uses such as Firefox.

Now create a new project, add to the linker / usr/lib/libsqlite3.so and write some code:

/ * * File: main.cpp
* Author: Daniel
* * Created on September 14 2009, 22:24
* / # include
stdlib.h # include # include \u0026lt;sqlite3.h>
\u0026lt;iostream>
\u0026lt;string> # include # include # \u0026lt;sstream> include \u0026lt;vector>
using std:: court;
using std:: endl; / * *

* / / / show error message if it exists, and if not then the code value
void handleReturnCode (returncode int, char * errormsg = NULL) { if (returncode! = SQLITE_OK) {if (errormsg! = NULL) {
std:: court
sqlite3_free (errormsg);
} else {std:: court
}}}

/ / structure that stores data about a person sOsoba struct {
std:: string name; std:: string name; Int rokUrodzenia;

sOsoba (const char * p_imie, const char * p_nazwisko, const int p_rokUrodzenia) {name = p_imie
; }};
int main (int argc , char ** argv) {sqlite3 * db
;
int returncode; court
sqlite3_open returncode = ("test2.db", & db);
if (returncode! = 0) std:: court
char * errormsg; court
returncode = sqlite3_exec (db, "DROP TABLE IF EXISTS tab", NULL, NULL, & errormsg);
 handleReturnCode (returncode, Errormsg); 

court
returncode = sqlite3_exec (db,
"create table tab (name string, name string,
rokUrodzenia integer);"
NULL, NULL, & errormsg);
handleReturnCode (returncode, errormsg);

court
std:: vector \u0026lt;sOsoba> listaOsob;
listaOsob.push_back (sOsoba ("Donald", "Tusk", 1957));
listaOsob.push_back (sOsoba ("Lech", "Kaczynski, 1949));
listaOsob.push_back (sOsoba ("Jaroslaw", "Kaczynski, 1949));
listaOsob.push_back (sOsoba ("Greg", "Napieralski, 1974));
listaOsob.push_back (sOsoba ('Waldemar', 'Pawlak, 1959));
listaOsob.push_back (sOsoba (" Greg "," Schetyna , 1963));

/ / Add more names
std:: vector \u0026lt;sOsoba>:: iterator it = listaOsob.begin ();
while (it! = listaOsob.end ()) {court

name
Name
std:: stringstream query; << ErrorMsg << std::endl;
query

name << "Return code: " << returnCode << std::endl;


name


rokUrodzenia
returncode = sqlite3_exec (db, query.str (). c_str (), NULL, NULL, & errormsg);
handleReturnCode (returncode, errormsg);
it + +;}


char ** result, int
nRows, ncols;
/ / query about the politicians who born after 1960
sqlite3_get_table (db,
"select name, rokUrodzenia
from Table
WHERE rokUrodzenia> 1960
order to rokUrodzenia asc",
& result, & nRows, & ncols, & errormsg);
court
/ / null line is the header table, and the rest of the results, so rows are nRows + 1 << "Otwieranie bazy danych: " << endl;
for (int i = 0; and
for (int j = 0; j << returnCode << std::endl;
court
if (j> 0 & & i> 0) {
/ / to check whether it is easy to convert a number << "Usunięcie tabeli tab" << endl;
int year = atoi (result [i * ncols + j]);
court

}}} << "Utworzenie tabeli tab" << endl;
court

/ / free memory allocated for the table
sqlite3_free_table (result);

court
sqlite3_close (db); << "Utworzenie listy osob" << endl;
/ / call system command exporting tab table to csv file
system ("sqlite3-csv test2.db \\" select * from tab; \\ "> wyjscie.csv");
court
/ / display the contents of a csv file in the console
system ("cat wyjscie.csv ");

return (EXIT_SUCCESS);}




0 comments:

Post a Comment