2

I tried to insert values in sqlite3 database, but it cant work on my app. I follow this code where all this values in table are varchar.

sqlite3_stmt *statement;

NSString *docsDir;
NSArray *dirPaths;
int ID;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"weather.sqlite"]];


const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO satish VALUES ('jodhpur','hazy1','68','7','700','30','28','10sept')"];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
    {
        NSLog(@"%@",statement);
        sqlite3_bind_text(statement, 1, insert_stmt, -1, SQLITE_TRANSIENT );

        if (sqlite3_step(statement) == SQLITE_ROW)
        {
            ID = sqlite3_last_insert_rowid(contactDB);

            NSLog(@"Data inserted successfully ");

        } 
        else{             
            NSLog(@"Failed to add contact");
        }
    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
}

2 Answers 2

2
//tyr this code    
// you must give the column name to insert values in DB   

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
            NSString *documentsDir = [paths objectAtIndex:0];

            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSError *error;
            NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"register.sqlite"];
            BOOL success = [fileManager fileExistsAtPath:dbPath]; 
            sqlite3_stmt *selectstmt;
            if(!success)
            {
                NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"register.sqlite"];
                success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

                if (!success) 
                    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
            }

            if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
                //*************** insert value in database******************************\\ 

                const char *sql = "insert into satish (firstname,lastname,email,company,phone) VALUES ('sdcbb','bbbb','bbbb','bbbb',1111122)";
                sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL); 
                if(sqlite3_step(selectstmt)==SQLITE_DONE)
                {
                    NSLog(@"insert successfully");
                }
                else
                {
                    NSLog(@"insert not successfully");

                }
            sqlite3_finalize(selectstmt);
                sqlite3_close(database);
            }
Sign up to request clarification or add additional context in comments.

12 Comments

sorry,i am not changed DB name, register.sqlite is my DB name
you change register.sqlite to weather.sqlite
and change "insert into satish (your column names) VALUES (your values)";
in your code ,it give some error near database ,says use to undeclared identifier 'datadase'
add "static sqlite3 *database = nil; " in after "@implementation class name"
|
0

I don't see where you copy your database from your bundle to the documents directory. If you do not copy a premade SQLite database, then your open call will create a new empty one. Since you did not create a table yet in your empty database, then the Insert call will fail. Solution 1: Write file routines to copy a premade SQLite database with a table named satish into your documents directory. Solution 2: If you don't want to premake a sqlite database, use a create statement in SQLite first to create the satish table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.