1

我试图在我的 sqlite 文件中只插入一个名称。我正在使用此代码,但它不起作用:/

-(void)InsertRecords:(NSMutableString *) txt{
    if(addStmt == nil) {
        const char *sql = "INSERT INTO myMovies (movieName)  VALUES(?) ";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        else
            sqlite3_bind_text(addStmt, 1, [txt UTF8String], -1, SQLITE_TRANSIENT);
    }
    if(SQLITE_DONE != sqlite3_step(addStmt))

        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        sqlite3_reset(addStmt);
}
4

4 回答 4

2

将您的查询传递给此方法并尝试,

-(void)Insertdata:(NSString*)query{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"YourDBName.sql"];

    if(sqlite3_open([databasePath UTF8String],&db) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat: @"%@",query];

        char *errmsg=nil;

        if(sqlite3_exec(db, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
        {
           NSLog(@".. Row Added ..");
        }
    }
    sqlite3_close(db);
}
于 2012-10-25T05:40:17.270 回答
1

尝试这个:

//save our data
- (BOOL) saveEmployee:(Employee *)employee
{
    BOOL success = false;
    sqlite3_stmt *statement = NULL;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &mySqliteDB) == SQLITE_OK)
    {
        if (employee.employeeID > 0) {
            NSLog(@"Exitsing data, Update Please");
            NSString *updateSQL = [NSString stringWithFormat:@"UPDATE EMPLOYEES set name = '%@', department = '%@', age = '%@' WHERE id = ?",
                                    employee.name,
                                    employee.department,
                                    [NSString stringWithFormat:@"%d", employee.age]];

            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_prepare_v2(mySqliteDB, update_stmt, -1, &statement, NULL );
            sqlite3_bind_int(statement, 1, employee.employeeID);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                success = true;
            }

        }
        else{
            NSLog(@"New data, Insert Please");
            NSString *insertSQL = [NSString stringWithFormat:
                                   @"INSERT INTO EMPLOYEES (name, department, age) VALUES (\"%@\", \"%@\", \"%@\")",
                                   employee.name,
                                   employee.department,
                                   [NSString stringWithFormat:@"%d", employee.age]];

            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_prepare_v2(mySqliteDB, insert_stmt, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                success = true;
            }
        }

        sqlite3_finalize(statement);
        sqlite3_close(mySqliteDB);

    }

    return success;
}
于 2015-04-17T04:41:46.327 回答
1

sqlite 的“内存不足”错误通常意味着您正在使用的数据库句柄尚未打开。确保您使用发布的代码中显示sqlite3_open_v2database变量进行调用?

于 2012-10-25T03:48:54.777 回答
1

好吧,我终于成功了!这是我为每个需要它的人使用的代码:

-(void)InsertRecords:(NSMutableString *)txt{

    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"movieData.sqlite"];
    const char *dbpath = [dbPath UTF8String];
    sqlite3 *contactDB;

    sqlite3_stmt    *statement;

    NSLog(@"%@",dbPath);
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO myMovies (movieName) VALUES (\"%@\")", txt];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            sqlite3_bind_text(statement, 1, [txt UTF8String], -1, SQLITE_TRANSIENT);
        } else {
            NSLog(@"error");
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }
}
于 2012-10-26T11:00:32.587 回答