Delete row of mySQL table from iOS app [closed] - ios

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.

Related

NSString key for value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"UserName"#"Password"]];
When I am running this code I am getting an error. Is this code correct?
The problem in your code is you have written %# twice and supplied only single value, that is the main reason behind error.
In NSDictionary you can get only one value at a the time using valueForKey.
Use this code
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"Password"],[params valueForKey:#"UserName"]];
In NSDictionary Get only one value using valueForKey.
For Getting Password access like [params valueForKey:#"Password"]
For UserName use syntax like [params valueForKey:#"UserName"]
Can't get both value same time use like [params valueForKey:#"UserName"#"Password"]
Below is sample code:
NSString *userUpdate = [NSString stringWithFormat:#"grant_type=password&Password=%#&UserName=%#&SchoolId=1",[params valueForKey:#"Password"],[params valueForKey:#"UserName"]];

iOS App Migration - Update an old native app to Cordova, migrate data

I was wondering if anyone can give any insight into the data migration I would like to have while upgrading my app. I am updating an app that was written in native objective C, to a cordova based web-view using jquery mobile.
The existing app has "favourites" that is implemented using sqllite3.h. I was wondering if it is possible that when we post the update to the app, we can hook into this existing sqlite database, and migrate the old "favourites" into the new database, which is implemeted using localStorage.
Any insight is greatly appreciated!
We just did something similar to this. Our old native app was using CoreData. We added a check in MainViewController, in the webViewDidFinishLoad delegate method, to see if an existing database was there. If it was, then we loaded the database, converted the contents for use via localStorage and then set a flag so that it would not try to import it again. Here's a code segment that transferred it into localStorage. First we put the info we wanted to transfer into a NSMutableString, but formatted it as a javascript dictionary
NSMutableString *dict = [NSMutableString string];
[dict appendString:#"{"];
[dict appendFormat:#"'notes':'%#'", notes];
[dict appendFormat:#",'date':'%f'",seconds];
[dict appendFormat:#",'count':'%d'",[ss.count intValue]];
[dict appendFormat:#",'weather':'%#'",wx];
[dict appendFormat:#",'location':'%#'",ss.event.location.name];
[dict appendFormat:#",'latitude':'%#'",[ss.event.location.latitude stringValue]];
[dict appendFormat:#",'longitude':'%#'",[ss.event.location.longitude stringValue]];
[dict appendString:#"}"];
and then we passed it into the javascript like so:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"importMyOldData(%#);",dict]];
This effectively calls the javascript method, and passes a dictionary of items into it. Then you can just take that and put it into local storage using Javascript, for example:
function importMyOldData( oldData )
{
// if you want to inspect each item
var notes = oldData.notes;
localStorage.setItem( "NoteKey", notes );
// if you want to dump the whole thing
localStorage.setItem( "MyKey", JSON.stringify(oldData) );
}

Access one specific element of NSMutableDictionary getting null values in iOS

I have an NSMutableDictionary that loads data from a certain source.
He is loading fine and while debugging i am sure he is loading and that in the end he has the exact number of elements. I also can see the elements as they should while debugging.
I just want to get the value of the the row having the key value = 3 .
I tried NSString * myString = [myMutabDict objectForKey:#"3"], considering that the value is in string ,and i followed it with the debugger, and i am sure that my NSDictionary has elements in it , and i can see them while debugging, and i can see the key 3 , but i still get null as an output …
What am i missing?
If you are sure you see a value with a key of 3 and you can't load it using the key #"3" then it is possible that the key is a number. Use:
NSString *myString = [myMutableDict objectForKey:#3];
or modern syntax:
NSString *myString = myMutableDict[#3];

Looking for code to 'set current key' when saving data --iOS App--

I recently posted a question regarding some issues I'm having saving data into text fields for an app I'm building in Xcode. The question can be seen here,
Having issues saving data from multiple text fields in Xcode
I received a very helpful answer which helped clue me in to exactly where I was going wrong, but I'm still unsure about the code needed to set the current key when specifying which field to save data from.
Here is the code:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (![self.tField.text isEqualToString:#""]) {
//SET THE CURRENT KEY HERE
[Data setNoteForCurrentKey:self.tField.text];
}
else {
//SET THE CURRENT KEY HERE
[Data removeNoteForKey:[Data getCurrentKey]];
}
if (![self.tField2.text isEqualToString:#""]) {
//SET THE CURRENT KEY HERE
[Data setNoteForCurrentKey:self.tField2.text];
}
else {
//SET THE CURRENT KEY HERE
[Data removeNoteForKey:[Data getCurrentKey]];
}
[Data saveNotes];
}
I added a second detail item property to enable the second text field to be saved, but don't know what code to use to call upon the different detail items. Any help will be greatly appreciated, thanks!
setNoteForCurrentKey: should take two parameters, not one. Then you can pass the key and the value associated with it. Something like:
- (void)setNote:(NSString *)note forKey:(NSString *)key
Using a 'current key' just makes maintenance of that information an issue that you don't need.
In your current code, because you have 2 if statements, you can set the keys as literal values in the code. That isn't very scalable, so going forwards you might want to think about storing your text fields in an array and having another array with the corresponding key names.

Using SQLite with iOS... very beginner program

I am attempting to write an application in which I have two text fields that I will be using for input. The input taken from the text fields are being initialized as a string. So what I want to accomplish is:
Create an SQLite database (file) that will be saved within the Xcode project (I'm not sure where it can be saved to, but I would need to be able to read it and write to it).
I then want to place the strings (from the text field inputs) into the SQLite table, which will only have two fields (for simplicity). I'm using a button to accept the input from the text fields, place the input into strings, and then put the value of the strings into a label(s).
I ultimately will want to place the strings from the text fields into the table, and then read the table to "grab" the strings and display them into the labels. All of this would be done by the click of a button.
I realize this is very specific, but I have been having a difficult time finding anything simple to accomplish this. I am very new to iOS development and I appreciate if anyone could be as specific and detailed as possible, or at least point me to some resources that would teach me how to accomplish this.
I am using Xcode v4.3.2 and I do not use Storyboard or ARC. I tried to be as detailed as possible, but as you can, my goal is really simple.
I'm tapped out and I appreciate all the help I can get.
Thanks!
-Matt
creating a database file: simply drop your data.sqlite file in your xcode project, as you drop any other class or image resource. Please note, that you will have to copy the file to writeable directory after app installation. take a look at createeditablecopyofdatabaseifneeded.
open your database with sqlite3_open with respect to your new database location.
query the database as you like with sqlite3_prepare_v2
a simple query snippet looks like:
NSString *querystring;
// create your statement
querystring= [NSString stringWithFormat:#"SELECT text1, text2 FROM datatable;"];
const char *sql = [querystring UTF8String];
NSString *text1 = nil;
NSString *text2 = nil;
if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK){
NSLog(#"sql problem occured with: %s", sql);
NSLog(#"%s", sqlite3_errmsg(db));
}
else
{
// you could handle multiple rows here
while (sqlite3_step(statement) == SQLITE_ROW) {
text1 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
text2 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
} // while
}
sqlite3_finalize(statement);
// go on with putting data where you want
There is an example project for using SQLite here you can refer to: https://github.com/AaronBratcher/ABSQLite
It has classes for accessing SQLite in a more traditional database way. The sample project uses SQL to create the tables and shows how you can change the schema over different versions of your app.

Resources