WS and ADD in DB

1)//IN APPDELEGATE……………………….

 

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

       [self checkDatabase];

    return YES;

   }

– (void)checkDatabase

{

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    NSString *databasePath = [documentsDir stringByAppendingPathComponent:@”TestData.sqlite”];

    NSString *databaseName = @”TestData.sqlite”;

    NSFileManager *fileManager = [NSFileManager defaultManager];

        NSLog(@”%@ === %@”,databaseName,databasePath);

        if(![fileManager fileExistsAtPath:databasePath])

    {

        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath]

                                         stringByAppendingPathComponent:databaseName];

        [fileManager removeItemAtPath:databasePath error:nil];

        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    }

    else

    {

        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    }

}

2) Add Class TestData add as NSObject……..

TestData.h

#import <Foundation/Foundation.h>

#import “sqlite3.h”

@interface TestData : NSObject

@property (nonatomic, readwrite) NSInteger  VideoID;

@property (nonatomic, strong) NSString   *videoName;

@property (nonatomic, strong) NSString   *imgPhoto;

@property (nonatomic, strong) NSString   *imgVideoURl;

+ (NSInteger)addRecord:(TestData *)sObj;

+ (NSMutableArray *)readRecords;

@end

TestData.m

#import “TestData.h”

@implementation TestData

+ (NSInteger)addRecord:(TestData *)sObj

{

    NSInteger retVal = false;

    

    NSString *query = [NSString stringWithFormat:

                       @”INSERT INTO TestData (videoName, imagePhoto, imageVideoUrl) VALUES (‘%@’, ‘%@’, ‘%@’)”,

                       sObj.videoName,

                       sObj.imgPhoto,

                       sObj.imgVideoURl

                       ];

    

    sqlite3 *db;

    

    

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@”TestData.sqlite”];

    

    

    

    if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)

    {

        sqlite3_stmt *compiledStatement;

        

        if(sqlite3_prepare_v2(db,[query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)

        {

            sqlite3_step(compiledStatement);

        }

        

        sqlite3_finalize(compiledStatement);

        

        retVal = sqlite3_last_insert_rowid(db);

        

        

        

    }

    sqlite3_close(db);

    

    return retVal;

}

+ (NSMutableArray *)readRecords

{

    NSMutableArray *recordsArr = [[NSMutableArray alloc] init];

    

    NSString *query = [NSString stringWithFormat:@”SELECT * FROM TestData ORDER BY videoName”];

    

    sqlite3 *db;

    

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@”TestData.sqlite”];

    

    if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)

    {

        sqlite3_stmt *compiledStatement;

        

        

        if(sqlite3_prepare_v2(db,[query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)

        {

            while(sqlite3_step(compiledStatement) == SQLITE_ROW)

            {

               

      TestData *sObj = [[TestData alloc] init];

                

                sObj.VideoID = sqlite3_column_int(compiledStatement, 0);

                

                sObj.videoName = [NSString stringWithUTF8String😦char *)sqlite3_column_text(compiledStatement, 1)];

                sObj.imgPhoto = [NSString stringWithUTF8String😦char *)sqlite3_column_text(compiledStatement, 2)];

                sObj.imgVideoURl = [NSString stringWithUTF8String😦char *)sqlite3_column_text(compiledStatement, 3)];

                [recordsArr addObject:sObj];

                

                NSLog(@”%@”,sObj);

            }

        }

        sqlite3_finalize(compiledStatement);

    }

    sqlite3_close(db);

    

    return recordsArr;

}

@end

3)MyVideoCell.h with .XIB

#import <UIKit/UIKit.h>

#import “TestData.h”

@interface MyVideoCell : UIViewController

@property (nonatomic, strong) IBOutlet UILabel  *lblVideoName;

@property (nonatomic, strong) IBOutlet UIImageView  *imgVideo;

@property (nonatomic, strong) TestData *testObj;

@end

MyVideoCell.m

#import “MyVideoCell.h”

@interface MyVideoCell ()

@end

@implementation MyVideoCell

– (void)viewDidLoad {

    [super viewDidLoad];

    

    [self.lblVideoName setText:self.testObj.videoName];

    

    NSString *ImageURL = self.testObj.imgPhoto;

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

    self.imgVideo.image = [UIImage imageWithData:imageData];

    // Do any additional setup after loading the view from its nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

{

    IBOutlet UITableView *tblData;

    NSMutableArray *recordArr;

}

@end

ViewController.m

#import “ViewController.h”

#import “MyVideoCell.h”

#import “TestData.h”

#import “VideoVC.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

}

– (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    recordArr = [TestData readRecords];

    

    [tblData reloadData];

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 60;

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return recordArr.count;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@”Cell”];

    

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    

    TestData *currentSobj = recordArr[indexPath.row];

    

    MyVideoCell *mcv = [[MyVideoCell alloc] init];

    

    mcv.testObj = currentSobj;

    

    

    [cell.contentView addSubview:mcv.view];

    

    return cell;

}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    VideoVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@”VideoVC”];

    

    vc.testObj = recordArr[indexPath.row];

    

    [self.navigationController pushViewController:vc animated:YES];

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s