如何通过iphone项目中的obj c方法改变sqlite数据库中的表 [英] how can i alter table in sqlite database through the obj c methods in iphone project

查看:158
本文介绍了如何通过iphone项目中的obj c方法改变sqlite数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hii每一个

我需要在现有表中添加一列,以便如何通过obj c更改sqlite数据库中的表,我使用以下内容以相同的方式将数据插入表中的代码如何编写updata表方法

I need to add one column to my existing table so how can i alter table in sqlite database through the obj c ,i am using the following code for inserting data into table in the same way how can i write updata table method

- (void) InsertRecord {


if(addStmt == nil) {
    const char *sql = "insert into tbl_Users(FirstName,MiddleName) Values(?,?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [strFirstName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [strMiddleName UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(addStmt, 3, [strLogin UTF8String], -3, SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    //productID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);

}

任何人都可以帮助我,, thanx提前

can any one help me,,thanx in advance

推荐答案

这可能有些用处。
头文件

This might be of some use. Header File

NSString *databasePath;

.m文件
//调用这些方法

.m File //Make a call to these Methods

    [self checkAndCreateDB ];
    [self alterDB];

-(void)checkAndCreateDB {
    NSString* databaseName = @"MasterDB.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success1;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success1 = [fileManager fileExistsAtPath:databasePath];
    if(success1) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

-(void) alterDB{
    sqlite3 *database;
    sqlite3_stmt *statement; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {

        NSString *updateSQL = [NSString stringWithFormat: @"ALTER TABLE User ADD COLUMN testColumn TEXT"];
        const char *update_stmt = [updateSQL UTF8String];
        sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);

        if(sqlite3_step(statement)==SQLITE_DONE) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB altered" message:@"Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];
            [alert release];
            alert=nil;

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Updation" message:@"DB not Altered" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            [alert release];
            alert=nil;
        }   
        // Release the compiled statement from memory
        sqlite3_finalize(statement);    
        sqlite3_close(database);
    }
}

这篇关于如何通过iphone项目中的obj c方法改变sqlite数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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