Thanks in advance for taking your time out for reading and hoping to assist me. I am relatively new to coding and the current problem I am facing is;
I have built a sqlite3 database, with three columns which are respectively stored as; string, float and an int. When trying to read and represent the columns, I am having trouble in finding a method in order to represent the float and int values.
The three properties which are stores in the database are set as;
@property (nonatomic, strong) NSString *name;
@property (nonaomtic, strong) NSNumber *price;
@property (nonaomtic, strong) NSNumber *quantity;
I'm not sure if maybe changing the latter two properties into float and int would make a difference?
However, the main confusion for me lies in the following code I have made in order to call on the data base;
-(void)readDataFromDatabase{
[self.stock removeAllObjects];
sqlite3 *database;
if (sqlite_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
char *sqlStatement = "select * from entries";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
while(sqlite3_step(compiledStatement) == SQLITE_ROW{
char *n = sqlite3_column_text(compiledStatement, 1);
char *p = sqlite3_column_int(compiledStatement, 2);
chat *q = sqlite3_column_int(compiledStatement, 3);
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSString stringWithUTF8String:p];
NSNumber *quantity = [NSString stringWithUTF8String:q];
Data *data = [[Data alloc] initWithData:name thePrice: price theQuantity:quantity];
[self.stock addObject:data];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
My common sense tells me the problem lies in this section;
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSString stringWithUTF8String:p];
NSNumber *quantity = [NSString stringWithUTF8String:q];
I'm not sure how I should approach the NSNumber? I'm aware the NSString stringWithUTF8String is not correct, but I don't know the equivalent for NSNumber.
I would really appreciate any insight and help on this matter.
Thank you!
You are on the right track with what you've mentioned for float
and int
. Float
does not need to be referenced like NSString
because it's size does not vary between 32-bit and 64-bit systems. If your integer is larger then I would recommened using NSInteger
for that value:
char *n = sqlite3_column_text(compiledStatement, 1);
float p = sqlite3_column_text(compiledStatement, 2);
int q = sqlite3_column_text(compiledStatement, 3);
NSString *name = [NSString stringWithUTF8String:n];
float price = p;
NSInteger quantity = q;
Note: You don't the *
for the float or int values, since they are not pointers.
You might even be able just to do (If you are already declaring the properties in your header):
name = [NSString stringWithUTF8String:sqlite3_column_text(compiledStatement, 1)];
price = sqlite3_column_text(compiledStatement, 2);
quantity = sqlite3_column_text(compiledStatement, 3);
To check the values you can use NSLog
:
NSLog(@"%@ %.02f %li", name, price, quantity);
For completeness sake, if you wanted to make the float
and int
into NSNumber
:
NSString *name = [NSString stringWithUTF8String:n];
NSNumber *price = [NSNumber numberWithFloat:p];
NSNumber *quantity = [NSNumber numberWithInt:q];
With the properties in your header declared:
name = [NSString stringWithUTF8String:n];
value = [NSNumber numberWithFloat:p];
quantity = [NSNumber numberWithInt:q];
NSLog(@"%@ %@.02f %@", name, price, quantity);
There are multiple ways to get your result, and that's what makes Objective-C quite flexible.