iphone - 在 SQLITE 数据库中插入值

标签 iphone ios database ios5 sqlite

我尝试在 sqlite3 数据库中插入值,但它无法在我的应用程序上运行。 我遵循此代码,其中表中的所有值都是 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);
}

最佳答案

//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);
            }

关于iphone - 在 SQLITE 数据库中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452813/

相关文章:

mysql - 在 MySQL 中使用 PostgreSQL 作为存储引擎?

android - 在 cocos2dx 中加载资源,它适用于 android 和 iPhone

ios - 弹出窗口中的 CNContactPickerViewController (iOS)

ios - iOS SDK中如何计算cos、sin、tan?

mysql - 从 2 个表中获取数据,在 Doctrine 上没有关系

database - 将 Google App 引擎数据库导出到 excel 文件

iphone - iPhone 是否有一个可以使用 HMAC-SHA-1 编码的库

ios - ZBarReaderViewController 阅读器 View 更改了 iOS6 和 iOS7 的方向,即使在被 _reader.supportedOrientationsMask 限制后也是如此

iOS - AVFoundation 是否支持 3D 条形码?

ios - 对 double 进行算术运算