Insert NSMutableDictionary into 1 cell of SQLite database - ios

I'm hoping someone here might be able to help me out.
I need to insert a NSMutableDictionary into one cell of a SQLite database. I am able to insert strings etc into the database, but when I try and insert a Dictionary I get a syntax error:
**Can't run query 'BEGIN TRANSACTION; UPDATE Database SET Column1 = {
Bad = "";
"End_Time" = 4;
Good = "";
Moderate = "";
Note = "";
} WHERE Title = Name; COMMIT TRANSACTION;' error message: unrecognized token: "{"**
To do this I am using the following code:
NSString *sql2 = [NSString stringWithFormat:#"UPDATE Database SET Column%# = %# WHERE Title = %#",previousQuestion,adictionary,Name];
Can anyone help or suggest a different approach,? I need to be able to store the key/values in 1 cell, as there will be 79 more cells with similar data and I need to reference each specific key from a specific column of the database.
I've tried turning the Dictionary into a string (and then I'd turn the string back to a dictionary on retrieval) But this causes the same issue.
Any suggestions?
Many thanks,
Andrew

Their is two way you can do this.
1st way :-
Archive your NSMutableDictionary convert it into NSData and store it in your Sqlite column which datatype should have be blob type. Archiving something like this,
NSData *theDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:yourDictionary];
Bind this data in sqlite,
sqlite3_bind_blob(addStmt, 5, theDictionaryData, -1, SQLITE_TRANSIENT);
Now retrieving time from sqlite,
NSData *retrieveData = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 4) length:sqlite3_column_bytes(selectstmt, 4)];
and finally convert it into NSDictionary,
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:retrieveData];
2nd way :-
Create numbers of column those are equal to numbers of keys of NSDictionary. Bring data from every key and save it respective table column.

You can't just insert an Objective-C object into the database. It must be a string, binary data, or a number. In converting it to a string, you also need to sanitize the input (so that it doesn't contain any invalid characters that SQL will interpret in another way). For example, if you tried to insert a string that was some SQL code, you wouldn't want it to try to execute that code. It looks like here it's getting hung up on a { in the string. You could instead convert the dictionary or the string to NSData and insert it into the database as binary data.

Related

Unable to check the text value in my table in sqlite

I have one value which looks like this "00123-23" ,I have no idea about the datatype to be used to store in the table. So I used Text data type to store this value.But When I try to check this value, the query is saying that there is no such value in my table, but actually there is a value. Here is my query:
NSString *x_accountNo;
x_accountNo=[_substrings objectAtIndex:2]; //x_accountNo=00123-23
NSString *query_newAccount = [NSString stringWithFormat:#"SELECT * FROM MYTABLE WHERE ACCOUNT=%# ",x_accountNo];
BOOL recordExist_newAccount = [self recordExistOrNot_newAccount:query_newAccount];
if (!recordExist_newAccount) {
nslog(#"no data");
}
Everytime I execute this statement, It is giving me no data . Could you please tell me what I am doing wrong here?
You might need to have string inside quotes:
SELECT * FROM MYTABLE WHERE ACCOUNT='%#'

Parse PFUser CurrentUser Returning Null

I save a few objects in the _User class in parse, such as displayname, gender, and bio. A simple user profile. I'm having difficulty retrieving them. I tried with many types of queries with no luck. So I figured if you could get the username by doing this:
self.username.text = [NSString stringWithFormat:#"#""%#",[[PFUsercurrentUser]valueForKey:#"username"]];
You should be able to get other objects that are stored in _User right? Well I tryed this to get the display name:
self.name.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"name"]];
And I get (null) printed out on the label, except in the data browser there is a value there. I'm really in need of some assistance this has been a problem that's taking me a while to figure it out. Thanks!
You have mentioned two lines in your code:-
//Have modified this line
In this line valueForKey you are passing #"username"
self.username.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"username"]];
In this line valueForKey you are passing #"name"
self.name.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"name"]];
Now in above two line both are having two separate keys username and name. So just check the valid key like that below if you are not sure which is valid, if below any of line prints value then use that key for setting the value in your label:-
NSLog(#"%#",[[PFUser currentUser]valueForKey:#"username"]);
NSLog(#"%#",[[PFUser currentUser]valueForKey:#"name"]);

How to check if NSDictionay key has a value efficiently when creating XML

I am creating some XML in objective C, I know how to do it however there is the possibility that there could be 800+ values I might be putting into XML, which I am getting from a NSArray of NSDictionaries.
So I was wondering if there is an efficient way of checking for nill or null in a keyvalue that's of type NSString.
Currently this is what my code looks like:
NSMutableArray *xmlItems = [coreDataController readInstallForXML:selectedInstallID];
for (int i = 0; i < [xmlItems count]; i++) {
NSDictionary *currentXMLItem = [xmlItems objectAtIndex:i];
[xmlWriter writeStartElement:#"Items"];
[xmlWriter writeAttribute:#"insID" value:[currentXMLItem valueForKey:#"insID"]];
// there are about another 20 attributes I have to add here.
}
// then write end elemtent etc.
In the code above I have no added any checking but I was hoping someone might have something better for me than adding a bunch of if statements for each attribute.
You can use [NSDictionary allKeysForObject:] to get all keys for the 'nil' values, so you have a list of keys to ignore.
Generating 800 items is not necessarily 'much' or 'slow'. You don't want to do that on the main thread anyway, so just make sure you perform it as a background operation.
use the allKeys method on the NSDictionary to return an NSArray of keys; then iterate through that array and for each key retrieve the value from the dictionary and use one if statement to check the string before writing out the xml element

how to encode NSData to string

I have a dictionary of NSData values where I am passing the different values into their variables that I later use to display in tableviewcells etc.
However I have some international characters that are not displaying correctly and I would like to encode the data I am passing to a NSString but I am not sure how to do it because of the circumstances.
This is what I am currently doing.
manString = [dict valueForKey:#"MAN"];
The dict contains all of the data that i am using. any help would be appreciated
You can do this:
NSString* string = [[NSString alloc] initWithData:[dict valueForKey:#"MAN"]
encoding:NSStringEncoding];
Where encoding should be a value from NSStringEncoding enum, you can find it in NSString.h, just choose encoding you need, usually it is NSUTF8StringEncoding, but I guess not in your case.

Integer from .plist

I'm new to plists, and I really need to use one. What I have is a plist where different numbers are stored, under two dictionaries. I need to get that number which is stored, and make it an integer. This process will be run from a method called 'readPlist.
The plist is called 'properties.plist'. The first dictionary is called 'Enemies'. It contains various other dictionaries, which will have the name stored in the NSMutableString called 'SpriteType'. The name of the number will have the format 'L - %d', with the %d being an integer called 'LevelNumber'.
If possible, can someone give me the code on how to get that integer using the information, and the names of dictionaries above.
I have looked around at how to access plists, but the code that people have shown doesn't work.
Thanks in advance.
EDIT: Too make it more understandable, this is my plist. What i want in an integer, called 'SpriteNumber' to be equal to the value of 'L - %d'
If you read the contents of your plist into a dictionary (I won't tell you how to do it, but this is the tutorial I refer to often), then it's a matter of getting the string out of the key for the level with [[myDictionary objectForKey:#"key"]stringValue];. Then, using of NSString's extremely helpful -stringByReplacingOccurencesOfString:withString: to get rid of the "L -" part and only get a numerical value. Finally, get an integer from the string with [myString intValue].
well, the easiest way would be something like :
-(int) getMosquitoCountForLevel:(int) level {
int mosquitoCount=0;
NSString *gsFile = #"whateverFullyQualifiedFileNameYourPlistIs";
NSDictionary* definitions = [NSDictionary dictionaryWithContentsOfFile:gsFile];
NSDictionary* mosquitos = [definitions objectForKey:#"Mosquito"];
if(mosquitos) {
NSString *levelKey = [NSString stringWithFormat:#"L - %d",level];
NSNumber *mosquitoCountAsNumber = [mosquitos objectForKey:levelKey];
if(mosquitoCountAsNumber) {
mosquitoCount=[mosquitoCountAsNumber intValue];
} else {
CCLOGERROR(#"%# - Mosquito definitions in %# does not contain a en entry for level %#.",self.class,gsFile,levelKey);
}
} else {
CCLOGERROR(#"%# - file %# does not contain a Mosquito dictionary.",self.class,gsFile);
}
return mosquitoCount;
}
this compiles but not tested with actual data.

Resources