I added SQLite
to my xcode project. (I used this tutorial to implement it.) I have 3 tables. the last 2 are linked to the first table. I tried doing a for
loop to go through all the strings
in a NSDictionary
, then add the appropriate string
to its table.
For example. I have a persons name, phone number, and address in a dictionary
. I then have 3 tables. One for name, another for phone #; and a third for address.
The tables
phoneNumber and address are linked to name like this: INTEGER REFERENCES personalInfo(id)
. So phoneNumber
and address
will have multiple rows for every row of name
. It's basically a 1 to many relationship.
Here is the full code:
[personalInfo setString:@"insert into personalInfo (id, name, image, info1, info2, info3, info4, info5, info6, info7, info8) values (null"];
for (NSString *allKeys in keys)
{
int i = [allKeys intValue];
NSArray *tempArray = [self.inputText objectForKey:@(i)]; // inputText is an NSDictionary
if (i <= 6)
{
for (id string in tempArray) {
[personalInfo appendString:[NSString stringWithFormat:@", '%@'", string]];
}
if (i == 6) {
[personalInfo appendString:@")"];
[self.dbManager executeQuery:personalInfo];
}
}
else if (i > 6 && i < [self.rowArray count])
{
if ([tempArray count] == 1)
{
[address setString:[NSString stringWithFormat:@"insert into address (id, address1, address2, address3, personalInfoID) values (null, null, null, '%@', null)", [tempArrayfirstObject]]];
[self.dbManager executeQuery:address];
}
if ([tempArray count] == 2)
{
[address setString:[NSString stringWithFormat:@"insert into address (id, address1, address2, address3, personalInfoID) values (null, '%@', '%@', null, null)", [tempArrayfirstObject], [tempArray lastObject]]];
[self.dbManager executeQuery:address];
}
}
else if (i == [[self.inputText allKeys] count])
{
[homePhone setString:[NSString stringWithFormat:@"insert into homePhone (id, phone1, personalInfoID) values (null, '%@', null)", [tempArray firstObject]]];
[self.dbManager executeQuery:homePhone];
}
}
The problem is, when I make an nslog
of the second 2 tables, I don't get any results.
SQLite table:
CREATE TABLE personalInfo(
id INTEGER PRIMARY KEY,
name text,
image text,
info1 text,
info2 text,
info3 text,
info4 text,
info5 text,
info6 text,
info7 text,
info8 text
);
CREATE TABLE address(
id INTEGER PRIMARY KEY,
address1 text,
address2 text,
address3 text,
personalInfoID INTEGER REFERENCES personalInfo(id)
);
CREATE TABLE homePhone(
id INTEGER PRIMARY KEY,
phone1 text,
personalInfoID INTEGER REFERENCES personalInfo(id)
);
I know this is very confusing with a lot of code, so if you have any questions, please ask in the comments.
Aucun commentaire:
Enregistrer un commentaire