iOS Insert Row with 2 Date Fields - ios

How does one insert a row into a table that I've created with two sqlserver smalldatetime fields via the windows azure mobile services library? I'm currently trying to use:
[NSDate timeIntervalSince1970]
to store the date-time value as an NSTimeInterval or double. However, this causes the following error:
Error Domain=com.Microsoft.WindowsAzureMobileServices.ErrorDomain Code=-1302 "Error: Bad request."

DateTime fields should be represented by an NSDate field in the NSDictionary object you are sending to insert or update call.
So you would just do:
[table insert:#{ #"id": #"myid", #"deliveryDate": [NSDate date] } completion:... ];
If you send an NSNumber instead, then you would see the -1302 error shown above.

Related

IOS/Objective-C: Testing date for null very slow

I have some code that is running very slow. I narrowed down the issue to the following lines. If commented out, the code runs rapidly. If present, this process can take 30 seconds or more although no error is reported by the compiler or at runtime.
I have a date in a temporary object as follows:
NSDate *lastedited = importObject.lastedited;
If I log this to the console it logs as:
2015-10-08 08:44:51 +0000
The above does not cause a delay
When I then go to save it in Core Data, I first check if it is null. However, the following code is what causes the extreme delay:
if (lastedited != (id)[NSNull null]){
[record setValue:lastedited forKey:#"lastedited"];
}
Edit
I have discovered that even taking out the test, the line [record setValue:lastedited forKey:#"lastedited"] runs extremely slowly.
This is despite the fact that lastedited in the entity is an NSDate in the data model.
And as the line at the top shows it is in the form of an NSDate and logs to the console as 2015-10-08 08:44:51 +0000
What might be causing the code to run so slowly?
Is there a reason you're not simply testing against nil?
if (lastedited != nil) {
....
}
Here's a good overview of the different kinds of nothing in Objective-C/Cocoa: http://nshipster.com/nil/
I think the issue is with the data type of lastedited field in your Coredata entity. The data type you are using is NSDate, which is of course not a primitive data type supported by Coredata. If the data type is not one of the primitive types (like int, float, double, String, NSTimeInterval), Coredata will first convert it into one of the primitive types. In your case, the date is converted to NSTimeInterval just before saving, which is a bulky process and takes more time than expected. So if you are doing the overhead of converting the NSDate to NSTimeInterval before saving, the process will become faster. Try this
[record setValue:([lastedited timeIntervalSince1970] * 1000) forKey:#"lastedited"];
Don't forget to change the data type of lastedited to NSTimeInterval in .h file of your entity.
#property (nullable, nonatomic, copy) NSTimeInterval *lastedited;
Keep in mind that you are saving the date as milliseconds to coredata now. So whenever you are fetching it from coredata, convert it to NSDate and use

Delete row of mySQL table from iOS app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm currently working on an iOS app that takes information from a mySQL database and outputs it to JSON which in turn is displayed in a UITableView. I would like to be able to delete a row of the UITableView, which would also delete the corresponding row in the database. I know I need to use this function:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[_wordsArray removeObjectAtIndex:indexPath.row];
[self.editTableView reloadData];
}
I'm not sure how it works, I know I'll have to use a PHP script in order for the row to be deleted from the database. What needs to be done on the iOS side? Can anyone point me in the right direction?
Thanks in advance.
What needs to be done on the iOS side?
This is a pretty broad question. You have to be more specific what you need help with.
Generally speaking this is what needs to be done which I am sure you already knew.
Delete the record from your array and reload the table. I see you have already done that.
I am assuming you are storing that array locally somewhere (NSUserDefaults, plist etc), otherwise the user will need to get latest data from your server on every app launch and make sure the delete row doesn't reappear
You need to make a Async call to your php on your server
You would need to pass in values like what info has the user decided to delete and then execute that delete in the database
If you need code samples then google it my man.
Doing the same, kinda, using a mySQL database to save my data and using arrays to access the tables on the iOS level. What I do for inserting, updating and deleting records from the database is something like this:
Using mySQL on a iOS Level ##
mySQL to iOS level: first is to create a method, or inline function, somewhere that is accessible anywhere in the app; inside a singleton for an example. This method will return an array that contains one record by passing a sqlite3_statement in the parameter list. This method looks something like:
example
/**
* From the parameter list, an array is produced in Entry format
* #param [in] statement incoming value from previous call sqlite3_step(..) == SQLITE_ROW
* #return NSArray: Entry format
*/
static inline NSMutableArray * SQLStatementRowIntoEntryEntry( sqlite3_stmt *statement) {
...
return [NSMutableArray arrayWithObjects: stringSubject, date dateCreated, stringBody, emotion, weatherCondition, temperature isBookmarked, [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: sqlite3_column_int( statement, SQL_ENTRIES_id)], #"id", nil], nil];
};
How I save id keys from the mySQL record is put it in a dictionary as the last object, so it kinda stays out of the way while I'm using the array on the iOS level. But then is there ready whenever I want to update or delete this record from the database.
Updating and Deleting Records from iOS level: so because the id was saved in the array for each record this makes it remarkably easy to update or delete records in the database. In your query you'd have something like:
NSString *sqlStatement = [NSString stringWithFormat: #"UPDATE Entries SET subject = \"%#\", date = \"%#\", dateCreated = \"%#\", body = \"%#\", emotion = %d, weatherCondition = %d, temperature = %d, isBookmarked = %d, diaryID = %d where id = %d;", [[arrayEntry objectEntry_subject] stringByReformatingForSQLQuries], [dateFormatter stringFromDate: [arrayEntry objectEntry_date]], [dateFormatter stringFromDate: [arrayEntry objectEntry_dateCreated]], [[arrayEntry objectEntry_body] stringByReformatingForSQLQuries], [arrayEntry objectEntry_emotion], [arrayEntry objectEntry_weatherCondition], [arrayEntry objectEntry_temperature], [arrayEntry objectEntry_isBookmarked], [[[[[arrayEntry optionsDictionary] objectForKey: #"diary"] optionsDictionary] objectForKey: #"id"] intValue], [[[arrayEntry lastObject] objectForKey: #"id"] intValue]];
What you want is the [[[arrayEntry lastObject] objectForKey: #"id"] intValue] to take place of .. where id = %d; to either update or delete a record.
I hope this helps and do ask for any questions, I copied and pasted code from my project m
You can pass the Database query string to your php Script.
NSString *sqlquery = [NSSring sringwithFormate : #"DELETE FROM tabel_name WHERE tabel_id='your_tabel_row_id'"];
In this tabel_id is the your database row id which is you want to delete and your_tabel_row_id is the identifier which help to you to delete the record from the database.

How to fetch date in iOS from string object

I have stored NSDate in the DB using http://www.appcoda.com/sqlite-database-ios-app-tutorial/.
When i fetch the date back to NSString i get following during debugging. (As mentioned in tutorial even though i have a date picker and store value in NSDate, its actually stored as string in DB finally.
(lldb) po _itemPurchaseDate
(
)
Attached screenshot. So how do i fetch date and present it in a ui label in iOS?

SUP error while sending date parameter to server

I am developing an iOS app using Sybase Unwired Platform 2.1.3 I am getting error while sending date parameter to database.
Here is my code,
#try
{
SUP105Sales_order *insertRow =[[SUP105Sales_order alloc]init];
int32_t idValue =2671;
int32_t custID =103;
int32_t sales =506;
insertRow.id_=idValue;
insertRow.cust_id=custID;
insertRow.order_date=[NSDate date];
insertRow.fin_code_id=#"r1";
insertRow.region=#"Hyd";
insertRow.sales_rep=sales;
[insertRow save];
[insertRow submitPending];
[SUP105SUP105DB synchronize];
}
#catch (NSException *exception) {
NSLog(#"Exception---%#",exception.description);
}
Server log,
2014-02-28 16:39:41.833 WARN Other Thread-182 [SUP105.server.SUP105DB]{"_op":"C","level":5,"code":412,"eisCode":"257","message":"com.sybase.jdbc3.jdbc.SybSQLException:SQL Anywhere Error -157: Cannot convert '' to a timestamp","component":"Sales_order","entityKey":"3210004","operation":"create","requestId":"3210005","timestamp":"2014-02-28 11:09:41.646","messageId":0,"_rc":0}
I think the error above is due to date format. I have tried few other ways to send date to the Sup server, but it did not work.
How to send DATE format to the sales_order table to the order_date column from iOS native app?
Appreciate your help.
The sybase doc says here
You cannot insert data into a timestamp column. You must either insert an explicit null by typing “NULL” in the column or use an implicit null by providing a column list that skips the timestamp column. Adaptive Server updates the timestamp value after each insert or update. See “Inserting data into specific columns” for more information.
In your case it seems that the value sent by your app is an empty string "" which is not allowed for a timestamp field. You can either skip the column in your insert request or set it to "NULL".
Could you try one those options.

Why can't I get this stringFromDate code to work?

The following code is supposed to format the datestamp from coredata entity "DIncome"
But I'm getting the following error, which I'm pretty sure has something to do with this code.
-"Incompatible pointer types initializing 'NSString *' with an expression of type 'NSDate *'"-
DIncome *dIncome = [dailyIncomeArray objectAtIndex:indexPath.row];
NSDateFormatter *formateDateString = [NSDateFormatter alloc];
[formateDateString setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *formattedDateString = [formateDateString stringFromDate:dIncome.datestamp];
NSLog(#"%#" "This is your date log", formattedDateString);
Thanks a bunch for your time and help!
One simple way to fix this issue is to change the type for your attribute datestamp from "date" to "string, You will need to format the date how you want it to display before you send it to your data model. Of course this doesn't explain why the datestamp was having an issue when you were pulling it back out of the database. It is in he same formate as when it went in.

Resources