{"id":18777726,"url":"https://github.com/daelsepara/mysql-connector-demo","last_synced_at":"2025-08-08T06:11:26.875Z","repository":{"id":142354076,"uuid":"424573763","full_name":"daelsepara/mysql-connector-demo","owner":"daelsepara","description":"This is a demonstration of how to use mysql-connector library in C++","archived":false,"fork":false,"pushed_at":"2021-11-04T12:47:04.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-21T05:11:41.737Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daelsepara.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-04T11:41:46.000Z","updated_at":"2021-11-04T12:47:07.000Z","dependencies_parsed_at":"2023-04-16T07:13:41.588Z","dependency_job_id":null,"html_url":"https://github.com/daelsepara/mysql-connector-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/daelsepara/mysql-connector-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fmysql-connector-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fmysql-connector-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fmysql-connector-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fmysql-connector-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daelsepara","download_url":"https://codeload.github.com/daelsepara/mysql-connector-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daelsepara%2Fmysql-connector-demo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269373111,"owners_count":24406321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T20:13:29.732Z","updated_at":"2025-08-08T06:11:26.825Z","avatar_url":"https://github.com/daelsepara.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySQL Connector/C++ Library Demo\n\nThis is a single header file demonstration of how to use mysql-connector library in C++. It uses smart pointers in to automatically free up allocated resources whenever they go out of scope. \n\n## Requires\n\n- MySQL Connector for C++ (development) files (\u003e= 1.1.12)\n- MySQL Connector for C++ (library) (\u003e= 1.1.12)\n- C++ compiler with C++17 support\n\n## Usage\n\n### Initialize database\n\n```\n// use connection details of your database in the statement below: host, user, password, database\nauto db = DB::Base(\"tcp://localhost:3306\", \"user\", \"password\", \"database\");\n```\n\n### Querying\n\n```\n// return the first 10 numbers of the sequence 0 to 99\nstd::string query1 = \"SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT 0, 10\";\n\nauto result1 = db.Query(query1.c_str());\n```\n\n### Querying with prepared statements\n\n```\n// return the next 10 numbers of the sequence 0 to 99\nstd::string query2 = \"SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT ?, ?\";\n\nauto result2 = db.Query(query2.c_str(), {10, 10});\n```\n\nAs of this moment the ![DB::Base](src/db.hpp/) class supports **std::string**, **const char***, **bool**, **float**, **double**, and **int** values through C++17's **std::any**.\n\n### Reading the results\n\n```\nauto result1 = db.Query(query1.c_str());\n\nif (result1)\n{\n    std::cerr \u003c\u003c \"Query: \" \u003c\u003c query1 \u003c\u003c std::endl;\n\n    while (result1-\u003enext())\n    {\n        // display the value first field/column\n        std::cout \u003c\u003c result1-\u003egetString(1) \u003c\u003c std::endl;\n    }\n}\n```\n\n## Compiling the sample program\n\nMake sure you change the connection information in ![test.hpp](src/test.cpp/) before compiling.\n\n```\ncd ~/path/to/repo/src\nmake\n```\n\n## Testing\n\n```\ncd ~/path/to/repo/src\n./test.exe\n```\n\n### Sample output\n\n```\nQuery: SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT 0, 10\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nQuery: SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT ?, ?\nValues used: 10, 10\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n```\n\n## Testing with valgrind\n\n```\ncd ~/path/to/repo/src\nvalgrind ./test.exe\n```\n\n### Sample output\n\n```\n==41129== Memcheck, a memory error detector\n==41129== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.\n==41129== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info\n==41129== Command: ./test.exe\n==41129==\nQuery: SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT 0, 10\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nQuery: SELECT * FROM SEQUENCE_TABLE (100) AS SEQ LIMIT ?, ?\nValues used for ?, ?: 10, 10\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n==41129== \n==41129== HEAP SUMMARY:\n==41129==     in use at exit: 0 bytes in 0 blocks\n==41129==   total heap usage: 6,311 allocs, 6,311 frees, 651,579 bytes allocated\n==41129== \n==41129== All heap blocks were freed -- no leaks are possible\n==41129== \n==41129== For lists of detected and suppressed errors, rerun with: -s\n==41129== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\n```\n\n## References\n\n- [MySQL Connector/C++ 1.1 Developer Guide](https://dev.mysql.com/doc/connector-cpp/1.1/en/)\n- [MySQL Connector/C++ 8.0 Developer Guide](https://dev.mysql.com/doc/connector-cpp/8.0/en/)\n- [std::any class](http://en.cppreference.com/w/cpp/utility/any)\n- [std::unique_ptr smart pointer](https://en.cppreference.com/w/cpp/memory/unique_ptr)\n- [Generating Numeric Sequences in MySQL](https://www.percona.com/blog/2020/07/27/generating-numeric-sequences-in-mysql/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaelsepara%2Fmysql-connector-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaelsepara%2Fmysql-connector-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaelsepara%2Fmysql-connector-demo/lists"}