0

I am trying to insert the value of 2 text box which are user id and password. But i am not getting any error or exception.

Code as follows:

I have taken 2 textbook and i am doing it on click of that button.

This is my .m file:

#import "ViewController.h"
#import "sqlite3.h"
#import <CommonCrypto/CommonCryptor.h>
NSString *databasePath;
 NSString *docsDir;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;
@interface ViewController ()
@end
@implementation ViewController
@synthesize  status,status2;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   }

- (IBAction)saveData:(id)sender{

    databasePath = [[NSBundle mainBundle]pathForResource:@"ButterFly2BE" ofType:@"db"];
  NSString *p=@"PAss";
    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &adddata) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO tblUser (UserId,Password,Description) VALUES(\"%@\",\"%@\",\"%@\")"
                                , _username.text, _password.text, p];


            const char *insert_stmt = [insertSQL UTF8String];
             sqlite3_prepare_v2(adddata, insert_stmt,-1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
                {
                        status.text = @"Contact added";
                       // status.text = @"";
                    status2.text = @"";
                   // phone.text = @"";
                } else {
            status.text = @"Failed to add contact";
            }
        sqlite3_finalize(statement);
    sqlite3_close(adddata);
}
    }
@end
Basant Gera
  • 103
  • 3
  • 16
  • What problem you are facing? The data is not inserted...right? Just log the sql query and open the database using [DB browser for SQLite](http://sqlitebrowser.org/). – Poles Mar 21 '17 at 09:30
  • Refer this link you will find your answer http://stackoverflow.com/questions/17080018/use-and-access-existing-sqlite-database-on-ios/39536484#39536484 – Indrajeet Mar 21 '17 at 09:35
  • Possible duplicate of [How to insert data into a SQLite database in iPhone](http://stackoverflow.com/questions/2184861/how-to-insert-data-into-a-sqlite-database-in-iphone) – kb920 Mar 21 '17 at 10:56

1 Answers1

2

The insertion failed because you used the database stored in the app bundle directly.

[[NSBundle mainBundle] pathForResource:@"ButterFly2BE" ofType:@"db"]

Files in the app bundle are read only. You need to first copy the database elsewhere e.g. the Documents folder before opening it.


Note that you should use the sqlite3_bind_xxxx functions instead of -stringWithFormat: because the latter exposes you to SQL injection attack.

sqlite3_prepare_v2(adddata, 
                   "INSERT INTO tblUser (UserId, Password, Description) VALUES (?, ?, ?);",
                   -1, &statement, NULL);
sqlite3_bind_text(statement, 1, _username.text.UTF8String, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, _password.text.UTF8String, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, p.UTF8String, -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE) {
    ...
Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005