Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 4, 2013, 12:10 PM   #1
whitedragon101
macrumors 6502a
 
Join Date: Sep 2008
sqlite drop table command

I have a function which is supposed to drop the table I'm working on so I can start again. It looks like this :

Code:
- (void) dropData
{
    
    sqlite3_stmt    *statement;
    char *errMsg;
    
    NSString *sql_stmt = @"DROP TABLE THOUGHT_RECORD";
    
    const char *drop_stmt = [sql_stmt UTF8String];
    sqlite3_prepare_v2(thoughtDB, drop_stmt,
                       -1, &statement, NULL);
    
    
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status = @"Didn't Drop table";
        NSLog(@"Didn't Drop table");
    } else
    {
        NSLog(@"Dropped table");
        
    }
    sqlite3_finalize(statement);
    sqlite3_close(thoughtDB);

    
}
The console outputs - " Dropped table"
However, if i then print out the table it is still there and so is all the data.

Any ideas? Is the above code not correct?
whitedragon101 is offline   0 Reply With Quote
Old Feb 4, 2013, 01:30 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
The logic of this code seems odd:
Code:
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status = @"Didn't Drop table";
        NSLog(@"Didn't Drop table");
    } else
    {
        NSLog(@"Dropped table");
        
    }
If the call indicates "Done", then the table WASN'T dropped, but if the call returns any other value, then it says the table WAS dropped. That seems either incomplete, backwards, or both.

What does the debugger say the value returned from sqlite3_step() really is? That may be a clue as to what's really happening.

If you don't know how to use the debugger, this would be a good time to learn it. Being able to step through statements one at a time is a powerful tool for seeing what really happens in code.
chown33 is offline   0 Reply With Quote
Old Feb 4, 2013, 02:03 PM   #3
waterskier2007
macrumors 68000
 
waterskier2007's Avatar
 
Join Date: Jun 2007
Location: White Lake, MI
Send a message via AIM to waterskier2007
Quote:
Originally Posted by whitedragon101 View Post
I have a function which is supposed to drop the table I'm working on so I can start again. It looks like this :

Code:
- (void) dropData
{
    
    sqlite3_stmt    *statement;
    char *errMsg;
    
    NSString *sql_stmt = @"DROP TABLE THOUGHT_RECORD";
    
    const char *drop_stmt = [sql_stmt UTF8String];
    sqlite3_prepare_v2(thoughtDB, drop_stmt,
                       -1, &statement, NULL);
    
    
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status = @"Didn't Drop table";
        NSLog(@"Didn't Drop table");
    } else
    {
        NSLog(@"Dropped table");
        
    }
    sqlite3_finalize(statement);
    sqlite3_close(thoughtDB);

    
}
The console outputs - " Dropped table"
However, if i then print out the table it is still there and so is all the data.

Any ideas? Is the above code not correct?
I believe that you need to use a different check than SQLITE_DONE. I can't remember off the top of my head but maybe SQLITE_OK instead. Done just returns whether or not it finished, so even if it finished with a bad result, it still might pop done

edit: see status codes here http://www.sqlite.org/c3ref/c_abort.html
__________________
2012 Mac Mini, 2.6 GHz, 16GB RAM, 1TB HDD
2.4Ghz 15" Macbook Pro
16 GB iPhone 5 : 32 GB iPhone 4S : 16 GB iPad 3
16 GB Nexus 7 (2012)
waterskier2007 is offline   0 Reply With Quote
Old Feb 4, 2013, 06:10 PM   #4
whitedragon101
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
Quote:
Originally Posted by chown33 View Post
The logic of this code seems odd:
Code:
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        status = @"Didn't Drop table";
        NSLog(@"Didn't Drop table");
    } else
    {
        NSLog(@"Dropped table");
        
    }
If the call indicates "Done", then the table WASN'T dropped, but if the call returns any other value, then it says the table WAS dropped. That seems either incomplete, backwards, or both.

What does the debugger say the value returned from sqlite3_step() really is? That may be a clue as to what's really happening.

If you don't know how to use the debugger, this would be a good time to learn it. Being able to step through statements one at a time is a powerful tool for seeing what really happens in code.
Whoops yep the NSlog commands are backwards.

The return value of
Code:
sqlite3_step(statement)
is 21 which is
Code:
#define SQLITE_MISUSE      21   /* Library used incorrectly */
So I think there is something wrong with this code. That its not the right way to drop a table. When I google I can't find anything other than just the sqlite command.
Code:
"DROP TABLE yourTableNameHere"
Any help appreciated
whitedragon101 is offline   0 Reply With Quote
Old Feb 4, 2013, 06:29 PM   #5
whitedragon101
Thread Starter
macrumors 6502a
 
Join Date: Sep 2008
Figured it out. It was missing :

Code:
    if (sqlite3_open(dbpath, &thoughtDB) == SQLITE_OK)
to open the db for editing

It should read

Code:
- (void) dropData{
    
  
    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];
    
    if (sqlite3_open(dbpath, &thoughtDB) == SQLITE_OK)
    {
    
    NSString *sql_stmt = @"DROP TABLE THOUGHT_RECORD";
    
    const char *drop_stmt = [sql_stmt UTF8String];
    sqlite3_prepare_v2(thoughtDB, drop_stmt,
                       -1, &statement, NULL);
        
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog(@"Dropped table");

    } else
    {
        status = @"Didn't Drop table";
        NSLog(@"Didn't Drop table");
        
    }
    sqlite3_finalize(statement);
    sqlite3_close(thoughtDB);

    }
}
whitedragon101 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Remap command q to command h and vice versa? TheArgonaut OS X Mavericks (10.9) 1 Jan 29, 2014 09:08 AM
General: iOS7 command line... Equivalent of "say" command? RobertoG Jailbreaks and iOS Hacks 0 Jan 19, 2014 05:05 PM
Slow SQLite Query Starfox Mac Programming 3 Sep 10, 2012 12:04 PM
Sqlite and Core Data kimbebot iPhone/iPad Programming 5 Jul 23, 2012 01:44 AM

Forum Jump

All times are GMT -5. The time now is 08:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC