SQLite with Ansi C and xCode -
i'm starting bottom-up learn ipad development after 15 years cold fusion. i'm getting comfortable ansi c , xcode, stumped taking next step sqlite.
i've built database (airports.sqlite) razorsql , installed in same directory main.c i've installed amalgamated sqlite3.h , sqlite3.h files.
everything compiles ok, following message when run...
error in select statement select length runways order length desc limit 5 [no such table: runways].
the database has runways table in it. can set me straight? here's code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlite3.h" #include "weightbalance.h" sqlite3* db; int first_row; int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) { int i; int *p_rn = (int*)p_data; if (first_row) { first_row = 0; for(i=0; < num_fields; i++) { printf("%20s", p_col_names[i]); } printf("\n"); for(i=0; i< num_fields*20; i++) { printf("="); } printf("\n"); } (*p_rn)++; for(i=0; < num_fields; i++) { printf("%20s", p_fields[i]); } printf("\n"); return 0; } void select_stmt(const char* stmt) { char *errmsg; int ret; int nrecs = 0; first_row = 1; ret = sqlite3_exec(db, stmt, select_callback, &nrecs, &errmsg); if(ret!=sqlite_ok) { printf("error in select statement %s [%s].\n", stmt, errmsg); } else { printf("\n %d records returned.\n", nrecs); } } void sql_stmt(const char* stmt) { char *errmsg; int ret; ret = sqlite3_exec(db, stmt, 0, 0, &errmsg); if(ret != sqlite_ok) { printf("error in statement: %s [%s].\n", stmt, errmsg); } } int main() { sqlite3_open("airports.sqlite", &db); if(db == 0) { printf("could not open database."); return 1; } printf("\nselecting airports longest runways.\n\n"); select_stmt("select length runways order length desc limit 5"); sqlite3_close(db); return 0; }
most likely, file "airports.sqlite" opened in main() not 1 think is. without path information, sqlite3_open() open file in current working directory.
as debug step, add "printf(getwd(null))" before sqlite3_open() statement. you'll know whether you're opening existing database or creating new, empty 1 missing table.
also, since you're using xcode, can pass path database command-line parameter (argv). in xcode 4, choose product->edit scheme. in "run" section, add path "arguments pass on launch". can pass argv[1] sqlite3_open().
Comments
Post a Comment