2

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

在下面找到我的代码:

-(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);

    }
    }}

提前致谢

4

2 回答 2

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);
    }
 }
于 2012-06-16T06:33:30.933 回答
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,...)
于 2012-06-15T11:04:52.477 回答