使用主键将表移动到另一个表

时间:2014-05-25 00:54:31

标签: ios sqlite integer copy

我在.sql数据库文件中创建了表。已创建整数列但错误地未将其设置为主键。现在我想通过创建新表来修复它,其中id表这次设置为主键。当尝试将数据从一个表复制到另一个表时,只要新表包含具有主键的列,就始终会收到错误。这对我来说非常重要,所以我要求任何帮助。

[self openDB];

sqlite3_stmt *statement3;
if(sqlite3_open([[self filePath] UTF8String], &db) == SQLITE_OK)
{
    NSString *tableName=@"bookings2";

    NSString *query=[NSString stringWithFormat:@"create table %@(id INTEGER PRIMARY KEY, testBooking TEXT, timeFrom TEXT, timeTo TEXT, pupilName TEXT, description TEXT, date_of_event TEXT, lessonBooked TEXT, pupilId TEXT)",tableName];
    const char *update_stmt = [query UTF8String];
    sqlite3_prepare_v2(db, update_stmt, -1, &statement3, NULL);

    if(sqlite3_step(statement3)==SQLITE_DONE)

        // Release the compiled statement from memory
        sqlite3_finalize(statement3);
    sqlite3_close(db);
}


[self openDB];

NSString *strSqlStatement = [NSString stringWithFormat:@"INSERT INTO bookings2 (testBooking, timeFrom, timeTo, pupilName, description, date_of_event, pupilId, lessonBooked) SELECT testBooking, timeFrom, timeTo, pupilName, description, date_of_event, pupilId, lessonBooked FROM bookings"];


char *err;
if (sqlite3_exec(db, [strSqlStatement UTF8String], NULL, NULL, &err) !=SQLITE_OK)
{
    sqlite3_close(db);
    NSAssert(0, @"Couldn't create table");
}else {
    sqlite3_close(db);
    NSLog(@"done");
}

2 个答案:

答案 0 :(得分:0)

定义列id时,还应包含AUTOINCREMENT。这样,它将自动为每个id值提供唯一的整数值。

NSString *query=[NSString stringWithFormat:@"create table %@(id INTEGER PRIMARY KEY AUTOINCREMENT, testBooking TEXT, timeFrom TEXT, timeTo TEXT, pupilName TEXT, description TEXT, date_of_event TEXT, lessonBooked TEXT, pupilId TEXT)", tableName];

而且,作为总法律顾问,我建议您始终记录sqlite3_errmsg(如果您传递了指向您自己char *err的指针,请查看),如果您的SQLite代码没有#&# 39; t表现如预期。返回的错误消息通常很有启发性。

答案 1 :(得分:0)

这是有效的代码:

-(void)fixDiary {



[self openDB];


//////// add column if it not exist
sqlite3_stmt *statement3;
if(sqlite3_open([[self filePath] UTF8String], &db) == SQLITE_OK)
{
    NSString *tableName=@"bookings2";

    NSString *query=[NSString stringWithFormat:@"create table %@(id INTEGER PRIMARY KEY AUTOINCREMENT, testBooking TEXT, timeFrom TEXT, timeTo TEXT, pupilName TEXT, description TEXT, date_of_event TEXT, lessonBooked TEXT, pupilId TEXT)", tableName];



    const char *update_stmt = [query UTF8String];
    sqlite3_prepare_v2(db, update_stmt, -1, &statement3, NULL);

    if(sqlite3_step(statement3)==SQLITE_DONE)

        // Release the compiled statement from memory
        sqlite3_finalize(statement3);
    sqlite3_close(db);
}

[self openDB];


// Setup the SQL Statement and compile it for faster access
NSString *strSqlStatement = [NSString stringWithFormat:@"INSERT INTO bookings2 (testBooking, timeFrom, timeTo, pupilName, description, date_of_event, pupilId, lessonBooked) SELECT testBooking, timeFrom, timeTo, pupilName, description, date_of_event, pupilId, lessonBooked FROM bookings"];


char *err;
if (sqlite3_exec(db, [strSqlStatement UTF8String], NULL, NULL, &err) !=SQLITE_OK)
{
    NSAssert(0, @"Couldn't create table: %s", sqlite3_errmsg(db));
    sqlite3_close(db);
}else {
    NSLog(@"done");
}

}