blob: 6398e16685ec00525a50c6939925c32fb899391d
1 | // |
2 | // |
3 | // amlogic 2015 |
4 | // |
5 | // @ Project : tv |
6 | // @ File Name : CSqlite |
7 | // @ Date : 2015-5 |
8 | // @ Author : |
9 | // |
10 | // |
11 | #if !defined(_CSQLITE_H_) |
12 | #define _CSQLITE_H_ |
13 | #include <unistd.h> |
14 | #include <stdlib.h> |
15 | #include <utils/String8.h> |
16 | #include <utils/Vector.h> |
17 | #include <utils/RefBase.h> |
18 | #include "CTvLog.h" |
19 | #include <sqlite3.h> |
20 | using namespace android; |
21 | class CSqlite |
22 | { |
23 | public: |
24 | class Cursor |
25 | { |
26 | public: |
27 | void Init(char **data, int cow, int col) |
28 | { |
29 | mData = data; |
30 | mCurRowIndex = 0; |
31 | mRowNums = cow; |
32 | mColNums = col; |
33 | mIsClosed = false; |
34 | } |
35 | Cursor() |
36 | { |
37 | mData = NULL; |
38 | mCurRowIndex = 0; |
39 | mRowNums = 0; |
40 | mColNums = 0; |
41 | mIsClosed = false; |
42 | } |
43 | |
44 | /*Cursor(Cursor& c) |
45 | { |
46 | data = c.data; |
47 | mCurRowIndex = 0; |
48 | mRowNums = c.mRowNums; |
49 | mColNums = c.mColNums; |
50 | mIsClosed = false; |
51 | } |
52 | |
53 | Cursor& operator = (const Cursor& c) |
54 | { |
55 | data = c.data; |
56 | mCurRowIndex = 0; |
57 | mRowNums = c.mRowNums; |
58 | mColNums = c.mColNums; |
59 | mIsClosed = false; |
60 | return *this; |
61 | }*/ |
62 | ~Cursor() |
63 | { |
64 | close(); |
65 | } |
66 | //Row nums |
67 | int getCount() |
68 | { |
69 | return mRowNums; |
70 | } |
71 | |
72 | int getPosition(); |
73 | |
74 | bool move(int offset); |
75 | |
76 | bool moveToPosition(int position); |
77 | |
78 | bool moveToFirst() |
79 | { |
80 | //LOGD("moveToFirst mRowNums = %d", mRowNums); |
81 | if (mRowNums <= 0) return false; |
82 | mCurRowIndex = 0; |
83 | return true; |
84 | } |
85 | |
86 | bool moveToLast(); |
87 | |
88 | bool moveToNext() |
89 | { |
90 | if (mCurRowIndex >= mRowNums - 1)return false; |
91 | mCurRowIndex++; |
92 | return true; |
93 | } |
94 | |
95 | bool moveToPrevious(); |
96 | |
97 | int getColumnIndex(const char *columnName) |
98 | { |
99 | int index = 0; |
100 | for (int i = 0; i < mColNums; i++) { |
101 | if (strcmp(columnName, mData[i]) == 0) |
102 | return index; |
103 | index++; |
104 | } |
105 | |
106 | return -1; |
107 | } |
108 | |
109 | //String getColumnName(int columnIndex); |
110 | //String[] getColumnNames(); |
111 | int getColumnCount(); |
112 | //字符串长度问题,弃用 |
113 | int getString(char *str, int columnIndex) |
114 | { |
115 | if (columnIndex >= mColNums || str == NULL) return -1; |
116 | strcpy(str, mData[mColNums * (mCurRowIndex + 1) + columnIndex]); |
117 | return 0; |
118 | } |
119 | //不限长度,依赖于String8 |
120 | String8 getString(int columnIndex) |
121 | { |
122 | if (columnIndex >= mColNums) return String8(""); |
123 | return String8(mData[mColNums * (mCurRowIndex + 1) + columnIndex]); |
124 | } |
125 | |
126 | int getInt(int columnIndex) |
127 | { |
128 | //if(columnIndex >= mColNums || str == NULL) return -1; |
129 | return atoi(mData[mColNums * (mCurRowIndex + 1) + columnIndex]); |
130 | } |
131 | unsigned long int getUInt(int columnIndex) |
132 | { |
133 | return strtoul(mData[mColNums * (mCurRowIndex + 1) + columnIndex], NULL, 10); |
134 | } |
135 | double getF(int columnIndex) |
136 | { |
137 | return atof(mData[mColNums * (mCurRowIndex + 1) + columnIndex]); |
138 | } |
139 | int getType(int columnIndex); |
140 | void close() |
141 | { |
142 | if (mData != NULL) |
143 | sqlite3_free_table(mData); |
144 | |
145 | mData = NULL; |
146 | mCurRowIndex = 0; |
147 | mRowNums = 0; |
148 | mIsClosed = true; |
149 | } |
150 | bool isClosed() |
151 | { |
152 | return mIsClosed; |
153 | } |
154 | private: |
155 | char **mData; |
156 | int mCurRowIndex; |
157 | int mRowNums; |
158 | int mColNums; |
159 | bool mIsClosed; |
160 | }; |
161 | public: |
162 | CSqlite(); |
163 | virtual ~CSqlite(); |
164 | int openDb(const char *path); |
165 | int closeDb(); |
166 | void setHandle(sqlite3 *h); |
167 | sqlite3 *getHandle(); |
168 | bool integrityCheck(); |
169 | int select(const char *sql, Cursor &); |
170 | bool exeSql(const char *sql); |
171 | void insert(); |
172 | void del(); |
173 | void update(); |
174 | void xxtable(); |
175 | bool beginTransaction(); |
176 | bool commitTransaction(); |
177 | bool rollbackTransaction(); |
178 | void dbsync() |
179 | { |
180 | sync(); |
181 | }; |
182 | private: |
183 | static int sqlite3_exec_callback(void *data, int nColumn, char **colValues, char **colNames); |
184 | sqlite3 *mHandle; |
185 | }; |
186 | #endif //CSQLITE |
187 |