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