on master help sign in
sqlite3 c lib
commit010193859ab8573c3534e557be8b62652de3c10f authorMelissa <mfm6@st-andrews.ac.uk> dateFri 13 Jul 2012, 12:31 (14 years ago) parent9a8fe0a
2 files changed+695
contribs/lib/SQLite3/sqlite3api.c+568 contribs/lib/SQLite3/sqlite3api.h+127
contribs/lib/SQLite3/sqlite3api.cnew file+568
@@ -0,0 +1,568 @@
1+/*
2+ * sqlite3api.c
3+ *
4+ *
5+ * Created by Melissa Farinaz MOZIFIAN on 22/06/2012.
6+ * Copyright 2012. All rights reserved.
7+ *
8+ */
9+
10+
11+#include "sqlite3api.h"
12+
13+
14+
15+static char sql_query_buffer[2000];
16+
17+
18+void* sqlite3_open_idr(const char *filename){
19+
20+ sqlite3 *db;
21+ int res =sqlite3_open(filename,&db);
22+
23+ if(res != SQLITE_OK){
24+
25+ printf("Error occured while opening the databse.");
26+ }
27+ DBinfo *dbi = malloc(sizeof(DBinfo));
28+ dbi->db_ptr = db;
29+ return dbi;
30+
31+}
32+
33+int sqlite3_close_idr(void* db){
34+
35+ DBinfo* dbi =(DBinfo*) db;
36+ int res =sqlite3_close(dbi->db_ptr);
37+ if (res == SQLITE_OK){
38+ free(dbi);
39+ printf("Successfuly closed the databse\n");
40+ return 0;
41+ }
42+
43+ if(res == SQLITE_BUSY){
44+ printf("SQL Busy");
45+ return 5;
46+ }
47+
48+ else {
49+ return 1;
50+ }
51+}
52+
53+int sqlite3_exec_idr(void* db, const char *sql)
54+{
55+
56+ DBinfo* dbi =(DBinfo*) db;
57+ char* err;
58+ int rc;
59+
60+ rc = sqlite3_exec(dbi->db_ptr,sql,NULL, NULL, &err);
61+ if (rc != SQLITE_OK && err != NULL) {
62+ strncpy(dbi->buffer, err, sizeof(dbi->buffer));
63+ sqlite3_free(err);
64+ }
65+ return rc;
66+}
67+
68+
69+char* sqlite3_get_error(void* db) {
70+ DBinfo* dbi =(DBinfo*) db;
71+ return dbi->buffer;
72+}
73+
74+void* sqlite3_prepare_idr(void *db,const char *zSql){
75+
76+ sqlite3_stmt* stmt;
77+ const char *tail;
78+
79+ DBinfo* dbi =(DBinfo*) db;
80+
81+ int rec = sqlite3_prepare_v2(dbi->db_ptr,zSql,-1,&stmt,&tail);
82+ dbi ->ppStmt =stmt;
83+ dbi ->Ptr_tail = tail;
84+
85+ if(rec != SQLITE_OK){
86+ fprintf(stderr, "SQL Prepare error.\n");
87+ return dbi
88+ }
89+
90+ return dbi;
91+}
92+
93+void* sqlite3_get_table_idr(void* db,
94+ const char *sql){
95+
96+ DBinfo* dbi =(DBinfo*) db;
97+ char* err;
98+
99+ Table* tbl = malloc(sizeof(Table));
100+ tbl->database = dbi;
101+
102+ int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
103+
104+ int array_size = sizeof(&tbl->table_data);
105+
106+ if( res != SQLITE_OK && err != NULL){
107+ strncpy(dbi->buffer, err, sizeof(dbi->buffer));
108+ sqlite3_free(err);
109+ }
110+
111+ tbl -> data_size = array_size;
112+
113+ int i, j, row, col;
114+
115+ row = tbl->num_row;
116+ col = tbl->num_col;
117+
118+ for( i=0; i< row; i++){
119+ for(j=0;j<col; j++){
120+ printf("%s\n", tbl->table_data[(i+1)*col +j]);
121+
122+ }
123+
124+ }
125+ return tbl;
126+
127+}
128+
129+int sqlite3_get_num_row(void* p){
130+
131+ Table* tbl =(Table*) p;
132+ int row_number =tbl->num_row;
133+ return row_number;
134+
135+
136+}
137+
138+int sqlite3_get_num_col(void* p){
139+
140+ Table* tbl =(Table*) p;
141+ int col_number =tbl-> num_col;
142+ return col_number;
143+
144+}
145+
146+int sqlite3_get_data_type(void* p,const char* tbl_name, int nRow, int nCol){
147+
148+ DBinfo* dbi =(DBinfo*) p;
149+ sqlite3_stmt* stmt;
150+ const char *tail;
151+ int rc,i, val, counter;
152+ const char* char_int;
153+
154+ counter =0;
155+ strcpy(sql_query_buffer, "select * from ");
156+ strcat(sql_query_buffer, tbl_name);
157+
158+ rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
159+ if(rc != SQLITE_OK){
160+ fprintf(stderr, "SQL Prepare error");
161+ return rc;
162+ }
163+ rc = sqlite3_step(stmt);
164+
165+ while (rc == SQLITE_ROW && counter < nRow){
166+
167+ rc = sqlite3_step(stmt);
168+ counter++;
169+
170+ }
171+
172+ val =sqlite3_column_type(stmt, nCol);
173+ printf("Type : %d\n", val );
174+
175+ rc =sqlite3_finalize(stmt);
176+ if( rc != SQLITE_OK){
177+ printf("Couldnt finalize:\n");
178+ }
179+ return val;
180+
181+}
182+
183+
184+int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol){
185+
186+
187+ DBinfo* dbi =(DBinfo*) p;
188+ sqlite3_stmt* stmt;
189+ const char *tail;
190+ int rc,i, val, counter;
191+ const char* char_int;
192+
193+ counter =0;
194+ strcpy(sql_query_buffer, "select * from ");
195+ strcat(sql_query_buffer, tbl_name);
196+
197+ rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
198+ if(rc != SQLITE_OK){
199+ fprintf(stderr, "SQL Prepare error");
200+ return rc;
201+ }
202+ rc = sqlite3_step(stmt);
203+
204+ while (rc == SQLITE_ROW && counter < nRow){
205+
206+ rc = sqlite3_step(stmt);
207+ counter++;
208+
209+ }
210+
211+ rc =sqlite3_column_int(stmt, nCol);
212+ printf("int_val : %d\n", rc );
213+ rc =sqlite3_finalize(stmt);
214+ if( rc != SQLITE_OK){
215+ printf("Couldnt finalize:\n");
216+ }
217+ return val;
218+
219+
220+}
221+
222+
223+const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol){
224+
225+
226+ DBinfo* dbi =(DBinfo*) p;
227+ sqlite3_stmt* stmt;
228+ const char *tail;
229+ int rc,i, val, counter;
230+ const unsigned char* text_val;
231+
232+ counter =0;
233+ strcpy(sql_query_buffer, "select * from ");
234+ strcat(sql_query_buffer, tbl_name);
235+
236+ rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
237+ if(rc != SQLITE_OK){
238+ fprintf(stderr, "SQL Prepare error");
239+
240+ }
241+ rc = sqlite3_step(stmt);
242+
243+ while (rc == SQLITE_ROW && counter < nRow){
244+
245+
246+ rc = sqlite3_step(stmt);
247+ counter++;
248+
249+ }
250+ text_val =sqlite3_column_text(stmt, nCol);
251+ printf("text_val : %s\n", text_val );
252+ rc =sqlite3_finalize(stmt);
253+ if( rc != SQLITE_OK){
254+ printf("Couldn't finalize.\n");
255+ }
256+ return text_val;
257+
258+}
259+
260+float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol){
261+
262+ DBinfo* dbi =(DBinfo*) p;
263+ sqlite3_stmt* stmt;
264+ const char *tail;
265+ int rc,i, val, counter;
266+ double double_val;
267+
268+
269+ counter =0;
270+ strcpy(sql_query_buffer, "select * from ");
271+ strcat(sql_query_buffer, tbl_name);
272+
273+ rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
274+ if(rc != SQLITE_OK){
275+ fprintf(stderr, "SQL Prepare error");
276+
277+ }
278+ rc = sqlite3_step(stmt);
279+
280+ while (rc == SQLITE_ROW && counter < nRow){
281+
282+
283+ rc = sqlite3_step(stmt);
284+ counter++;
285+
286+ }
287+ double_val =sqlite3_column_double(stmt, nCol);
288+ float float_val =(float)double_val;
289+ printf("float_val %f\n", float_val);
290+ rc =sqlite3_finalize(stmt);
291+ if( rc != SQLITE_OK){
292+ printf("Couldn't finalize.\n");
293+ }
294+ return float_val;
295+
296+}
297+
298+
299+void sqlite3_free_table_idr(void* db){
300+ Table* tbl =(Table*) db;
301+ sqlite3_free_table(tbl->table_data);
302+ free(tbl);
303+}
304+
305+
306+int sqlite3_step_idr(void* db){
307+
308+ DBinfo* dbi =(DBinfo*) db;
309+ int rc =sqlite3_step(dbi->ppStmt);
310+
311+ return rc;
312+}
313+
314+int sqlite3_bind_int_idr(void* p,int index, int val){
315+
316+ DBinfo* dbi =(DBinfo*) p;
317+ int rc;
318+
319+ printf("\nThe statement has %d wildcards\n", sqlite3_bind_parameter_count(dbi->ppStmt));
320+ rc =sqlite3_bind_int(dbi->ppStmt,index,val);
321+ if(rc != SQLITE_OK){
322+ printf("\nCould not bind int.\n");
323+ return 1;
324+ }
325+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
326+ printf("\nCould not step (execute) stmt.\n");
327+ return 1;
328+ }
329+ return rc;
330+}
331+
332+int sqlite3_bind_float_idr(void* p,int index, float val){
333+
334+ DBinfo* dbi =(DBinfo*) p;
335+ int rc;
336+ double res =(float)val;
337+
338+ rc =sqlite3_bind_double(dbi->ppStmt,index,res);
339+
340+ if(rc != SQLITE_OK){
341+ printf("\nCould not bind float.\n");
342+ return 1;
343+ }
344+
345+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
346+ printf("\nCould not step (execute) stmt.\n");
347+ return 1;
348+ }
349+ return rc;
350+}
351+
352+int sqlite3_bind_null_idr(void* p,int index){
353+
354+ DBinfo* dbi =(DBinfo*) p;
355+ int rc;
356+ rc =sqlite3_bind_null(dbi->ppStmt,index);
357+ if(rc != SQLITE_OK){
358+ printf("\nCould not bind null.\n");
359+ return 1;
360+ }
361+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
362+ printf("\nCould not step (execute) stmt.\n");
363+ return 1;
364+ }
365+ return rc;
366+}
367+
368+int sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
369+
370+ DBinfo* dbi =(DBinfo*) p;
371+ int rc;
372+ rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
373+ if(rc != SQLITE_OK){
374+ printf("\nCould not bind null.\n");
375+ return 1;
376+ }
377+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
378+ printf("\nCould not step (execute) stmt.\n");
379+ return 1;
380+ }
381+ return rc;
382+
383+}
384+int sqlite3_column_count_idr(void* db, const char* tbl_name){
385+ DBinfo* dbi =(DBinfo*) db;
386+ sqlite3_stmt* stmt;
387+ const char *tail;
388+ int rc;
389+
390+ strcpy(sql_query_buffer, "select * from ");
391+ strcat(sql_query_buffer, tbl_name);
392+
393+ rc = sqlite3_prepare_v2(dbi->db_ptr, sql_query_buffer, -1, &stmt, &tail);
394+ if(rc != SQLITE_OK){
395+ fprintf(stderr, "SQL Prepare error");
396+ return rc;
397+ }
398+ printf("Prepare successful %d\n", rc);
399+
400+ rc =sqlite3_column_count(stmt);
401+ if(rc == 0){
402+ fprintf(stderr, "SQL column count error\n");
403+ return rc;
404+ }
405+ printf("Column count successful = %d\n", rc);
406+ sqlite3_finalize(stmt);
407+
408+ // rc = actual column count
409+ return rc;
410+}
411+
412+
413+int sqlite3_data_count_idr(void* db){
414+
415+ DBinfo* dbi =(DBinfo*) db;
416+ int rc = sqlite3_data_count(dbi->ppStmt);
417+
418+ return rc;
419+
420+}
421+
422+int sqlite3_finalize_idr(void* db){
423+
424+ DBinfo* dbi=(DBinfo*) db;
425+ int rc =sqlite3_finalize(dbi->ppStmt);
426+ return rc;
427+}
428+
429+int sqlite3_complete_idr(const char *sql){
430+
431+ int rc = sqlite3_complete(sql);
432+ return rc;
433+
434+
435+}
436+int sqlite3_reset_idr(void* db){
437+
438+ DBinfo* dbi=(DBinfo*) db;
439+ int rc = sqlite3_reset(dbi-> ppStmt);
440+ return rc;
441+
442+}
443+
444+const char *sqlite3_column_name_idr(void* db, int N){
445+
446+ DBinfo* dbi=(DBinfo*) db;
447+ const char *name = sqlite3_column_name(dbi->ppStmt, N);
448+
449+ return name;
450+}
451+
452+const char *sqlite3_column_decltype_idr(void* db,int n){
453+ DBinfo* dbi=(DBinfo*) db;
454+ const char *dectype = sqlite3_column_decltype(dbi->ppStmt, n);
455+
456+ return dectype;
457+
458+}
459+
460+
461+int sqlite3_column_bytes_idr(void* db, int n){
462+
463+ DBinfo* dbi=(DBinfo*) db;
464+ int res = sqlite3_column_bytes(dbi->ppStmt, n);
465+ return res;
466+
467+
468+}
469+
470+const void *sqlite3_column_blob_idr(void* db, int iCol){
471+ DBinfo* dbi=(DBinfo*) db;
472+ const void* data =sqlite3_column_blob(dbi-> ppStmt, iCol);
473+ return data;
474+}
475+
476+const unsigned char *sqlite3_column_text_idr(void* db, int iCol){
477+ DBinfo* dbi=(DBinfo*) db;
478+ const unsigned char* col_text =sqlite3_column_text(dbi->ppStmt, iCol);
479+ return col_text;
480+
481+}
482+
483+int sqlite3_column_int_idr(void* db, int iCol){
484+ DBinfo* dbi=(DBinfo*) db;
485+ int res =sqlite3_column_int(dbi-> ppStmt, iCol);
486+ return res;
487+
488+}
489+
490+
491+void* sqlite3_backup_init_idr(void* pDest,
492+ const char *zDestName,
493+ void* pSource,
494+ const char *zSourceName
495+ ){
496+
497+ DBinfo* dbi=(DBinfo*) pDest;
498+ DBbackup* dbi2=(DBbackup*) pSource;
499+
500+ void* res = sqlite3_backup_init(dbi->db_ptr,zDestName,
501+ dbi2->source_ptr,zSourceName);
502+
503+ if(res == NULL){
504+ printf("Error number in initializing backup : %d\n", sqlite3_errcode(dbi->db_ptr));
505+ }
506+
507+ dbi2->backup = res;
508+ return dbi2;
509+
510+
511+}
512+
513+int sqlite3_backup_step_idr(void *backup, int nPage){
514+
515+ DBbackup* dbi=(DBbackup*) backup;
516+ int res = sqlite3_backup_step(dbi->backup, nPage);
517+
518+ if( res == SQLITE_OK){
519+ printf("Backup Success\n");
520+ }
521+ return res;
522+
523+
524+}
525+int sqlite3_backup_finish_idr(void *backup){
526+
527+ DBbackup* dbi=(DBbackup*) backup;
528+ int res = sqlite3_backup_finish(dbi->backup);
529+ return res;
530+
531+
532+}
533+
534+int sqlite3_backup_remaining_idr(void *backup){
535+
536+ DBbackup* dbi=(DBbackup*) backup;
537+ int res = sqlite3_backup_remaining(dbi->backup);
538+
539+ return res;
540+}
541+
542+int sqlite3_backup_pagecount_idr(void *backup){
543+
544+ DBbackup* dbi=(DBbackup*) backup;
545+ int res =sqlite3_backup_pagecount(dbi-> backup);
546+ return res;
547+
548+}
549+
550+int sqlite3_value_bytes_idr(void* value){
551+
552+ DBinfo* dbi=(DBinfo*) value;
553+ int res = sqlite3_value_bytes(dbi->value);
554+ return res;
555+
556+}
557+
558+int sqlite3_value_bytes16_idr(void* value){
559+
560+ DBinfo* dbi=(DBinfo*) value;
561+ int res =sqlite3_value_bytes16(dbi->value);
562+ return res;
563+
564+}
565+
566+
567+
568+
contribs/lib/SQLite3/sqlite3api.hnew file+127
@@ -0,0 +1,127 @@
1+/*
2+ * sqlite3api.h
3+ *
4+ *
5+ * Created by Melissa Farinaz MOZIFIAN on 22/06/2012.
6+ * Copyright 2012. All rights reserved.
7+ *
8+ */
9+#include <stdio.h>
10+#include <stdlib.h>
11+#include <string.h>
12+#include <sqlite3.h>
13+#include <ctype.h>
14+
15+typedef struct {
16+ sqlite3 *db_ptr;
17+ sqlite3_stmt *ppStmt;
18+ char buffer[1000];
19+ const char *Ptr_tail;
20+ sqlite3_blob *blob_ptr;
21+ sqlite3_value *value;
22+
23+} DBinfo;
24+
25+typedef struct {
26+ sqlite3 *source_ptr;
27+ sqlite3_backup *backup;
28+}DBbackup ;
29+
30+typedef struct {
31+ int num_row;
32+ int num_col;
33+ char** table_data;
34+ int data_size;
35+ int* data_type;
36+ DBinfo* database;
37+}Table;
38+
39+void* sqlite3_open_idr(const char *filename);
40+
41+int sqlite3_close_idr(void* db);
42+
43+int sqlite3_exec_idr(void*, const char *sql);
44+
45+char* sqlite3_get_error(void* db);
46+
47+
48+const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol);
49+
50+void* sqlite3_get_table_idr(void* db, const char *sql);
51+
52+int sqlite3_get_num_col(void* p);
53+
54+int sqlite3_get_num_row(void* p);
55+
56+void* sqlite3_get_data(void* p, int nRow, int nCol);
57+
58+int sqlite3_get_data_type(void* p,const char* tbl_name,int nRow, int nCol);
59+
60+int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol);
61+
62+float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol);
63+
64+void sqlite3_free_table_idr(void* db);
65+
66+void* sqlite3_prepare_idr(
67+ void *db, /* Database handle */
68+ const char *zSql /* SQL statement, UTF-8 encoded */
69+);
70+
71+int sqlite3_step_idr(void* stmt);
72+
73+int sqlite3_bind_float_idr(void* p,int index, float val);
74+
75+int sqlite3_bind_int_idr(void* p,int index , int val);
76+
77+int sqlite3_bind_int_idr_v2(void* p,int index , int val);
78+
79+int sqlite3_bind_null_idr(void* p,int index);
80+
81+int sqlite3_bind_text_idr(void* p,const char* text, int index,int length);
82+
83+int sqlite3_column_count_idr(void* stmt, const char* tbl_name);
84+
85+int sqlite3_data_count_idr(void* stmt);
86+
87+int sqlite3_reset_idr(void* stmt);
88+
89+int sqlite3_finalize_idr(void* stmt);
90+
91+int sqlite3_complete_idr(const char *sql);
92+
93+const char *sqlite3_column_decltype_idr(void* stmt,int n);
94+
95+const char *sqlite3_column_name_idr(void* stmt, int N);
96+
97+int sqlite3_column_bytes_idr(void* stmt, int n);
98+
99+int sqlite3_column_bytes_idr(void* stmt, int n);
100+
101+const void *sqlite3_column_blob_idr(void* stmt, int iCol);
102+
103+const unsigned char *sqlite3_column_text_idr(void* stmt, int iCol);
104+
105+int sqlite3_column_int_idr(void* stmt, int iCol);
106+
107+
108+void* sqlite3_backup_init_idr(void* pDestm,
109+ const char *zDestName,
110+ void* pSource,
111+ const char *zSourceName
112+ );
113+
114+int sqlite3_backup_finish_idr(void *backup);
115+
116+int sqlite3_backup_step_idr(void *backup, int nPage);
117+
118+int sqlite3_backup_remaining_idr(void *backup);
119+
120+int sqlite3_backup_pagecount_idr(void *backup);
121+
122+int sqlite3_value_bytes_idr(void* value);
123+
124+int sqlite3_value_bytes16_idr(void* value);
125+
126+int sqlite3_errcode(sqlite3 *db);
127+