dots, commas etc get converted into a special symbol "?" in iphone - iphone

i'm using a database to fetch datas. there are almost 750 questions in database.some questions contain special character's like ",....,' etc. but while fetch the data and print it in a textview it get converted into "?". is there any way to remove these kind of symbols and print it in original format.
I used the below code for fetching and displaying in textview.
qsql=[NSString stringWithFormat:#"Select * from mydata where col_1 = '365'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == (SQLITE_ROW))
{
char *field0 = (char *)sqlite3_column_text(statement, 1);
char *field1 = (char *)sqlite3_column_text(statement, 2);
NSString *t1 = nil;
if(field0!=NULL){
t1 = [[NSString alloc] initWithUTF8String:field0];
//NSString *t1 = [[NSString alloc] initWithCString:field0 encoding:NSUTF8StringEncoding];
NSLog(#"%#jujuj" ,t1);
}
NSString *t2 = nil;
if(field1!=NULL){
t2 = [[NSString alloc] initWithUTF8String:field1];
NSLog(#"%#jhjhh" ,t2);
}
NSString *string=t1;
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#",."];
string = [[string componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:#""];
NSLog(#"%#jhgkjghkjg",string);
//NSLog(#"%# %#",Q_NO, Q_NO1);
tv.text=t1;
tv1.text=t2;
[t1 release];
[t2 release];
}
}

Use NSCharacterSet to remove that special Character from you string
NSString *string = #"ram,...";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#",."];
string = [[string componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:#""];
NSLog(#"%#",string);

use like this
const char *sqlStatement = Select * from mydata where col_1 = '365'";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
nsstring *t1 = [NSString stringWithFormat:#"%s",(char*)sqlite3_column_text(compiledStatement, 1)];

Related

how to use login valid value select in database.the first value select login proprely but second value select login not proprely

- (BOOL)addloginprofile:(NSString *)username andPassword:(NSString *)password
{
//XYZAppDelegate *appDelegate = (XYZAppDelegate *)[UIApplication sharedApplication].delegate;
//const char *sql = "select Name='%#',EnterPassword='%#' from ProfileTable";
NSString *sql = [[NSString alloc] initWithFormat:#"select Name='%#',EnterPassword='%#' from ProfileTable",username,password];
//NSString *sql = [NSString stringWithFormat:#"select Name='%#',EnterPassword='%#' from ProfileTable",username,password];
NSLog(#"sql statment %#",sql);
sqlite3_stmt *statment;
if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &statment, NULL) == SQLITE_OK)
//if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &statment, NULL) == SQLITE_OK)
{
if(sqlite3_step(statment) == SQLITE_ROW)
{
NSString *username=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statment,0)];
NSString *password=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment,1)];
// NSString *username = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statment, 1)];
// NSString *password=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statment, 2)];
NSLog(#"User name %#,",username);
NSLog(#"password %#",password);
if ([username isEqualToString:#"0"]&&[password isEqualToString:#"0"]) {
return NO;
}
else return YES;
}
}
I think the query is not properly formated it should be like this #"select * from ProfileTable where Name='%#' and EnterPassword='%#'",username,password.

How to print all data in given table?

i am iPhone application developer, and now we understood the concept of database.
i want to print all data from database. but i m not getting how can i print all data. here is i pest some code. please give me correct direction to print all data..
for example in sql we print all data as "select * from contact5;" we fire this string. can we done in iPhone coding?
-(IBAction)PrintData:(id)sender
{
NSLog(#"Button Pressed");
sqlite3_stmt *statement1;
NSString *querySQL=#"SELECT * FROM CONTACT5";
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement1, NULL);
while (sqlite3_step(statement1) == SQLITE_ROW)
{
NSLog(#"Enter in the denger zone");
NSString *idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 0)];
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 2)];
NSString *NameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 1)];
NSString *phoneFiels = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 3)];
NSLog(#"ID is=%#",idField);
NSLog(#"Name is=%#",NameField);
NSLog(#"Address is=%#",addressField);
NSLog(#"Phone No. is=%#",phoneFiels);
[idField release];
[NameField release];
[phoneFiels release];
[addressField release];
}
sqlite3_finalize(statement1);
sqlite3_close(contactDB);
}
thanks in advance.
We are sharing Some come which is using NSMutableDictionary to store your data and you can easily use that code to print your data,where do you want
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "Select * from CalculateTime";
sqlite3_stmt *insertStmt;
if(sqlite3_prepare_v2(database, sql, -1, &insertStmt, NULL) != SQLITE_OK)
NSAssert1(0,#"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));
arrayCountry = nil;
arrayCountry = [[NSMutableArray alloc]init];
arrayCity = nil;
arrayCity = [[NSMutableArray alloc]init];
arrayFlag = nil;
arrayFlag = [[NSMutableArray alloc]init];
arrayZone = nil;
arrayZone = [[NSMutableArray alloc]init];
NSString *str2;
while(sqlite3_step(insertStmt)==SQLITE_ROW)
{
char *row;
row = (char*)sqlite3_column_text(insertStmt, 0);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayCountry addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 1);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayCity addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 2);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayFlag addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 3);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayZone addObject:str2];
}
}
}
Access these dictionary data to print anywhere..

How to list the resultset based on order using query

//===================================================================================
- ( NSMutableDictionary * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
//===================================================================================
NSMutableDictionary *aTierTwoTemplateData = [[NSMutableDictionary alloc]init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat: #"select * from sub_categories_reference scr inner join storyboard_sub_categories ssc on ssc.id = scr.sub_category_id inner join subcategory_order as sco on sco.sub_category_id = scr.sub_category_id where scr.main_category_id = %# and sco.main_category_id = %# order by sco.position asc",iD,iD];
NSLog(#"%#", selectSQL);
const char *sql_query_stmt = [selectSQL UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSString *aValue = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 6)];
NSString *aId = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 5)];
[aTierTwoTemplateData setObject:aId forKey:aValue];
[aValue release];
[aId release];
NSLog(#"%# %# ^^^^^^^^^^^^^^^^^^^^picker value id ", aValue, aId);
}
}
}
sqlite3_close(database);
return aTierTwoTemplateData;
}
I am able to get the resultset when i assign this to array , but it loses the order in which , i have stored in the dictionery.
Actually , i have stored the result set based on the position field .
When i assign the resultset into array , the order gets changed.
Please let me know how can i handle this situation.
This is not a duplicate, as i have a coulmn in the db as "position"
If you want to store data as key-value pair and maintain the order, then you can use combination of NSArray and NSDictionary.The same code will be:
//===================================================================================
- ( NSArray * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
//===================================================================================
NSMutableArray *aTierTwoTemplateData = [[NSMutableArray alloc]init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat: #"select * from sub_categories_reference scr inner join storyboard_sub_categories ssc on ssc.id = scr.sub_category_id inner join subcategory_order as sco on sco.sub_category_id = scr.sub_category_id where scr.main_category_id = %# and sco.main_category_id = %# order by sco.position asc",iD,iD];
NSLog(#"%#", selectSQL);
const char *sql_query_stmt = [selectSQL UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSString *aValue = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 6)];
NSString *aId = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 5)];
[aTierTwoTemplateData addObject:[NSDictionary dictionaryWithObject:aId forKey:aValue]];
[aValue release];
[aId release];
NSLog(#"%# %# ^^^^^^^^^^^^^^^^^^^^picker value id ", aValue, aId);
}
}
}
sqlite3_close(database);
return [aTierTwoTemplateData autorelease];
}
In this way you'll be having an array of dictionaries, where your values will be stored in the dictionary and the order of the data also will be preserved.

How to retrieve a particular column from a database in iphone

in my project im using a database.There are almost 365 questions in that.so i want to get a particulat question for a particular day.i used the code below to fetch a qusestion from database.
-(void)print{
sqlite3_stmt *statement;
// SELECT * from light where rowid = %i",1
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '365'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"%dsssssssssssssss",sqlite3_step(statement));
NSLog(#"%ddddddddddddddddddddd", (SQLITE_ROW));
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSLog(#"%# gg",Q_NO);
when i use the above code it is printing the correct question from database.Also when i give the statement like qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"]; it is not fetching the question.
But when i use the below code it is not fetching from the database.
-(void)print{
sqlite3_stmt *statement;
// SELECT * from light where rowid = %i",1
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"%dsssssssssssssss",sqlite3_step(statement));
NSLog(#"%ddddddddddddddddddddd", (SQLITE_ROW));
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSLog(#"%# gg",Q_NO);
while (sqlite3_step(statement) == (SQLITE_ROW))
{
NSLog(#" iolo");
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
//NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%i",sqlite3_column_int(statement, 0)]];
NSLog(#"%# gg",Q_NO);
}
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
}
Can anyone tell me where im going wrong.Thanks in advance.
-(void)print{
sqlite3_stmt *statement;
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == (SQLITE_ROW))
{
char *field0 = (char *)sqlite3_column_text(statement, 0);
char *field1 = (char *)sqlite3_column_text(statement, 1);
NSString *Q_NO = nil;
if(field0!=NULL){
Q_NO = [[NSString alloc] initWithUTF8String:field0];
}
NSString *Q_NO1 = nil;
if(field1!=NULL){
Q_NO1 = [[NSString alloc] initWithUTF8String:field1];
}
NSLog(#"%# %#",Q_NO, Q_NO1);
[Q_NO release];
[Q_NO1 release];
}
}
sqlite3_finalize(statement);
}

Selecting the values from the database through sqlite in iPhone

I am developing an iphone application using sqlite. In that, I have a method to retrieve the values from the table which is shown partially.
NSString *sqlQuery = [NSString stringWithFormat: #”select * from %#”, tableName];
If(sqlite3_prepare_v2(db, [sqlQuery UTF8STRING] , -1, &statement, NULL)== SQLITE_OK)
{
While(sqlite3_step(statement) == SQLITE_ROW)
{
}
Sqlite3_finalize(statement);
}
What my doubt is , inside the while loop we can get the values of the column through the index of the table like the following code.
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
address.text = addressField;
For one column we can retrieve like this. In my case, I don’t know the number of columns to be retrieved. In this case, how to iterate over the columns. Please help me out.
Thanks.
NSString *sqlQuery = [NSString stringWithFormat: #”select * from %#”, tableName];
If(sqlite3_prepare_v2(db, [sqlQuery UTF8STRING] , -1, &statement, NULL)== SQLITE_OK)
{
While(sqlite3_step(statement) == SQLITE_ROW)
{
int columnCount = YouKnowColumnCount;
NSMutableArray* array = [[NSMutableArray alloc]init];
for( int i=0; i<columnCount ; ++i) {
[array addObject:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, i)]];
}
Sqlite3_finalize(statement);
}
Something like this it depend what do you want to do ...