不在SQLite中保存数据

时间:2012-06-15 10:57:55

标签: iphone ios ipad

每件事都在代码中,但不在SQLite数据库中保存数据。任何人都可以帮忙吗?

在下面找到我的代码:

-(IBAction)signin:(id)sender{
    NSLog(@"%@",perDas.string1);
    nameString=[[NSString alloc]initWithString:perDas.string1];
     statusString=[[NSString alloc]initWithFormat:@"IN"];
    {
        NSLog(@"passed");
        sqlite3_stmt    *statement;

        const char *dbpath = [perDas.databasePath UTF8String];

        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            NSLog(@"pass");
            NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO status  VALUES (\"%@\", \"%@\", datetime(), \"%@\")", nameString, statusString,empString];
            NSLog(@"%@",insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);

            sqlite3_finalize(statement);
            sqlite3_close(database);

    }
    }}

提前致谢

2 个答案:

答案 0 :(得分:1)

-(IBAction)signin:(id)sender{
    NSArray *dirPath;
    NSString *docDir;
    NSString *databasePath;

    dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docDir=[dirPath objectAtIndex:0];
    databasePath=[docDir stringByAppendingPathComponent:@"YourDB.sql"];

    const char *dbPath=[databasePath UTF8String];

    if(sqlite3_open(dbPath, &db)==SQLITE_OK)
    {

        NSString *insertSQL =  [NSString stringWithFormat: @"INSERT INTO status  VALUES (\"%@\", \"%@\", datetime(), \"%@\")", nameString, statusString,empString];


        const char *insertStmt=[insertSQL UTF8String];

        char *errmsg=nil;

        if(sqlite3_exec(db, insertStmt, NULL, NULL, &errmsg)==SQLITE_OK)
        {
             NSLog(@"ADDED!");
        }

        sqlite3_close(db);
    }
 }

答案 1 :(得分:0)

你应该检查你的sql语句是否正确:

if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
...
}

如果您只插入行集的子集,请使用此INSERT语句表单:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)