objective-c - 将文本字段中的数据插入 SQLite 数据库

标签 objective-c cocoa-touch ios sqlite

我正在尝试将数据插入到我的 SQLite 数据库中。我已经导入了我的项目所需的所有 neccassry 框架和数据库文件。在我的 Controller 类 xib 中,有两个文本字段和一个按钮。当我单击按钮时,我希望将输入到两个文本字段中的数据保存在我的数据库中。

在我的 app delagate 中,我创建了两个函数,一个用于附加数据库路径,另一个用于将数据插入数据库。在插入函数中,我检查某些条件,即,如果添加了数据,应显示一个警报 View ,显示添加的记录,但是当我添加一条新记录时,它总是进入 else block ,这是一个错误。

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

-(void)insertData{

    sqlite3 *database;
    sqlite3_stmt *statement;
     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"login.sqlite"];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO Loginchk (uname,password) VALUES ('%@',' %@');",Gunameq,Gpassq];
        const char *insert_stmt = [insertSQL UTF8String];
        if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, nil)== SQLITE_OK)
        {
            sqlite3_bind_text(statement, 1, [Gunameq UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 2, [Gpassq UTF8String], -1, NULL);
        }

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

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

这是我的登录 Controller 类。我在这里声明了一个函数,通过它我可以访问我的应用程序委托(delegate)的函数。

-(IBAction)buttonPressed:(id)sender
{

    Gpassq=Password.text;
    Gunameq=Uname.text;


    NSLog(@"%@%@",Gunameq,Gpassq);
    AddListAppDelegate *appDelegate =(AddListAppDelegate *)[[UIApplication sharedApplication]delegate];
    [appDelegate insertData];

}

请解决这个问题。

最佳答案

static sqlite3_stmt *insertStmt = nil;

if(insertStmt == nil) 
{
    insertSql = "INSERT INTO Loginchk (uname,password) VALUES(?,?)";
    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(insertStmt, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    NSLog("Inserted");
//Reset the add statement.
sqlite3_reset(insertStmt);
insertStmt = nil;           

在上面你可以看到。如果您要绑定(bind)数据,则不需要 stringWithFormat。只需放置问号而不是绑定(bind)文本。

希望对您有所帮助。

关于objective-c - 将文本字段中的数据插入 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700488/

相关文章:

objective-c - 发现对早期 iOS 版本中不可用的方法的调用

objective-c - 我希望在用户单击导航 View 中的 'save' 按钮时重新加载 TableView

objective-c - 关于 Objective C,便利方法的定义是什么?

ios - 将我的 Obj-C 框架与 Swift 框架联系起来

ios - 获取 : Property 'getFlower' not found on object of type 'ViewController *'

c++ - 在 Objective-C++ 中重载 [] 运算符

ios - 使用方法参数

objective-c - 如何在 Objective-C 中声明双指针属性?

cocoa - 如何禁用 NSNumberFormatter 中的任何分组?

objective-c - 删除mapkit中的边框