Objective C中的Sqlite数据库插入语句 [英] Sqlite Database Insert Statement in Objective C

查看:19
本文介绍了Objective C中的Sqlite数据库插入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试只向我的 sqlite 数据库中插入两个整数变量.我创建了一个名为 ups.sqlite 的数据库,它有一个表(upssTable),该表有两列.但是当我打开/Users/ds/Library/Application Support/iPhone Simulator/5.0/Applications/FCB4B455-4B7F-4C47-81B6-AC4121874596/SqliteDeneme.app/ups.sqlite 时,ups.sqlite 中没有数据.我的代码在这里:

I try to insert only two integer variable to my sqlite database. I created a database which name is ups.sqlite, it has a one table (upssTable) and the table have two column. But when I open /Users/ds/Library/Application Support/iPhone Simulator/5.0/Applications/FCB4B455-4B7F-4C47-81B6-AC4121874596/SqliteDeneme.app/ups.sqlite there is no data in ups.sqlite. My code is here:

- (IBAction)buttonClick:(id)sender {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ups.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

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

NSLog(@"database path %@",dbPath);


if(!(sqlite3_open([dbPath UTF8String], &cruddb) == SQLITE_OK))
{
    NSLog(@"An error has occured.");
}

if(sqlite3_open([dbPath UTF8String], &cruddb) ==SQLITE_OK){

    NSString *  str1 =@"1";
    NSString * str2 =@"1";


    const char *sql = "INSERT INTO upssTable (column1, column2) VALUES (?,?)"; 

    NSInteger result = sqlite3_prepare_v2(cruddb,sql, -1, &stmt, NULL);
    NSLog(@"upss %s
", sqlite3_errmsg(cruddb));

    if(result == SQLITE_OK)        
    {
        sqlite3_bind_int(stmt, 1, [str1 integerValue]);
        sqlite3_bind_int(stmt, 2, [str2 integerValue]);

    }
    else
    {
        NSAssert1(0, @"Error . '%s'", sqlite3_errmsg(cruddb));                        
    }
    sqlite3_reset(stmt);
    sqlite3_finalize(stmt);
    sqlite3_close(cruddb);
}
}

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"ups.sqlite"];
}

我该如何解决这个问题?请帮我.感谢您的回复.

How can I solve this problem? Please help me. Thanks for your reply.

推荐答案

你正在创建你的 sqlite3 语句,但你实际上并没有使用 sqlite3_step() 执行它.

You are creating your sqlite3 statement, but you aren't actually executing it using sqlite3_step().

您似乎也打开了两次数据库?

Also you appear to be opening the database twice?

这篇关于Objective C中的Sqlite数据库插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆