How to retrieve a particular column from a database in iphone - 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);
}

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.

My code snippet is leaking

The below code snippet leaks when i am trying the build and analyse thing.
What is the problem in this code , pls let me know
- ( NSString *) getSubCategoryTitle:(NSString*)dbPath:(NSString*)ID{
NSString *subCategoryTitle;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat: #"select sub_category_name from sub_categories where id = %#",ID];
NSLog(#"%# I am creashes here", 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)
{
subCategoryTitle = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 0)];
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
return [subCategoryTitle autorelease];
}
You allocate instance into subCategoryTitle in a loop, but don't release the previous allocation.
subCategoryTitle = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(selectstmt, 0)];
Either (auto)release it, or directly go to the last row, and avoid this while, as it doesn't make much sense.
Example for creating only last object:
char * col_text = NULL;
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
col_text = sqlite3_column_text(selectstmt, 0);
}
if (col_text != NULL)
{
subCategoryTitle = [[NSString alloc] initWithUTF8String:col_text];
}

how to retrieve the multiple columns data from the database?

I am new to this database programming.i retrieved single column data from the database.but i am unable to retrieve multiple columns data from the database.
here is my code
-(void) readItemsFromDatabaseforTable:(NSString *)tableName {
arrayItems = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSString *sql_str = [NSString stringWithFormat:#"select * from %#", tableName];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"query %s",sqlStatement);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *idsstr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
[arrayItems addObject:idsstr];
NSLog(#"*************** %#",arrayItems);
}
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *caloriesstr = [NSString stringWithUTF8String:
(char*)sqlite3_column_text(compiledStatement, 4)];
[caloriesarry addObject:caloriesstr];
NSLog(#"naveenkumartesting");
NSLog(#"*************** %#",caloriesarry);
}
}
my main aim is to retrieve columns data from the database and store into multiple arrays.
please guys anyone help me,
You dont need to loop with while a second time when you can get all data in a single while as so:
const char *stmt = "select id, name from table1 where isImage = 1";
sqlite3_stmt *selectstmt;
if (sqlite3_prepare_v2(database, stmt, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
int rowid = sqlite3_column_int(selectstmt, 0);
NSString * name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
}
}
Use this sample Query and make your query according to your requirements..
NSString *str = [NSString stringWithFormat:#"Select * from Yourtablename where image1 = '%#' AND person1 = '%#' AND category = '%#' AND PurchaseAmt = '%#'",Str,person1,category,Amountdata];
Use this tutorial

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.

Query is not retrieving the data from table

im trying to integrate a database into my project.This is for first time im implementing a database project.I integrated the database into my project.Database has 3 columns named rowid,col1,col2.Below code i used for acessing the datafrom database.But it is not enetering in "if loop"Can anyone help me where im doing wrong in this case.Thanks in advance.![enter image description here][1]
/* -(void)print
{
sqlite3_stmt *statement;
qsql=[NSString stringWithFormat:#"SELECT * from light where rowid = '%d'",1];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
// tv.text=qsql;
}
}}*/
-(void)print
{
sqlite3_stmt *statement;
//qsql=[NSString stringWithFormat:#"SELECT * from light where rowid = %d",1];
qsql=[NSString stringWithFormat:#"SELECT * from light where rowid = %i",1];
//const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
// NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%i",sqlite3_column_int(statement, 0)]];
// tv.text=qsql;
}
}
}
-(void) checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
-(void) openDB {
databaseName = #"Lighthr.sqlite";
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentsPaths objectAtIndex:0];
databasePath = [documentDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
NSLog(#"coming here???");
}
}
[1]: http://i.stack.imgur.com/lm1ZV.png
because rowid is always integer and you are trying to access using string.
I thing you have to try this.
Edit
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
qsql=[NSString stringWithFormat:#"SELECT * from light where rowid = %i",1];
const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
// tv.text=qsql;
}
}
}
Are you trying to test without your "where" clause ?
And what is the error code return by sqlite3_prepare_v2 ?
int sql_answer = sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL);
NSLog(#" SQL answer : %d",sql_answer );
if(sql_answer == SQLITE_OK) {
...
i think you should do in this way .
-(void)print
{
sqlite3_stmt *statement;
qsql=[NSString stringWithFormat:#"SELECT * from light where rowid = %d",1];
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"hhhhhhhhh");
while (sqlite3_step(statement) == (SQLITE_ROW)){
NSString *Q_NO =[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]
}
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
above stameny replace with below statement
NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%i",sqlite3_column_int(statement, 0)]];
and
const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
now replace [qsql UTF8String] with qsql_stament. It will work fine.