Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save and fetch data from Sqlite database

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 520
    Comment on it

    First include the sqlite file in your project. Then copy that file from bundle to documents directory as below and create Database method.

    1. -(void)createDatabase{
    2. NSString *pathForDocumentDirectory =[self databasePath]; //define new path and put old path in new one
    3. if ([[NSFileManager defaultManager]fileExistsAtPath:pathForDocumentDirectory]) {
    4. return;
    5. }
    6. NSString *path = [[NSBundle mainBundle] pathForResource:@"myDatabase" ofType:@"sqlite"];
    7. NSLog(@"bundle path %@",path);
    8. NSLog(@"doc dir path %@",pathForDocumentDirectory);
    9. NSError *error;
    10. [[NSFileManager defaultManager] copyItemAtPath:path toPath:pathForDocumentDirectory error:&error];
    11. if (error) {
    12. NSLog(@"error while transferring file=> %@",error.localizedDescription);
    13. } else {
    14. NSLog(@"db file saved in documents");
    15. }
    16. }

     

    For Database path we can use below method.

    1. -(NSString *)databasePath{
    2.  
    3.  
    4. //define the property of databasepath ,path start from zero.
    5.  
    6.  
    7. NSString *pathForDocumentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex:0];
    8. pathForDocumentDirectory=[pathForDocumentDirectory stringByAppendingPathComponent:@"myDatabase.sqlite"]; //add myDatabase in this, myDatabase is the database which we created
    9. return pathForDocumentDirectory; //got database path in pathForDocumentDirectory
    10. }

    By using below code we can save data in database.

    1. -(BOOL)saveRecordsInDb {
    2. const char *databasePath = [self.databasePath UTF8String]; //convert db path in char formate
    3. if (sqlite3_open(databasePath, &database) == SQLITE_OK) //open database
    4. {
    5. NSString *insertData = [NSString stringWithFormat:@"insert into myTable (firstName,lastName) values (\"%@\",\"%@\")",@"jaya",@"arti"]; //myTable is the table name having two columns first name and last name.
    6. const char *insert_stmt = [insertData UTF8String]; // converting insertData into char
    7. sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);//prepare statement
    8. BOOL success;
    9. if (sqlite3_step(statement) == SQLITE_DONE)//step is used for execution
    10. {
    11. success = YES;
    12. }
    13. else {
    14. success = NO;
    15. }
    16. sqlite3_reset(statement); //reset to value 0
    17. return success;
    18. }
    19. return NO;
    20. }
    21.  
    22.  

    If we want to fetch records then we can use following code. In below code we are printing fetched data.
     

    1. -(NSArray *)getAllDetailsFromDb{
    2. const char *databasePath = [self.databasePath UTF8String]; //converting databasePath path into char
    3. NSMutableArray *resultArray = [NSMutableArray new];
    4. if (sqlite3_open(databasePath, &database) == SQLITE_OK)
    5. {
    6. NSString *getDetails = [NSString stringWithFormat:@"select * from myTable];
    7. const char * get_stmt = [getDetails UTF8String];//convert getDetails into char formate
    8. if (sqlite3_prepare_v2(database,
    9. get_stmt, -1, &statement, NULL) == SQLITE_OK)//for connection
    10. {
    11. while (sqlite3_step(statement) == SQLITE_ROW)
    12. {
    13. //it will run values column by column so we have to write column 0 ,column1 and all.
    14.  
    15. NSString *firstDetail = [[NSString alloc] initWithUTF8String:
    16. (const char *) sqlite3_column_text(statement, 0)];
    17. NSString *secondDetail = [[NSString alloc] initWithUTF8String:
    18. (const char *) sqlite3_column_text(statement, 1)];
    19.  
    20. NSLog(@"checks are %@ %@ ",firstDetail,secondDetail);
    21. }
    22. sqlite3_reset(statement);
    23. }
    24. else{
    25. NSLog(@"Not found");
    26. return nil;
    27. }
    28. }
    29. NSLog(@"%@",resultArray);
    30. return resultArray;
    31. }
    32.  
    33.  
    34.  
    35.  
    36.  

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: