summaryrefslogtreecommitdiff
path: root/tvapi/libtv/tvutils/CSqlite.cpp (plain)
blob: f4e91fbbef54341320100b9a2cbd64ad00dd2dcb
1//
2//
3// amlogic 2015
4//
5// @ Project : tv
6// @ File Name : CSqlite
7// @ Date : 2015-5
8// @ Author :
9//
10//
11#include "CSqlite.h"
12#ifdef LOG_TAG
13#undef LOG_TAG
14#define LOG_TAG "CSqlite"
15#endif
16
17using namespace android;
18
19CSqlite::CSqlite()
20{
21 mHandle = NULL;
22}
23
24CSqlite::~CSqlite()
25{
26 if (mHandle > 0) {
27 sqlite3_close(mHandle);
28 mHandle = NULL;
29 }
30}
31//完整性检测,检测数据库是否被破坏
32bool CSqlite::integrityCheck()
33{
34 char *err;
35 int rval = sqlite3_exec(mHandle, "PRAGMA integrity_check;", sqlite3_exec_callback, NULL, &err);
36 if (rval != SQLITE_OK) {
37 LOGD(" val = %d msg = %s!\n", rval, sqlite3_errmsg(mHandle));
38 return false;
39 } else {
40 return true;
41 }
42 return true;
43}
44int CSqlite::sqlite3_exec_callback(void *data, int nColumn, char **colValues, char **colNames)
45{
46 LOGD("sqlite3_exec_callback--- nums = %d", nColumn);
47 for (int i = 0; i < nColumn; i++) {
48 LOGD("%s\t", colValues[i]);
49 }
50 LOGD("\n");
51
52 return 0;
53}
54
55int CSqlite::openDb(const char *path)
56{
57 if (sqlite3_open(path, &mHandle) != SQLITE_OK) {
58 LOGD("open db(%s) error", path);
59 mHandle = NULL;
60 return -1;
61 }
62 return 0;
63}
64
65int CSqlite::closeDb()
66{
67 int rval = 0;
68 if (mHandle != NULL) {
69 rval = sqlite3_close(mHandle);
70 mHandle = NULL;
71 }
72 return rval;
73}
74void CSqlite::setHandle(sqlite3 *h)
75{
76 mHandle = h;
77}
78
79sqlite3 *CSqlite::getHandle()
80{
81 return mHandle;
82}
83int CSqlite::select(const char *sql, CSqlite::Cursor &c)
84{
85 int col, row;
86 char **pResult = NULL;
87 char *errmsg;
88 assert(mHandle && sql);
89
90 if (strncmp(sql, "select", 6))
91 return -1;
92 //LOGD("sql=%s", sql);
93 if (sqlite3_get_table(mHandle, sql, &pResult, &row, &col, &errmsg) != SQLITE_OK) {
94 LOGD("errmsg=%s", errmsg);
95 if (pResult != NULL)
96 sqlite3_free_table(pResult);
97 return -1;
98 }
99
100 //LOGD("row=%d, col=%d", row, col);
101 c.Init(pResult, row, col);
102 return 0;
103}
104
105void CSqlite::insert()
106{
107
108}
109
110bool CSqlite::exeSql(const char *sql)
111{
112 char *errmsg;
113 if (sql == NULL) return false;
114 if (sqlite3_exec(mHandle, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
115 LOGD("exeSql=: %s error=%s", sql, errmsg ? errmsg : "Unknown");
116 if (errmsg)
117 sqlite3_free(errmsg);
118 return false;
119 }
120 LOGD("sql=%s", sql);
121 return true;
122}
123
124bool CSqlite::beginTransaction()
125{
126 return exeSql("begin;");
127}
128
129bool CSqlite::commitTransaction()
130{
131 return exeSql("commit;");
132}
133
134bool CSqlite::rollbackTransaction()
135{
136 return exeSql("rollback;");
137}
138
139void CSqlite::del()
140{
141
142}
143
144void CSqlite::update()
145{
146
147}
148
149void CSqlite::xxtable()
150{
151
152}
153