How to Use ODB (how to use ODB on Windows)

Source: Internet
Author: User

1. Download ODB Library: ODB compiler, common Runtime Library, and database Runtime Library.

Http://www.codesynthesis.com/products/odb/download.xhtml (Note: ODB compiler is the odb-x.x.x-i686-windows, database runtime libraries the database you want to use) 2. Unzip, install you can execute ODB in the root directory, or add it to the environment variable. 3. Define the class in the header file. The class above is normal, and the class below is modified to the ODB persistent class.

class person { ... private: string email_; string name_; unsigned short age_; };
 
 #include <string>
 #include <odb/core.hxx>  
 #pragma db object  class person  {    ...  private:    friend class odb::access;    person () {}    #pragma db id    string email_;    string name_;    unsigned short age_;  };
4. Use ODB to generate database support code. Only in this way can the persistent class defined above be used for query. Specific command line parameters: Export -- database MySQL -- generate-query D: \ mcsf_backup \ main \ Features \ mcsfprotocoldbwrapper \ include \ catalog. hxx Generate 5. Add the three generated files to the project for use. Such as insert:
 
#include <memory>   // std::auto_ptr#include <iostream>#include <odb/database.hxx>#include <odb/transaction.hxx>#include <odb/mysql/database.hxx>
// Add a reference to the generated file # include "person. hxx "# include" person-odb.hxx "using namespace STD; using namespace ODB: core; intmain (INT argc, char * argv []) {try {auto_ptr <database> dB (New ODB: mysql: Database (argc, argv); unsigned long john_id, jane_id, joe_id; // create a few persistent person objects. // {person John ("John", "doe", 33); person Jane ("Jane", "doe", 32); person Joe ("Joe ", "dirt", 30); transaction T (db-> begin (); // make objects persistent and save their IDs for later use. // john_id = DB-> persist (John); jane_id = DB-> persist (Jane); joe_id = DB-> persist (Joe); T. commit () ;}} catch (const ODB: exception & E) {cerr <E. what () <Endl; return 1 ;}}
6. Note: Several # pragma are frequently used in the defined persistent class. 1) Specify the field as the primary key: # pragma dB Id2) Specify the field as the database type: # pragma dB type ("varchar (64) binary not null") 3) specify the field as the corresponding column of the database: # pragma DB column ("catalogname") 4) Specify the auto-increment of the primary key of the database: # pragma dB ID auto
 
When ODB is used to generate database support code, sometimes other libraries are referenced in the persistent class, and the path can be added through-I during compilation.
For example:
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.