@@ -0,0 +1,568 @@
5+ * Created by Melissa Farinaz MOZIFIAN on 22/06/2012.
6+ * Copyright 2012. All rights reserved.
11+#include "sqlite3api.h"
15+static char sql_query_buffer[2000];
18+void* sqlite3_open_idr(const char *filename){
21+ int res =sqlite3_open(filename,&db);
23+ if(res != SQLITE_OK){
25+ printf("Error occured while opening the databse.");
27+ DBinfo *dbi = malloc(sizeof(DBinfo));
33+int sqlite3_close_idr(void* db){
35+ DBinfo* dbi =(DBinfo*) db;
36+ int res =sqlite3_close(dbi->db_ptr);
37+ if (res == SQLITE_OK){
39+ printf("Successfuly closed the databse\n");
43+ if(res == SQLITE_BUSY){
53+int sqlite3_exec_idr(void* db, const char *sql)
56+ DBinfo* dbi =(DBinfo*) db;
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));
69+char* sqlite3_get_error(void* db) {
70+ DBinfo* dbi =(DBinfo*) db;
74+void* sqlite3_prepare_idr(void *db,const char *zSql){
79+ DBinfo* dbi =(DBinfo*) db;
81+ int rec = sqlite3_prepare_v2(dbi->db_ptr,zSql,-1,&stmt,&tail);
83+ dbi ->Ptr_tail = tail;
85+ if(rec != SQLITE_OK){
86+ fprintf(stderr, "SQL Prepare error.\n");
93+void* sqlite3_get_table_idr(void* db,
96+ DBinfo* dbi =(DBinfo*) db;
99+ Table* tbl = malloc(sizeof(Table));
100+ tbl->database = dbi;
102+ int res = sqlite3_get_table(dbi->db_ptr,sql,&tbl->table_data,&tbl->num_row,&tbl->num_col,&err);
104+ int array_size = sizeof(&tbl->table_data);
106+ if( res != SQLITE_OK && err != NULL){
107+ strncpy(dbi->buffer, err, sizeof(dbi->buffer));
111+ tbl -> data_size = array_size;
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]);
129+int sqlite3_get_num_row(void* p){
131+ Table* tbl =(Table*) p;
132+ int row_number =tbl->num_row;
138+int sqlite3_get_num_col(void* p){
140+ Table* tbl =(Table*) p;
141+ int col_number =tbl-> num_col;
146+int sqlite3_get_data_type(void* p,const char* tbl_name, int nRow, int nCol){
148+ DBinfo* dbi =(DBinfo*) p;
151+ int rc,i, val, counter;
152+ const char* char_int;
155+ strcpy(sql_query_buffer, "select * from ");
156+ strcat(sql_query_buffer, tbl_name);
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");
163+ rc = sqlite3_step(stmt);
165+ while (rc == SQLITE_ROW && counter < nRow){
167+ rc = sqlite3_step(stmt);
172+ val =sqlite3_column_type(stmt, nCol);
173+ printf("Type : %d\n", val );
175+ rc =sqlite3_finalize(stmt);
176+ if( rc != SQLITE_OK){
177+ printf("Couldnt finalize:\n");
184+int sqlite3_get_val_int(void* p,const char* tbl_name, int nRow, int nCol){
187+ DBinfo* dbi =(DBinfo*) p;
190+ int rc,i, val, counter;
191+ const char* char_int;
194+ strcpy(sql_query_buffer, "select * from ");
195+ strcat(sql_query_buffer, tbl_name);
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");
202+ rc = sqlite3_step(stmt);
204+ while (rc == SQLITE_ROW && counter < nRow){
206+ rc = sqlite3_step(stmt);
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");
223+const unsigned char* sqlite3_get_val_text(void* p,const char* tbl_name,int nRow, int nCol){
226+ DBinfo* dbi =(DBinfo*) p;
229+ int rc,i, val, counter;
230+ const unsigned char* text_val;
233+ strcpy(sql_query_buffer, "select * from ");
234+ strcat(sql_query_buffer, tbl_name);
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");
241+ rc = sqlite3_step(stmt);
243+ while (rc == SQLITE_ROW && counter < nRow){
246+ rc = sqlite3_step(stmt);
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");
260+float sqlite3_get_float(void* p,const char* tbl_name, int nRow, int nCol){
262+ DBinfo* dbi =(DBinfo*) p;
265+ int rc,i, val, counter;
270+ strcpy(sql_query_buffer, "select * from ");
271+ strcat(sql_query_buffer, tbl_name);
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");
278+ rc = sqlite3_step(stmt);
280+ while (rc == SQLITE_ROW && counter < nRow){
283+ rc = sqlite3_step(stmt);
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");
299+void sqlite3_free_table_idr(void* db){
300+ Table* tbl =(Table*) db;
301+ sqlite3_free_table(tbl->table_data);
306+int sqlite3_step_idr(void* db){
308+ DBinfo* dbi =(DBinfo*) db;
309+ int rc =sqlite3_step(dbi->ppStmt);
314+int sqlite3_bind_int_idr(void* p,int index, int val){
316+ DBinfo* dbi =(DBinfo*) p;
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");
325+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
326+ printf("\nCould not step (execute) stmt.\n");
332+int sqlite3_bind_float_idr(void* p,int index, float val){
334+ DBinfo* dbi =(DBinfo*) p;
336+ double res =(float)val;
338+ rc =sqlite3_bind_double(dbi->ppStmt,index,res);
340+ if(rc != SQLITE_OK){
341+ printf("\nCould not bind float.\n");
345+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
346+ printf("\nCould not step (execute) stmt.\n");
352+int sqlite3_bind_null_idr(void* p,int index){
354+ DBinfo* dbi =(DBinfo*) p;
356+ rc =sqlite3_bind_null(dbi->ppStmt,index);
357+ if(rc != SQLITE_OK){
358+ printf("\nCould not bind null.\n");
361+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
362+ printf("\nCould not step (execute) stmt.\n");
368+int sqlite3_bind_text_idr(void* p,const char* text, int index,int length){
370+ DBinfo* dbi =(DBinfo*) p;
372+ rc =sqlite3_bind_text(dbi->ppStmt,index,text,length,SQLITE_STATIC);
373+ if(rc != SQLITE_OK){
374+ printf("\nCould not bind null.\n");
377+ if (sqlite3_step(dbi->ppStmt) != SQLITE_DONE) {
378+ printf("\nCould not step (execute) stmt.\n");
384+int sqlite3_column_count_idr(void* db, const char* tbl_name){
385+ DBinfo* dbi =(DBinfo*) db;
390+ strcpy(sql_query_buffer, "select * from ");
391+ strcat(sql_query_buffer, tbl_name);
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");
398+ printf("Prepare successful %d\n", rc);
400+ rc =sqlite3_column_count(stmt);
402+ fprintf(stderr, "SQL column count error\n");
405+ printf("Column count successful = %d\n", rc);
406+ sqlite3_finalize(stmt);
408+ // rc = actual column count
413+int sqlite3_data_count_idr(void* db){
415+ DBinfo* dbi =(DBinfo*) db;
416+ int rc = sqlite3_data_count(dbi->ppStmt);
422+int sqlite3_finalize_idr(void* db){
424+ DBinfo* dbi=(DBinfo*) db;
425+ int rc =sqlite3_finalize(dbi->ppStmt);
429+int sqlite3_complete_idr(const char *sql){
431+ int rc = sqlite3_complete(sql);
436+int sqlite3_reset_idr(void* db){
438+ DBinfo* dbi=(DBinfo*) db;
439+ int rc = sqlite3_reset(dbi-> ppStmt);
444+const char *sqlite3_column_name_idr(void* db, int N){
446+ DBinfo* dbi=(DBinfo*) db;
447+ const char *name = sqlite3_column_name(dbi->ppStmt, N);
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);
461+int sqlite3_column_bytes_idr(void* db, int n){
463+ DBinfo* dbi=(DBinfo*) db;
464+ int res = sqlite3_column_bytes(dbi->ppStmt, n);
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);
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);
483+int sqlite3_column_int_idr(void* db, int iCol){
484+ DBinfo* dbi=(DBinfo*) db;
485+ int res =sqlite3_column_int(dbi-> ppStmt, iCol);
491+void* sqlite3_backup_init_idr(void* pDest,
492+ const char *zDestName,
494+ const char *zSourceName
497+ DBinfo* dbi=(DBinfo*) pDest;
498+ DBbackup* dbi2=(DBbackup*) pSource;
500+ void* res = sqlite3_backup_init(dbi->db_ptr,zDestName,
501+ dbi2->source_ptr,zSourceName);
504+ printf("Error number in initializing backup : %d\n", sqlite3_errcode(dbi->db_ptr));
513+int sqlite3_backup_step_idr(void *backup, int nPage){
515+ DBbackup* dbi=(DBbackup*) backup;
516+ int res = sqlite3_backup_step(dbi->backup, nPage);
518+ if( res == SQLITE_OK){
519+ printf("Backup Success\n");
525+int sqlite3_backup_finish_idr(void *backup){
527+ DBbackup* dbi=(DBbackup*) backup;
528+ int res = sqlite3_backup_finish(dbi->backup);
534+int sqlite3_backup_remaining_idr(void *backup){
536+ DBbackup* dbi=(DBbackup*) backup;
537+ int res = sqlite3_backup_remaining(dbi->backup);
542+int sqlite3_backup_pagecount_idr(void *backup){
544+ DBbackup* dbi=(DBbackup*) backup;
545+ int res =sqlite3_backup_pagecount(dbi-> backup);
550+int sqlite3_value_bytes_idr(void* value){
552+ DBinfo* dbi=(DBinfo*) value;
553+ int res = sqlite3_value_bytes(dbi->value);
558+int sqlite3_value_bytes16_idr(void* value){
560+ DBinfo* dbi=(DBinfo*) value;
561+ int res =sqlite3_value_bytes16(dbi->value);