I have a column in sqlite3 table. Column has values like
1 ½”
1 ¾”
2 ½” etc.
Column has VARCHAR datatype.
I am using this code.
pref_HoseDiameter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
Now, when I am fetching these values from database, I am getting pref_HoseDiameter string values like this:
1 1/2"
1 3/4"
2 1/2"
How to fetch those values as they are in database or how to convert them that look like database values.
Help would be appreciated.
Thanks in advance.
Try to use sqlite3_column_text16 to get the string as UTF-16, then create the string with +stringWithCharacters:.
const UInt16* text = sqlite3_column_text16(statement, column);
int text_len = // find the first 0x0000 in text;
NSString* str = [NSString stringWithCharacters:text length:text_len];
Related
I am doing an iPad project, there are 3 tables in UI. if i select row in each table. Sqlite query will take the value from table and run the query. please find my Query below.
NSString * str3=tableFou.string4;
NSString * str4=tableFiv.string5;
NSString * str2=tablethr.string3;
sqlite3 *database;
favorite=[[NSMutableArray alloc]init];
if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
NSString *sql=[NSString stringWithFormat:#"Create view prizeview as SELECT country.name,item_country.id,text.text,substr(text,1,2) FROM country,item_country,prize,item_prize,gender,item_gender,text WHERE country.name = '%#' AND text.item = item_country.item AND country.id = item_country.id AND prize.name = '%#' and text.item=item_prize.item and prize.id = item_prize.id and gender.name = '%#' and text.item=item_gender.item and gender.id = item_gender.id ",str3,str4,str2];
const char *sqlStatement = [sql UTF8String];;
problem is that, the query is running if i select all the tables only.. but i want results if i select even one table. how can i do that?
Try with if condition use with and condition and pass all your str in if condition only.
use your if condition like this.
if(([propertyTypeLabel.text length]==0) || ([cityLabel.text length]==0 ) ||( [locationLabel.text length]==0 )|| ([priceLabel.text length] ==0)||([postedLabel.text length]==0))
In this like any of text is zero condition true you also use if ant of the table cell is tap your query run.
You can't search for empty fields unless the field is actually empty. F.ex. if you search
select * from mytable where username = "";
You won't find it, but if you are ok with the field being empty or whatever result, you must use like instead of =
select * from mytable where username like "%";
or better yet, you can create your query like this, lets say you have three fields to search for, username, firstname and lastname. The code below needs a few more if's and buts, but you should be able to make this work and only add the parameters to where if they fulfill the requirements.
NSMutableString *query = [[NSMutableString alloc] init];
[query appendString:#"select * from table where "];
if (username.text.lenghth >1) [query appendString:[NSString stringWithFormat:#"username = '%#'"]];
if (firstname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& firstname = '%#'"]];
if (lastname.text.length >1) [query appendString:[NSString stringWithFormat:#"&& lastname = '%#'"]];
Hope this helps
How to update table column having this:
/var/mobile/233KKFSDK3234/Documents/Page.jpg
and replace it with
/Documents/Page.jpg
in SQLite?
Note: All text, except /Documents/ is dynamic.
You have to read first the string and put it in to an nsstring and then generate newstring form the old one and then update in to db.
Here you have the objective c code for generate the new string. The db update you can doit with the other answers.
NSString *originalString = #"/var/mobile/233KKFSDK3234/Documents/Page.jpg";
NSRange documentsRage = [originalString rangeOfString:#"/Documents/"];
NSString *newString = [NSString stringWithString:[originalString substringWithRange:NSMakeRange(documentsRage.location, [originalString length]-documentsRage.location)]];
NSLog(#"newString:%#",newString);
UPDATE table SET column = IF(
POSITION('/Documents/' IN column),
RIGHT(column, (LENGTH(column) - POSITION('/Documents/' IN column) - LENGTH('/Documents/') + 1)),
column
)
If the pattern is found, only keep characters right of the occurrence; else, do nothing (keep old value).
Following worked for me:
UPDATE yourtable
SET yourfield = SUBSTR(yourfield,18);
..as stated by Bjoern on this question:
How to Update this data using SQL?
Do the string processing in ObjC and then Use this query.
update tablename set columnname = 'processedString' ;
Updated as per your requirement
I am trying to delete some entries in my sqlite database on my iPhone app, but am getting a weird error.
Here is my code:
if(sqlite3_open([databasePath UTF8String], &yerocDB)==SQLITE_OK)
{
sqlite3_stmt *compiledstatement;
NSString *deleteSql=[NSString stringWithFormat: #"delete from Favorites_Table where playlist_name = Studying and date = 1/1/2012"];
const char *sqlstmt = [deleteSql UTF8String];
if(sqlite3_prepare_v2(yerocDB, sqlstmt, -1, &compiledstatement, NULL)==SQLITE_OK)
{
int result = sqlite3_step(compiledstatement);
if(SQLITE_DONE != result)
NSAssert1(0,#"Error while creating delete statement => %s",sqlite3_errmsg(yerocDB) );
}else{
NSLog(#"didn't delete error: %s", sqlite3_errmsg(yerocDB));
}
sqlite3_finalize(compiledstatement);
}
but then I get the error:
didn't delete error: no such column: Studying
playlist_name and date are my columns...Why is it saying the "Studying" is not a column?
You need to wrap Studying in single quotes. And your date.
This:
delete from Favorites_Table
where playlist_name = Studying and date = 1/1/2012
Should be this:
delete from Favorites_Table
where playlist_name = 'Studying' and date = '2012-01-01'
The reason is that if you don't put it in single quotes, the parser will think it's a column name. In your first query, you were trying to delete from Favorites_Table where the playlist_name column equalled the Studying column. Henceforth your error, "No such column."
And once you fixed the quotes around studying, your date was going to throw an error, too. Dates use ISO format (yyyy-mm-dd) to compare. Don't use the localized mm/dd/yyyy or dd/mm/yyyy formats, as a rule of thumb.
I have a method which is supposed to return a PatientDetails object when I insert an unique nric as the parameter. This doesn't work. However when I changed this method to accept int age instead of NSString * nric, it worked.
Is there something wrong with my syntax "WHERE ic = %s" cause it seems wierd. I have googled 2 days on this and cannot find a solution.
Please help as I am a newbie.
- (PatientDetails *)patientDetails:(NSString *)nric {
PatientDetails *retval = nil;
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %s" , nric];
sqlite3_stmt *statement;
%s denotes the standard C string. In order to use NSString objects as the argument, you need to use the object notation %#.
WHERE ic = '%#'
Yes, the format specifier for Objective-C strings is %#. So it should be,
NSString *query = [NSString stringWithFormat:#"SELECT ic, district, age, race, referralSource, DMRelatedAdmin, postalCode FROM patientInfo WHERE ic = %#" , nric];
in my table of sqlite database, there are fields named id,firstName,middle, lastName
now i want to run the query as following
MyQuery = [NSString stringWithFormat:#"select id,(firstName || middlename || lastname) as Name from Students where firstname like '%#' or middlename like '%#' or lastname like '%#'",searchBarString,searchBarString,searchBarString];
above query work well. but i want to add a space between first and middle name(also middle and last name). so how can i perform this task? please suggest
MyQuery = [NSString stringWithFormat:#"select id,(firstName || ' ' || middlename || ' ' || lastname) as Name from Students where firstname like '%#' or middlename like '%#' or lastname like '%#'",searchBarString,searchBarString,searchBarString];
Try this!
You could simply return the three columns in separate variables and then join them when displaying them:
NSString *lastName = ...;
NSString *middleName = ...;
NSString *firstName = ...;
NSString *joined = [NSString stringWithFormat:#"%# %# %#",lastName,middleName,firstName];
This has the advantage that you can sort by any of the three columns in the UI without making new sqlite queries and allow the user to edit these.