db.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef SQLITE_DB_H_
  2. #define SQLITE_DB_H_
  3. #include <vector>
  4. #include <string>
  5. #include <map>
  6. #include "sqlite3.h"
  7. #define SQL_OK SQLITE_OK
  8. #define SQL_ERR SQLITE_ERROR
  9. typedef sqlite3* HDB;
  10. typedef int (*db_callback)(void* userdata, int rows, char** values, char** keys);
  11. typedef std::map<std::string, std::string> KeyVal;
  12. typedef KeyVal DBRecord;
  13. typedef KeyVal DBOption;
  14. typedef std::vector<std::string> DBRow;
  15. typedef std::vector<std::string> DBColomn;
  16. typedef std::vector<DBRow> DBTable;
  17. int db_open(const char* dbfile, HDB* phdb);
  18. int db_close(HDB* phdb);
  19. int db_exec(HDB hdb, const char* sql);
  20. int db_exec_with_result(HDB hdb, const char* sql, DBTable* table);
  21. int db_exec_cb(HDB hdb, const char* sql, db_callback cb, void* userdata);
  22. // select count(*) from sqlite_master where type='table' and name='$table_name';
  23. int dbtable_exist(HDB hdb, const char* table_name);
  24. // select count(*) from $table_name where $where;
  25. int dbtable_count(HDB hdb, const char* table_name, const char* where);
  26. // select keys from $table_name where $where limit $limit order by $column ASC|DESC;
  27. int dbtable_select(HDB hdb, const char* table_name, const char* keys, const char* where, DBTable* table, const KeyVal* options=NULL);
  28. // insert into $table_name ($keys) values ($values);
  29. int dbtable_insert(HDB hdb, const char* table_name, const char* keys, const char* values);
  30. // update $table_name set $set where $where;
  31. int dbtable_update(HDB hdb, const char* table_name, const char* set, const char* where);
  32. // delete from $table_name where $where;
  33. int dbtable_delete(HDB hdb, const char* table_name, const char* where);
  34. int dbtable_get_index(const char* key, const DBTable& table);
  35. #endif // SQLITE_DB_H_