Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6769247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:10:13+00:00 2026-05-26T15:10:13+00:00

So I loock at sqlite3cpp wiki . And thay have such a nice API:

  • 0

So I loock at sqlite3cpp wiki. And thay have such a nice API:

sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)");
cmd.bind(":user", "Mike");
cmd.bind(":phone", "555-1234");
cmd.execute();

I wonder how using boost to create API alike it forregular std::string? meaning something like

std::string str = "INSERT INTO contacts (name, phone) VALUES (:user, :phone)";
bind(str, ":user", "Mike");
bind(str, ":phone", "555-1234");

Is it possible to create such thing with boost and how to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:10:13+00:00Added an answer on May 26, 2026 at 3:10 pm

    Replacing a string is easy, but to perform something that is type-safe and will convert the SQL nicely, is slightly different. We need a binder class that can bind by the type passed and do any necessary conversions.

    Firstly, we need to wrap up std::type_info so that it can be used in a hash map:

    class typeInfoWrapper
    {
    friend bool operator == (const typeInfoWrapper& l, const typeInfoWrapper& r);
    private:
        const std::type_info& typeInfo_;
    
    public:
        typeInfoWrapper(const std::type_info& info) : typeInfo_(info) { };
    
        // hasher
        class hash
        {
        public:
            size_t operator()(const typeInfoWrapper& typeInfo) const
            {
                return typeInfo.typeInfo_.hash_code();
            };
        };  // eo class hash
    };  // eo class typeInfoWrapper
    
    bool operator == (const typeInfoWrapper& l, const typeInfoWrapper& r)
    {
        return l.typeInfo_.hash_code() == r.typeInfo_.hash_code();
    }   // eo operator == 
    

    Next, we need the class itself. I’m using C++11 here, so I’m going to use lambdas. For each type we register, we’ll register a function that takes a string and returns it in a format suitable for SQL. In this example, I register one for a string and one for an int. The string one just replaces ' with '' and returns it in quotes itself. The int one just returns itself, no parsing to do for SQL.

    class binder
    {
    private:
        typedef std::function<std::string(std::string&)> ReplaceFunc;
        typedef std::tr1::unordered_map<typeInfoWrapper, ReplaceFunc, typeInfoWrapper::hash> ReplaceMap;
        typedef std::pair<typeInfoWrapper, ReplaceFunc> ReplacePair;
        ReplaceMap typeMap_;
    
    public:
        binder()
        {
            // add string and int for test purposes
            typeMap_.insert(ReplacePair(typeid(const char*), [](std::string& data) -> std::string
            {
                // escape the "'" to prevent SQL injection
                boost::replace_all(data, "'", "''");
                return "'" + data + "'";
            }));
    
            typeMap_.insert(ReplacePair(typeid(int), [](std::string& data) -> std::string
            {
                // for sql, this is easy, just return the value as is
                return data;
            }));
        };
    
    
        // func
        template<class T>
        void bind(std::string& input, const std::string& expr, T data)
        {
            ReplaceMap::const_iterator cit(typeMap_.find(typeid(T)));
            if(cit != typeMap_.end())
                boost::replace_all(input, expr, cit->second(boost::lexical_cast<std::string>(data)));
        };  // eo bind
    };  // eo class bind
    

    And as you can see, we have the bind function.

    Now we can bind in a type-safe way!

    binder b;
    std::string data = "SELECT * FROM table WHERE _user = :user AND _id = :id";
    b.bind(data, ":user", "Moo-Juice");
    b.bind(data, ":id", 32);
    

    EDIT: fixed some errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
Delegates look like such a powerful language feature, but I've yet to find an
Look at your Gmail inbox. You have 3 main columns contains the Sender contains
look at this table please table |id| |name| |order| i must get the rows,
I actually seem to have problem with this (only on linux, described here with
Look this what i have string imagePath = http:// & ConfigurationManager.AppSettings(DomainName) & /images/ htmlstring
Look at the line THIS LINE #### in following example. <ListBox Grid.Row=0 x:Name=listBoxServers> <ListBoxItem
So imagine we have TCP server recieving all browser requests. How a cookie generated
Look at this makefile, it has some sort of primitive progress indication (could have
What I need is simple: qml way to create a button that would loock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.