Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:59:20+00:00 2026-06-03T09:59:20+00:00

How to insert data into table in sqlite iPhone? I am trying following, but

  • 0

How to insert data into table in sqlite iPhone?

I am trying following, but its failing:

 NSString *query=[NSString stringWithFormat:@"insert into %@ (name) values ('%@')", table name,myName ];


 sqlite3 *database;

sqlite3_stmt *createStmt = nil;

if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    if (createStmt == nil) {

        if (sqlite3_prepare_v2(database, [query UTF8String], -1, &createStmt, NULL) != SQLITE_OK) {
            return NO;
        }
        sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);
        return YES;
    }

    return YES;
}else {
    return NO;
}

I have created table in following manner:

create table if not exists myDets (dets_id integer primary key asc, name text);

I am also using Firefox SQLite plugin to check db. When I try to insert record via firefox into my db it gives me following error:

Failed to insert values
Exception Name: NS_ERROR_STORAGE_IOERR
Exception Message: Component returned failure code: 0x80630002 (NS_ERROR_STORAGE_IOERR)        [mozIStorageStatement.execute]

Badly stuck 🙁

Also, i am running this on iPhone simulator, does it matter?

Please help

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:59:21+00:00Added an answer on June 3, 2026 at 9:59 am
    • Use parameters and don’t string format your update statement
    • Add lots of tracing, check return codes and check for error codes (you’re question isn’t clear what error, issue you’re hitting on iPhone).
    • use sqlite3_errmsg to get error messages
    • construct your dbPath and log it out. Ensure you can open the db from terminal under the emulator. Remember that sqlite will create a database in memory passively so if you’re path is off and the db doesn’t exist it can be confusing.
    • print out your update statement and try it from sqlite cmdline app in terminal in the path you logged.
    • if you’re putting the db in the main bundle resources, it’s a template and needs to be copied in order to open and write to it.

    Here’s an update function from a sample of mine:

    - (void)updateContact: (Contact*)contact error:(NSError**)error
    {
        if (![self ensureDatabaseOpen:error])
        {
            return;
        }
    
        NSLog(@">> ContactManager::updateContact");
    
        // prep statement
        sqlite3_stmt    *statement;
        NSString *querySQL = @"update contacts set name=?,address=?,phone=? where id=?";
        NSLog(@"query: %@", querySQL);
        const char *query_stmt = [querySQL UTF8String];
    
        // preparing a query compiles the query so it can be re-used.
        sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);     
        sqlite3_bind_text(statement, 1, [[contact name] UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_text(statement, 2, [[contact address] UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_text(statement, 3, [[contact phone] UTF8String], -1, SQLITE_STATIC);
        sqlite3_bind_int64(statement, 4, [[contact id] longLongValue]);
    
        NSLog(@"bind name: %@", [contact name]);
        NSLog(@"bind address: %@", [contact address]);
        NSLog(@"bind phone: %@", [contact phone]);
        NSLog(@"bind int64: %qi", [[contact id] longLongValue]);
    
        // process result
        if (sqlite3_step(statement) != SQLITE_DONE)
        {
            NSLog(@"error: %s", sqlite3_errmsg(_contactDb));
        }
    
        sqlite3_finalize(statement);
    }
    

    In the sample, the db is copied from resources to a path and opened. Here’s the ensure opened function I use to do that:

    - (BOOL)ensureDatabaseOpen: (NSError **)error
    {
        // already created db connection
        if (_contactDb != nil)
        {
            return YES;
        }
    
        NSLog(@">> ContactManager::ensureDatabaseOpen");    
        if (![self ensureDatabasePrepared:error])
        {
            return NO;
        }
    
        const char *dbpath = [_dbPath UTF8String]; 
        if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
            error != nil)
        {
            *error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
            return NO;
        }
    
        NSLog(@"opened");
    
        return YES;
    }
    
    - (BOOL)ensureDatabasePrepared: (NSError **)error
    {
        // already prepared
        if ((_dbPath != nil) &&
            ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
        {
            return YES;
        }
    
        // db in main bundle - cant edit.  copy to library if !exist
        NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
        NSLog(@"%@", dbTemplatePath);
    
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];
    
        NSLog(@"dbPath: %@", _dbPath);
    
        // copy db from template to library
        if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
        {
            NSLog(@"db not exists");
            NSError *error = nil;
            if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
            {
                return NO;
            }
    
            NSLog(@"copied");
        }    
    
        return YES;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert data into SQLite using Phonegap. Its working fine when hard
I am trying to insert data into a table in sorted order, for later
I am trying to insert some text data into a table in SQL Server
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I am trying to insert a data into SQLite database using Python. INSERT INTO
I can't insert data into sqlite table. Here is my code: import sqlite3 connection
I'm trying to insert non-latin data into sqlite database using bind variables using System.Data.SQLite.
I have the following code that creates my table. I insert data into it
I insert data into sqlite database table and when i insert first record ,
I want to insert data into a table where I don't know the next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.