Sqlite3 database is not executing a query - ios

I am developing an iPhone app in Xcode, I am unable to access the database I have created. Following is the code where I am creating my database
//create local database
NSString *docsDir;
NSArray *dirPaths;
sqlite3 *localDB;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"local.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO) {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &localDB) == SQLITE_OK) {
char *errMsg;
const char *sql_stmt1 = "CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT)";
const char *sql_stmt2 = "CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC)";
const char *sql_stmt3 = "CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)";
if (sqlite3_exec(localDB, sql_stmt1, NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(#"%#",#"Failed to create table My_choices");
}
if (sqlite3_exec(localDB, sql_stmt2, NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(#"%#",#"Failed to create table ratings");
}
if (sqlite3_exec(localDB, sql_stmt3, NULL, NULL, &errMsg) != SQLITE_OK) {
NSLog(#"%#",#"Failed to create table Consultant_Details");
}
sqlite3_close(localDB);
}
else {
NSLog(#"%#",#"Failed to open/create database");
}
}
add below is the code where I am trying to access it
NSString *docsDir;
NSArray *dirPaths;
sqlite3 *localDB;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
NSString *dbPath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: #"local.db"]];
NSString *q = #"select * from Ratings where perfume_id in";
q = [q stringByAppendingString:idList];
NSLog(#"%#",q);
const char *sqlStatement2 = [q UTF8String];
sqlite3_stmt *compiledStatement2;
if(sqlite3_open([dbPath UTF8String], &localDB) == SQLITE_OK) {
if(sqlite3_prepare_v2(localDB, sqlStatement2, -1, &compiledStatement2, NULL) == SQLITE_OK) {
NSLog(#"%d", SQLITE_ROW);
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement2) == SQLITE_ROW) {
// Read the data from the result row
int r = sqlite3_column_int(compiledStatement2, 1);
int id = sqlite3_column_int(compiledStatement2, 0);
NSLog(#"%d", id);
for (ListItem *item in self.listItemArray) {
if(item.perfumeId == id){
item.rating = r;
}
}
}
}
else{
NSLog(#"query failed: %s", sqlite3_errmsg(localDB));
}
}
sqlite3_close(localDB);
I always get "query failed No such table exists:Ratings" in my log.
Any help will be appreciated.

Basically this error occured due to unavailabilty of Table in database or wrong query. According to your post, IT look like a error in query.
Query should be something like this:
NSString *q = #"select * from Ratings where perfume_id in ('12','13','14','15')";
Print your "String q" and make sure it follow as above.

Thanks everyone for your help.The database file with the same already existed at that path. So my first method where I was creating a database
if ([filemgr fileExistsAtPath: databasePath ] == NO) check was false and tables were not created.

first remove database file from document directory then use following code to create a sqlite table..
if (![fileManager fileExistsAtPath: _databasePath ])
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_myDataBase) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS My_choices (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, na TEXT, got_it TEXT);" //TEST
"CREATE TABLE IF NOT EXISTS Ratings (perfume_id INTEGER PRIMARY KEY AUTOINCREMENT, rating NUMERIC);" //USER
"CREATE TABLE IF NOT EXISTS Consultant_Details (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,website TEXT,domain TEXT,domain_no NUMERIC,phone TEXT,facebook TEXT)"; //METADATA
if (sqlite3_exec(_myDataBase, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create tables");
}
sqlite3_close(_myDataBase);
NSLog(#"DataBase created at: %#",_databasePath);
} else {
NSLog(#"Failed to open/create database");
}
}

Related

Can not read data from the sqlite in iOS

I checked from sqlitebrowser that I successfully created sqlite and inserted data to sqlite, but now my problem is I could not read data from sqlite. I am new to sqlite.
Should I open the sqlite first, and read it? Where is wrong on my code below?
This code for open the existing sqlite.
-(void)openSqlite{
NSString *docsDir = [NSString stringWithFormat:#"Questiondata.db"];
NSArray *dirPaths;
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:#"Questiondata.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath:_databasePath] == NO) {
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
char *errorMessage;
const char *sql_statement ="CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB,sql_statement,NULL,NULL,&errorMessage) != SQLITE_OK) {
NSLog(#"Failed to create the table");
}
sqlite3_close(_contactDB);
}
else{
NSLog(#"Fail to open/create the table");
}
}
}
This is code for read data from existing sqlite.
-(void)readData{
NSLog(#"we came here");
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB)) {
NSString *querySQL = [NSString stringWithFormat:#"SELECT * FROM Questions WHERE QuestionNumber = 1"];
const char*query_statement = [querySQL UTF8String];
if (sqlite3_prepare_v2(_contactDB, query_statement, -1, &statement, NULL)== SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
NSString *addressfield = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
NSLog(#"finddata:%#",addressfield);
}else{
NSLog(#"notfinddatabase");
}
sqlite3_finalize(statement);
}else{
NSLog(#"failed to serache database");
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
NSLog(#"not be here");
}
}
In openSqlite method
const char *sql_statement ="CREATE TABLE IF NOT EXISTS users (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
and in your query
NSString *querySQL = [NSString stringWithFormat:#"SELECT * FROM Questions WHERE QuestionNumber = 1"];
table name and column name are not same.
Change in Place "Questions" with "users" .
and also change "QuestionNumber" with the table column.
May be, it will helps you or feel free.
sqlite doesn't report all errors until you start accessing the data. For example just about any filename and path will pass without errors.
First of all you must check that, the database is in the Documents folder?
It's not there by default.
Perhaps it's in the main bundle?
Try this:
NSString *dbName = [[NSBundle mainBundle] pathForResource:#"dbFile" ofType:#"db"];

Data not inserting into sqlite database

Hi in my application I have data which want insert into my sqlite database .So I have already created the database successfully, But the problem its not insert data into database.
My database creation code.
- (void)createDatabase
{
NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doctumentsDirectory = [directories lastObject];
self.databasePath = [[NSString alloc] initWithString:[doctumentsDirectory stringByAppendingPathComponent:#"/bp.sqlite"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
// create DB if it does not already exists
if (![fileManager fileExistsAtPath:self.databasePath]) {
const char *dbPath = [self.databasePath UTF8String];
if (sqlite3_open(dbPath, &_myDataBase) == SQLITE_OK) {
char *errorMsg;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS br (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, COST TEXT,QUTY TEXT)";
if (sqlite3_exec(_myDataBase, sql_statement, NULL, NULL, &errorMsg) != SQLITE_OK) {
[self errorCreatingTable:[NSString stringWithFormat:#"failed creating table. ERROR:%s", errorMsg]];
}
sqlite3_close(_myDataBase);
} else {
[self errorCreatingTable:#"failed openning / creating table"];
}
}
}
My insert code i have UIButton action method where i give my insert code.
-(void)aMethod2:(UIButton*)sender
{
NSString *inr=[intarr objectAtIndex:sender.tag];
NSLog(#"%#",inr);
NSString *item=[self.menuarray objectAtIndex:sender.tag];
NSLog(#"%#",item);
NSString *cost=[self.menuarray1 objectAtIndex:sender.tag];
NSLog(#"%#",cost);
NSString *sql = [NSString stringWithFormat:#"INSERT INTO br ('NAME','COST','QUTY') VALUES ('%#','%#','%#')",item,cost,inr];
NSLog(#"%#",sql);
}
I have used the above code where get the data and inserting into my database but values all passing correctly problem is its not inserting into my database please tell me how to resolve this issue I have been stuck here for long time help me out.
Thanks.
When i saw your question, i couldn't help seeing something that was a bit strange to me... and that was seeing my own code. thanks for using my code snippet from stackoverflow :) getting error while trying to create sqlite database 'Could not create table' IOS7
Any way, here is a quick example on how to insert:
- (void)insertDictionaryToDataBase:(NSDictionary *)dictionary
{
if (dictionary) {
sqlite3_stmt *sql_statement;
const char *dbPath = [self.databasePath UTF8String];
if (sqlite3_open(dbPath, &_inboxDatabase) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO MY_TABLE_NAME (COLUMN_A, COLUMN_B, COLUMN_C) VALUES (?,?,?)"];
const char *insertStatement = [insertSQL UTF8String];
sqlite3_prepare_v2(_inboxDatabase, insertStatement, -1, &sql_statement, NULL);
sqlite3_bind_text(sql_statement, 1, [dictionary[#"key1"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(sql_statement, 2, [dictionary[#"key2"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(sql_statement, 3, [dictionary[#"key3"] intValue]);
if (sqlite3_step(sql_statement) == SQLITE_DONE) {
NSLog(#"sucess!");
}
sqlite3_finalize(sql_statement);
sqlite3_close(_inboxDatabase);
}
}
}
DBmanager.h file
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#interface DBReadWriteUtil : NSObject
+(sqlite3*)copyDbToDocumentsDirectoryIfNotExist;
+(BOOL)insertData:(NSString*)arg1 cost:(int)arg2 quty:(int)arg3;
+(NSDictionary*)getUploadPendingMedia;
+(int)removeMediaPOStFormId:(int)row_id;
#end
DBmanager.m file
#import "DBReadWriteUtil.h"
#implementation DBReadWriteUtil
+(sqlite3*)copyDbToDocumentsDirectoryIfNotExist{
NSString *dbPath = [NSTemporaryDirectory() stringByAppendingPathComponent:#"bp.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dbPath]) {
NSString *localDB = [[NSBundle mainBundle] pathForResource:#"bp" ofType:#"sqlite"];
NSError *err;
if(![fileManager copyItemAtPath:localDB toPath:dbPath error:&err]){
if (DEBUG_MODE) {
NSLog(#"SQL: Not ABLE TO COPY FILE -> %#",err);
}
}
}
sqlite3 *db_connction;
if(sqlite3_open([dbPath UTF8String], &db_connction) != SQLITE_OK){
if (DEBUG_MODE) {
NSLog(#"SQL : Not ABLE TO CONNECT DB:");
}
}
return db_connction;
}
+(BOOL)insertData:(NSString*)arg1 cost:(int)arg2 quty:(int)arg3{
BOOL dbStatus = NO;
sqlite3 *db_connction = [DBReadWriteUtil copyDbToDocumentsDirectoryIfNotExist];
sqlite3_stmt *compiledStmt;
const char *query = "insert into br(NAME,cost,quay) values(?,?,?)";
if(sqlite3_prepare_v2(db_connction, query, -1, &compiledStmt, NULL) == SQLITE_OK ){
sqlite3_bind_text(compiledStmt,1,[arg1 UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_int(compiledStmt, 2, arg2);
sqlite3_bind_int(compiledStmt,3, arg3,-1);
NSUInteger err = sqlite3_step(compiledStmt);
if (err != SQLITE_DONE){
if(DEBUG_MODE){
NSLog(#"error while Inserting image %d %s",err, sqlite3_errmsg(db_connction));
}
}else{
dbStatus = YES;
}
}
sqlite3_finalize(compiledStmt);
sqlite3_close(db_connction);
return dbStatus;
}
Call DBmanager
BOOL successDB = [DBReadWriteUtil insertData: arg1 cost:arg2 quty:arg3];
try this code...
-(void)Insertdata:(NSString*)query{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:#"bp.sqlite"];
NSLog(#"%#",databasePath);
if(sqlite3_open([databasePath UTF8String],&_myDataBase) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"%#",query];
char *errmsg=nil;
if(sqlite3_exec(_myDataBase, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(#".. Inserted ..");
}
}
sqlite3_close(_myDataBase);
}

Add table to existing database IOS sqlite [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am using SQLite DB in my iOS app. In my screen 1, I was successfully able to create a Database, a table, insert into it and retrieve from it.
However from screen 2 when I am trying to create a table in the same database and insert values, I am unable to.
This is the code which I am using.
-(void) createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
#"coning.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS order (ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT UNIQUE , ADDRESS TEXT UNIQUE, PHONE TEXT UNIQUE)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create table");
}
sqlite3_close(_contactDB);
} else {
NSLog(#"Failed to open/create database");
}
}
}
- (void)saveData {
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT OR REPLACE INTO order (name, address, phone) VALUES (\"%#\", \"%#\", \"%#\")",
#"name", #"address", #"phone"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"added");
} else {
NSLog(#"Failed to add contact");
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
I have been using the same code to add entries in table 1 but when I do the same to create table 2, its says 'Failed to add contact'. Can someone suggest where I might be going wrong?
Also I want to make the PK of table 1 as a FK in table 2.
You can use sqlite3_exec() method instead of sqlite3_step().
sqlite3_exec() will execute whatever the query you have given.
I am sure, It will definitely help you.
-(BOOL)createNewTableInExistingDb
{
NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath=[array objectAtIndex:0];
filePath =[filePath stringByAppendingPathComponent:#"database.db"];
NSFileManager *manager=[NSFileManager defaultManager];
BOOL success = NO;
if ([manager fileExistsAtPath:filePath])
{
success =YES;
}
if (!success)
{
NSString *path2=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"database.db"];
success =[manager copyItemAtPath:path2 toPath:filePath error:nil];
}
createStmt = nil;
NSString *tableName=#"SecondTable";
if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
if (createStmt == nil) {
NSString *query=[NSString stringWithFormat:#"create table %#(RegNo integer, name text)",tableName];
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &createStmt, NULL) != SQLITE_OK) {
return NO;
}
sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);
return YES;
}
}
return YES;
}

Communication of the database

I am working with the communication of sqlite database from one viewcontroller to another, but some how i am not getting the database on the second view controller. Bellow is the code which i am using for it.:-
On First View controller
//Creating a table of MOtivational Thoughts
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPath objectAtIndex:0];
Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"S.H.E.D_DB"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([ filemgr fileExistsAtPath: Second.databasePath] == NO) {
const char *dbpath = [Second.databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS THOUGHTS (ID integer ,Motivation_Thaought TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create table");
}
else
{
NSLog(#" created table");
}
sqlite3_close(contactDB);
} else {
}
}
// Fetching Data from table of MOtivational Thoughts
if(sqlite3_open([Second.databasePath UTF8String], &contactDB) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
int randomNumber = [self getRandomNumberBetween:0 to:6];
NSString *queryString = [NSString stringWithFormat:#"Select * FROM THOUGHTS where ID= '%d'",randomNumber];
const char* sql = [queryString UTF8String];
NSLog(#"path value : %#", Second.databasePath);
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, nil)==SQLITE_OK) {
// Loop through the results and add them to the feeds array
NSLog(#"Ready to enter while loop");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSLog(#"reading");
NSString *aid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
motivationView.text = aid;
NSLog(#"Thought of the day %#", aid);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(contactDB);
// Do any additional setup after loading the view from its nib.
}
On Second View Controller
NSLog(#"path value : %#", databasePath);
if(sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK){
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS SHEDSLIST (SHED_ID integer ,SHED_Name TEXT ,SHED_Description TEXT, SHED_TIME DATETIME)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(#"Failed to create table");
}
else
{
NSLog(#" created table");
}
sqlite3_close(contactDB);
} else {
}
PLease locate my error and give me sutable solution.
May be you should access the database path as self.databasePath in SecondViewController because I believe you have created a property for that. Instead of that you are trying to access some instance variable databasePath. Look at your following code in FirstViewController:
Second.databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:#"S.H.E.D_DB"]];

How to iterate through sqlite_step result

Could someone be so kind as to fill in the few missing pieces of code below. I am struggling with executing an sqlite statement using sqlite3_step, iterating through each row of results and putting the data into a useable format.
Here is the code that creates the database (Database is only two colums)
// Check if database is setup, if not create it
NSString *documents_directory;
NSArray *directory_path;
// Get the documents directory
directory_path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documents_directory = [directory_path objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [documents_directory stringByAppendingPathComponent: #"weights.db"]];
NSFileManager *file_manager = [NSFileManager defaultManager];
// Create database if it doesn't already exist
if([file_manager fileExistsAtPath: databasePath ] == NO){
const char *database_path = [databasePath UTF8String];
if(sqlite3_open(database_path, &contactDB) == SQLITE_OK){
char *error_message;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS RECORDED_WEIGHTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, WEIGHT TEXT, TIME TEXT)";
if(sqlite3_exec(contactDB, sql_statement, NULL, NULL, &error_message) != SQLITE_OK){
status.text = #"Failed to create table";
}
sqlite3_close(contactDB);
}else{
status.text = #"Failed to open/create database";
}
}
And here is the code I am trying to complete;
// Get info from database and load it.
const char *database_path = [databasePath UTF8String];
sqlite3_stmt *statement;
if(sqlite3_open(database_path, &contactDB) == SQLITE_OK){
NSString *SQLquery = [NSString stringWithFormat:#"SELECT * FROM RECORDED_WEIGHTS ORDER BY TIME DESC"];
const char *query_statement = [SQLquery UTF8String];
if(sqlite3_prepare_v2(contactDB, query_statement, -1, &statement, NULL) == SQLITE_OK){
// Please help me execute statement and get results into useable format here.
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
Thanks in advance.
You want to do something like this:
while(sqlite3_step(statement) == SQLITE_ROW)
{
// int value
int intValue = sqlite3_column_int(statement,fieldIndex);
// string value
NSString* stringValue;
if (sqlite3_column_type(dbps, fieldIndex) != SQLITE_NULL)
{
const char *c = (const char *)sqlite3_column_text(dbps, fieldIndex);
if (c)
{
stringValue = #(c);
}
}
... etc...
}
Look at the documents for the different column types. You can also look at different objective-c wrappers out there to get an idea of how to access the database.

Resources