~Database~ |
Usage exampleHow 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 -lib libsampledb str-sampledb -namespace db -overwrite Code generationGenerated source code documentation by Doxygen Sample code using sampledb
#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);
strcpy(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);
}
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);
}
}
} // main
|