项目作者: somnisoft

项目描述 :
SQL Database Abstraction Library in C
高级语言: C
项目地址: git://github.com/somnisoft/sqldbal.git
创建时间: 2019-02-24T00:57:57Z
项目社区:https://github.com/somnisoft/sqldbal

开源协议:Other

下载


SQLDBAL - SQL Database Abstraction Library

SQL database abstraction library written in C which can get included
directly into another program. Each driver has been implemented
using the client connector library provided by the corresponding
database system.

Feature List

  • Single C and header file
  • Cross-platform (POSIX, BSD, Windows)
  • Simple API that handles most common database operations
    (driver-specific database/statement handles available if needed)
  • Test cases with 100% code and branch coverage
  • Doxygen with 100% documentation
  • Free software (permissive - CC0)

Supports the following database engines:

  • MariaDB/mySQL (requires libmariadbclient or libmysqlclient)
  • PostgreSQL (requires libpq)
  • SQLite (requires libsqlite3)

To include the library into your application, simply copy the src/sqldbal.h
and src/sqldbal.c files into your project directory. Then include the
sqldbal.h header into your C file, compile sqldbal.c, and include the
resulting object file into the build system.

Examples

The following example code demonstrates how to use this library with the
SQLite driver. This example opens a database file, creates a table,
inserts a record, and then reads back the inserted records.

  1. #include <assert.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "sqldbal.h"
  5. #define DRIVER SQLDBAL_DRIVER_SQLITE
  6. #define LOCATION "example.db"
  7. #define PORT NULL
  8. #define USERNAME NULL
  9. #define PASSWORD NULL
  10. #define DATABASE NULL
  11. #define FLAGS (SQLDBAL_FLAG_DEBUG | \
  12. SQLDBAL_FLAG_SQLITE_OPEN_CREATE | \
  13. SQLDBAL_FLAG_SQLITE_OPEN_READWRITE)
  14. int main(void){
  15. enum sqldbal_status_code rc;
  16. struct sqldbal_db *db;
  17. struct sqldbal_stmt *stmt;
  18. int64_t i64;
  19. const char *text;
  20. rc = sqldbal_open(DRIVER,
  21. LOCATION,
  22. PORT,
  23. USERNAME,
  24. PASSWORD,
  25. DATABASE,
  26. FLAGS,
  27. NULL,
  28. 0,
  29. &db);
  30. assert(rc == SQLDBAL_STATUS_OK);
  31. rc = sqldbal_exec(db,
  32. "CREATE TABLE IF NOT EXISTS test(id INTEGER, str TEXT)",
  33. NULL,
  34. NULL);
  35. assert(rc == SQLDBAL_STATUS_OK);
  36. rc = sqldbal_stmt_prepare(db,
  37. "INSERT INTO test(id, str) VALUES(?, ?)",
  38. -1,
  39. &stmt);
  40. assert(rc == SQLDBAL_STATUS_OK);
  41. rc = sqldbal_stmt_bind_int64(stmt, 0, 10);
  42. assert(rc == SQLDBAL_STATUS_OK);
  43. rc = sqldbal_stmt_bind_text(stmt, 1, "test string", -1);
  44. assert(rc == SQLDBAL_STATUS_OK);
  45. rc = sqldbal_stmt_execute(stmt);
  46. assert(rc == SQLDBAL_STATUS_OK);
  47. rc = sqldbal_stmt_close(stmt);
  48. assert(rc == SQLDBAL_STATUS_OK);
  49. rc = sqldbal_stmt_prepare(db,
  50. "SELECT id, str FROM test WHERE id = 10",
  51. -1,
  52. &stmt);
  53. assert(rc == SQLDBAL_STATUS_OK);
  54. rc = sqldbal_stmt_execute(stmt);
  55. assert(rc == SQLDBAL_STATUS_OK);
  56. while(sqldbal_stmt_fetch(stmt) == SQLDBAL_FETCH_ROW){
  57. rc = sqldbal_stmt_column_int64(stmt, 0, &i64);
  58. assert(rc == SQLDBAL_STATUS_OK);
  59. rc = sqldbal_stmt_column_text(stmt, 1, &text, NULL);
  60. assert(rc == SQLDBAL_STATUS_OK);
  61. printf("%d / %s\n", (int)i64, text);
  62. }
  63. rc = sqldbal_stmt_close(stmt);
  64. assert(rc == SQLDBAL_STATUS_OK);
  65. rc = sqldbal_close(db);
  66. assert(rc == SQLDBAL_STATUS_OK);
  67. return 0;
  68. }

Place the code snippet above into a file named ‘example.c’ and change
each #define to the appropriate values for your database. Then copy sqldbal.c
and sqldbal.h into the same directory and run the following commands to
compile with support for all databases.

cc -DSQLDBAL_MARIADB -DSQLDBAL_POSTGRESQL -DSQLDBAL_SQLITE sqldbal.c -c -o sqldbal.o

cc -DSQLDBAL_MARIADB -DSQLDBAL_POSTGRESQL -DSQLDBAL_SQLITE example.c -c -o example.o

cc example.o sqldbal.o -o sqldbal_test -lsqlite3 -lpq -lmariadbclient

Remove the corresponding -D and -l arguments for the database drivers that
you do not need to use. The commands as above should create an
executable called ‘sqldbal_test’.

Technical Documentation

See the
Technical Documentation
generated from Doxygen.