batuman batuman - 1 year ago 67
iOS Question

How to get/delete multiple rows in SQLITE table in iOS?

My table in SQLITE is in the following format.

id name date height
1 tt 12-2-15 120
2 ss 15-3-15 110
3 tt 14-5-15 120
10 tt 19-6-15 130

(1)I like to get all rows under name tt in one instruction, then put into two dimensional array. How can i implement it?

(2)How to delete all rows under name tt in one instruction?

My code is in iOS. I couldn't find solution easily.

Just to share what I implement.

NSString *databasePath;
NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent: @"RECORDS.db"]];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath,& RECORDS) == SQLITE_OK)
retval = [[NSMutableArray alloc] init];

NSString *query = [NSString stringWithFormat:@"SELECT id, nameid, date, height FROM RECORD WHERE nameid=\"%@\"", currentNameID];
const char *query_stmt = [query UTF8String];
if (sqlite3_prepare_v2(RECORDS, query_stmt, -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *date = (char *) sqlite3_column_text(statement, 1);
int height = (int) sqlite3_column_text(statement, 2);
NSString *name = [[NSString alloc] initWithUTF8String:date];
HeightInfo *info = [[HeightInfo alloc] initWithInfos:name withheight:(int)height];
[retval addObject:info];



Answer Source

Do you need help with the SQL or the ios framework?
The SQL is pretty straightforward;

select id, name, date, height from mytablename where name = "tt";

delete from mytablename where name == "tt";         

Practice the commands on the command line so you see how they work.

For processing your data look at the great examples by Ray Wenderlich;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download