-(void) readProductsFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the animals Array
products = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(#"db opened");
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "SELECT DISTINCT productname FROM iphone ";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSLog(#"inside sqlite3 prepare");
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
My problem is
const char *sqlStatement = "SELECT DISTINCT productname FROM iphone ";
This line not executing ,i am using sqlite3,
thanks in advance,
problem is in this step
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
instead of 2 use 0
Related
how to check at the start of the app wether there is data in sqlite or not if data is available in data base then it should upload other wise normally work the application
i will upload this data using post and using php for to upload data to server.
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select response_Id,question_id,answer_option,answer_text,update_date_time from survey_question_responses";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.question_Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.answer_text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)];
coffeeObj.update_date_time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
int count=[appDelegate.coffeeArray count];
NSLog(#"count is beofore getting values %d",count);
[appDelegate.coffeeArray addObject:coffeeObj];
int countone=[appDelegate.coffeeArray count];
NSLog(#"count is after getting values %d",countone);
[coffeeObj release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
You can get number of rows present into the database by:
SELECT COUNT(*) FROM table;
if it returns 0 then your table is empty.
//update...................
const char *sqlQuery="Select count(*)from yourTable";
if(sqlite3_prepare_v2(database, sqlQuery, -1, &queryStatement, NULL)==SQLITE_OK)
{
while (sqlite3_step(queryStatement)==SQLITE_ROW)
{
const char *count=(const char *)sqlite3_column_text(queryStatement, 0);
NSString *rowCount=[NSString stringWithCString:count encoding:NSASCIIStringEncoding];
//here you will get the number of rows in string format;
}
}
I have this code in my viewWillAppear method:
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
}
sqlite3_stmt *statement;
//why is this if statement failing?
if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
-1, &statement, nil) == SQLITE_OK) {
It passes the first if statement without entering (which is good). The 2nd if statement is the problem.
The sqlStatement is in the form of SELECT * FROM food WHERE foodType = 'fruit'
I don't understand why it's not getting into the if statement. Any help would be appreciated.
The problem ended up not being in the code, but in the way that I exported the sqlite file. It was a series on INSERT statements, not an actual table.
and make changes according to ur requirement...
-(void) readDataFromDatabase
{
chapterAry = [[NSMutableArray alloc] init];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:#"QuotesApp.sqlite"];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement ="select * from MsExcelTutorial";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement)==SQLITE_ROW)
{
NSNumber *chapterId = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 0)];
NSString *chapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *link = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *bookmark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:chapterId,#"chapterId",chapter,#"chapter",link,#"link",bookmark,#"bookmark", nil];
[chapterAry addObject:dic];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
NSString *DBPath = [ClsCommonFunctions GetDatabasePath];
if ([self OpenDBWithPath:DBPath])//Method to open database connection
{
//filesDB is database object
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select fileID,filePath from tblFiles where isUploadPending =1";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(filesDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// u Need to get data from database...
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
sqlite3_close(filesDB);
}
go through it it will work
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
i am running a method for containing select query, but compiled statement is not working the break point skips the line where it declares it and when i puts the cursor on it it shows no value, Here's the code i am using:
-(NSMutableArray *)GetAllPartsName
{
NSString *path = [self getDBPath];
// Open the database from the users filessytem
NSMutableArray *Parts=[[[NSMutableArray alloc]init]autorelease ];
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *sqlQuery = [NSString stringWithFormat:#"SELECT Parts_Name FROM Parts"];
NSLog(#"%#",sqlQuery);
const char *sqlStatement = [sqlQuery UTF8String];
//The break point skips the sqlite3_stmt.
sqlite3_stmt *compiledStatement;
//when i put the cursor over compiledStatement it shows no value
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSMutableDictionary *PositionDict=[[NSMutableDictionary alloc]init];
//setting the parts into dictionary
[PositionDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)] forKey:#"Parts_Name"];
[Parts addObject:PositionDict];
NSLog(#"%#",PositionDict);
[PositionDict release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return Parts;
}
Try FMDattabase (https://github.com/ccgus/fmdb)
BASIC:
FMDatabase *_db = [[FMDatabase databaseWithPath:[[self class] databaseFilePath]] retain];
[_db open];
NSMutableArray *emails = [[NSMutableArray alloc] init];
NSString * query = [NSString stringWithFormat:#"SELECT `email` FROM `email`"];
FMResultSet * result = [_db executeQuery:query];
while ([result next])
{
[emails addObject:[result stringForColumn:#"email"]];
}
return [emails autorelease];
Can any one help me .How can a fetch the remote data using WEB API's or SERVER API's with of JSON & then store into SQLite table in iphone how can do this .For fetching data I want to use JSON.In SQLite database table is user_detailes which has username & password .And the server also has the same data which i want fetch.
Help me to this.
Give some hints or links or sample to solve this
NSString * txtUsername = #"";
NSString * txtPassword = #"";
sqlite3 *database;
NSString *sqlStatement = #"";
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
sqlStatement = [NSString stringWithString:#"select * from account_info "];
NSLog(#"%#",sqlStatement);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
txtUsername = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)] retain];
txtPassword = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] retain];
}
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
if(txtUsername isEqulaToString:#"user1"])
{
//use UIAlertView here
}