2

I create one application and I read many data in table View from JSON and I want parsed this JSON and store in sqlite but I dont know from where should I start?

this is parsed my json code :

@implementation TableViewController
{
    NSArray *news;
    NSMutableData *data;
    NSString *title;
    NSMutableArray *all;
}
@synthesize mainTable;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"News";
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:@"http://zacandcatie.com/YouTube/json.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [con start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    news = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    for (int i =0; i < [news count]; i++)
    {
    NSIndexPath *indexPath = [self.mainTable indexPathForSelectedRow];
    title =[[news objectAtIndex:indexPath.row+i]objectForKey:@"title"];
        if (!all) {
            all = [NSMutableArray array];
        }
        [all addObject:title];
    }
    NSLog(@"%@",all);

    [mainTable reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The Connection has been LOST" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

you my json url. I want store "title"&"date_string" value in sqlite. please guide me!!!

7
  • You can use dictionary instead of sqlite db Commented Apr 29, 2013 at 8:51
  • ok I know But I dont know what I do? Commented Apr 29, 2013 at 8:53
  • I dont know how to store Dictionary in table on sqlite!!! Commented Apr 29, 2013 at 8:54
  • Here is an answered question of "[How can we store data into an NSDictionary?][1]" [1]: stackoverflow.com/questions/1760371/… Commented Apr 29, 2013 at 9:04
  • 1
    my friend my problem is that I dont know about sqlite data base and store data in it (not store dictionary) please guide me how to store nsdictionary in sqlite Commented Apr 29, 2013 at 9:15

5 Answers 5

1

After parsing you data in the form of NSDictionary you can create a query of insert into and fire the query n your data will be save into your database

Sign up to request clarification or add additional context in comments.

3 Comments

@fred create a method by which you just pass a dictionary to the method that will create a insert query along with the key and the value from the dictionary by using NSMutableString create a query.
my friend Im sorry can you tell me with code??? I do first part but in second part its my problem
i Have posted the code try this hope you will get the result.
1
-(void)InsertRecords:(NSMutableDictionary *)dict
{

sqlite3_stmt *stmt;
sqlite3 *cruddb;

NSMutableString *str = [NSMutableString stringWithFormat:@"Insert into tblName ("];

for (int i = 0; i<[[dict allKeys] count]; i++)
{
    [str appendFormat:@"%@,",[[dict allKeys] objectAtIndex:i]];
}
[str appendFormat:@")values ("];
for (int i = 0; i<[[dict allKeys] count]; i++)
{
    [str appendFormat:@"%@,",[dict valueForKey:[[dict allKeys] objectAtIndex:i]]];
}

[str appendFormat:@");"];
NSLog(@"qry : %@",str);
const char *sql = [str UTF8String]; ;

if((sqlite3_open([database UTF8String], &cruddb)==SQLITE_OK))
{
    if (sqlite3_prepare(database, sql, -1, &stmt, NULL) ==SQLITE_OK)
    {
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
    }
    else
    {
        NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(database));
    }
    sqlite3_close(database);
}
else
{
    NSLog(@"An error has occured: %s",sqlite3_errmsg(database));
}
}

Try this.

Comments

0

Continuing @Divz Ans...

you will have create the .sqlite file. And there is nothing easier than this.

There are two ways(that i know) to create sqlite file,

1> you can download SQLite Manager add-on in firefox, where you can manipulate data in database graphically.

Or,

2> you can use Terminal with a single line command, sqlite3 dbFileName.sqlite. enter,

where you will get sqlite> now start with further SQL(create/insert/update..) queries.

you can find your sqlite file at MacHD>users>admin(not shared one)>yourFile.sqlite or, finder---go>home>yourFile.sqlite

Comments

0
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        db=[[SKDatabase alloc]initWithFile:@"student.sqlite"];
        NSURL *url=[NSURL URLWithString:@"..........Your Url............"];
        NSURLRequest *json_request=[[NSURLRequest alloc]initWithURL:url];
        NSData *data=[NSURLConnection sendSynchronousRequest:json_request returningResponse:nil error:nil];
        NSMutableDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *student_ary=[dic objectForKey:@"students"];

        for (NSMutableArray *student_info in student_ary) {
            NSMutableDictionary *insert=[[NSMutableDictionary alloc]initWithCapacity:2];
            NSMutableDictionary *info=[student_info mutableCopy];
            [insert setObject:[info objectForKey:@"name"] forKey:@"name"];
            [insert setObject:[info objectForKey:@"city"] forKey:@"city"];
            [db insertDictionary:insert forTable:@"student_info"];

        }
    })

//.m file view....

-(void)viewDidAppear:(BOOL)animated
{
    NSString *qry=@"select * from student_info";
    ary=[[db lookupAllForSQL:qry] mutableCopy];
    [tableView reloadData];
}

Comments

0

You can do some thing like this :

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  tableview cell setup

    NSArray* keys = [self.data allKeys];

    cell.textLabel.text = [self.data objectForKey:[keys objectAtIndex:indexPath.row]];

    return cell;
}

Please refer this links to have data in order in dictionary

NSDictionary with ordered keys

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.