![]() ~Database~ |
This example will generate code with std::string from the standard template library. Changes from the previous example are highlighted with bold text. Usage example with '-stl' optionHow to create a mysql sample database, one sample table, and then generate source code and build a library for that table using sql2class. $ mysqladmin create sampledb $ mysql sampledb MySQL> create table sampletable ( num integer auto_increment not null, MySQL> name varchar(255) default '' not null, MySQL> primary key(num)); MySQL> quit $ mysqldump -d sampledb > str-sampledb $ sql2class -build -global -stl -lib libsampledb str-sampledb -namespace db -overwrite Code generationGenerated source code documentation by Doxygen Sample code#include <mysql/mysql.h> #include <libsql++.h> // the mysql api wrapper #include <libsampledb.h> // our fresh lib int main(int argc,char *argv[]) { Database db("localhost","myuser","","sampledb"); // create records for each input argument for (int i = 1; i < argc; i++) { if (strlen(argv[i]) < 255) { db::Sampletbl tbl(&db); tbl.name = argv[i]; tbl.save(); } } // read and display all records Query q(&db); q.get_result("select * from sampletbl"); while (q.fetch_row()) { db::Sampletbl tbl(&db,&q); // spawns an object from Query object printf("%ld: %s\n",tbl.num,tbl.name.c_str()); } q.free_result(); // spawn an object using primary key { db::Sampletbl tbl(db, 1); if (tbl.num) // member variables will still be reset if query fails { printf("%ld: %s\n",tbl.num,tbl.name.c_str()); } } } // main |