sqlite in objective c.
Create Database.
Open terminal.
Type command
$ sqlite3 rajneesh.sqlite SQLite version 3.6.11 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table books(id integer primary key,name text, cost integer);sqlite>.tables;
sqlite>.exit;
Search your database in your mac,and put it(rajneesh.sqlite) in your project.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Now you can open your database in any tool, i use FireFox SQliteManager.
You can create, insert , update , delete tables in your database.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USE it in our Project
in .h
#import <sqlite3.h>@interface.. { NSString *databasePath; }
in .m
First you have to check that your database is present in documentDir or not,
if available then we use that database else we copy rajneesh.sqlite from application bundle to docDir.
-(void)sqliteDataBaseCode { NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPath objectAtIndex:0]; databasePath = [documentDir stringByAppendingPathComponent:@"rajneesh.sqlite"]; [self checkAndCreateDatabase]; } -(void)checkAndCreateDatabase { BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; success = [fileManager fileExistsAtPath:databasePath]; if(success) return; NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"storeLocator.sqlite"]; [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; }
Now you can use your dataBase to select,insert,delete or anything else you want.
SELECT
Way1
-(void)selectDataFromDataBase { if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { NSString *sqlStatement =[NSString stringWithFormat:@"SELECT * FROM tableName"]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) { while(sqlite3_step(compiledStatement)==SQLITE_ROW) { NSString *primaryKey = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)]; NSString *column1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; NSString *column2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; NSString *column3 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); } sqlite3_close(database); }
UPDATE
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { NSString *value=@"Rajneesh071"; NSString *sqlStatement = [NSString stringWithFormat:@"UPDATE tableName SET colum = '%@' WHERE id = 1" ,value]; const char *sql = [sqlStatement UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(database, sql, -1, &statement, nil) == SQLITE_OK) { } if(SQLITE_DONE != sqlite3_step(statement)) { NSLog(@"data is not inserted"); } else { NSLog(@"data is inserted"); } sqlite3_finalize(statement); sqlite3_close(database); }
Way2- BindMethod
if (sqlite3_open(databasePath, & database) == SQLITE_OK) { const char *insert_stmt = "UPDATE profile SET username = ? WHERE id = 1"; sqlite3_stmt *statement; if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL)== SQLITE_OK) { sqlite3_bind_text(statement, 1, [value UTF8String], -1, SQLITE_TRANSIENT); } if (sqlite3_step(statement) == SQLITE_DONE) { NSLog(@"Data Updated"); } else { NSLog(@"Data Not Updated); } sqlite3_finalize(statement); sqlite3_close(database); } else { NSLog(@"DB Not Connect"); }
Comments
Post a Comment