Translate

Wednesday, December 12, 2012

Insert values in sqlite


// Insert values in sqlite

#import <sqlite3.h>


#define kDatabaseName  @"time.sqlite"



sqlite3 *database;


-(void)writeDatabase:(NSString *)str1 andid:(int)number
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *DatabasePath = [documentsDirectory stringByAppendingPathComponent:"myDB.sqlite"];
    
    if(sqlite3_open([DatabasePath UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *compiledStatement;
const char *sql ="INSERT INTO TableName (string, No) VALUES (?,?)";
if (sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK
{
            
}
        sqlite3_bind_text(compiledStatement, 1, [str1 UTF8String], -1SQLITE_TRANSIENT);
        sqlite3_bind_int(compiledStatement, 2, number);
        
        sqlite3_reset(compiledStatement);
int success = sqlite3_step(compiledStatement);
if (success == SQLITE_ERROR
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
else
NSLog(@"Success");
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

//=========================================================================

-(void)readDBFile
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *DatabasePath = [documentsDirectory stringByAppendingPathComponent:kDatabaseName];
sqlite3_stmt *compiledStatement;
    int d=1;
if(sqlite3_open([DatabasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement;
        NSString *newString=[NSString stringWithFormat:@"Select InTime from Time where no=%d",d];
sqlStatement=[newString UTF8String];
NSLog(@"newString=%@",newString);
printf("\nError%s",sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, sqlStatement,-1, &compiledStatement, NULL) == SQLITE_OK) {
//sqlite3_
while(sqlite3_step(compiledStatement) == SQLITE_ROW
{
@try
{
NSString *str1 = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]retain];
                    int d = sqlite3_column_int(compiledStatement, 1);
                    
                    NSLog(@"fld %@ ==== %i", str1, d);
}
@catch (NSException *e)
{
NSLog(@"Error cacthed");
}
}
sqlite3_reset(compiledStatement);
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}




//=========================================================================

//write sqlite in library path

#import <sqlite3.h>

   NSString *databasePath;
NSString *databaseName;
sqlite3 *database;


//=============================

didFinishLaunchingWithOptions

-----------------------------------

 // Setup some globals
databaseName = @"time.sqlite";
    
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    NSLog(@"datapath %@", databasePath);
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];

// ==========================================

-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
    
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
    
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
    
// If the database already exists then return without doing anything
if(success) return;
    
// If not then proceed to copy the database from the application to the users filesystem
    
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    
[fileManager release];
}



No comments:

Post a Comment