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
}
Related
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
- (UserInfo*)getCurrentUserInfo:(NSString*)userName
{
UserInfo *userInfo = [[UserInfo alloc]init];
sqlite3 *database;
sqlite3_stmt *selectstmt;
NSLog(#"userName:%#",userName);
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *sqlString = [NSString stringWithFormat:#"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = '%#'" , userName];
NSLog(#"getCurrentUserInfo:%#",sqlString);
const char *SqlCommand = [sqlString UTF8String];
if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {
NSLog(#"success!");
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSLog(#"select result:%#",userInfoStr);
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close (database);
return userInfo;
}
the following is my log output:
2012-03-06 17:50:11.556 MagicWords[508:f803] userName:Tan
2012-03-06 17:50:11.557 MagicWords[508:f803] getCurrentUserInfo:SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = 'Tan'
It doesn't print "success",so sqlite3_prepare_v2 don't return yes.but my database is ok:
I can't find the problem?
Add sqlite error message . It will give an insight of what is going on inside. If all about your database is correct ,check the table name.
UserInfo *userInfo = [[UserInfo alloc]init];
sqlite3 *database;
sqlite3_stmt *selectstmt;
NSLog(#"userName:%#",userName);
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *sqlString = [NSString stringWithFormat:#"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = '%#'" , userName];
NSLog(#"getCurrentUserInfo:%#",sqlString);
const char *SqlCommand = [sqlString UTF8String];
if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {
NSLog(#"success!");
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSLog(#"select result:%#",userInfoStr);
}
}
//Add the error message here.
else
{
NSLog(#"%s",sqlite3_errmsg(database));
}
///////
sqlite3_finalize(selectstmt);
}
sqlite3_close (database);
return userInfo;
Remove the quotes inside ' ' and Use Semicolon at the end of the sqlString.
NSString *sqlString = [NSString stringWithFormat:#"SELECT USER_LEVEL FROM USER_INFO WHERE USER_NAME = %#;" , userName];
try this when you are converting sqlString to char
const char *SqlCommand = (char *)[sqlString
cStringUsingEncoding:NSUTF8StringEncoding];
I am using a database to populate a table view.
I created an NSMutable Array of object in the AppDelegate. The Array is populated from the DB as follows:
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select barcode, vintage, vineyard, varietal from wine";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while(sqlite3_step(statement) == SQLITE_ROW) {
NSString *barcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
DBHelper *wineObj = [[DBHelper alloc] initWithBarcode:barcode];
wineObj.vintage = sqlite3_column_int(statement, 1);
wineObj.vineyard = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
wineObj.varietal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
[appDelegate.wineList addObject:wineObj];
[wineObj release];
}
}
}
All of the data goes into the array correctly with barcode being of type NSString *. However, when the table view is displayed, the barcode turns into "Variable Not a CFString." When I select an item, I am using:
//get the bottle details for selected object
DBHelper *tempObj = [appDelegate.wineList objectAtIndex:indexPath.row];
DBHelper getBottleDetails:[appDelegate getDBPath]:tempObj.barcode];
Because the barcode is not longer being interpreted as an NSString, the app crashes. How do I ensure that the barcode remains valid to use?
-(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