在循环中在Sqlite3中插入数据

时间:2014-04-17 07:50:50

标签: ios objective-c ios7

我是iOs编程的新手。请帮忙

我正在创建一个数据库,如果它不存在然后创建表并在从文件中逐行读取记录后,我试图将其插入到数据库中 我正在循环插入数据,我已经通过互联网阅读了这段代码,并认为可能还有一些东西, 请帮助,并指导为此方案获得最佳方式 是的问题只与数据的插入有关,创建数据库和表格的架构工作正常

NSString * docsDir;     NSArray * dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = dirPaths[0];

// Buil d the path to the database file
_databasePath = [[NSString alloc]
                 initWithString: [docsDir stringByAppendingPathComponent:
                                  @"testing.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
    const char *dbpath = [_databasePath UTF8String];

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        char *errMsg;
        const char *sql_stmt =
        "CREATE TABLE IF NOT EXISTS testt (ID INTEGER PRIMARY KEY AUTOINCREMENT, Quotes VarChar, Author TEXT)";

        if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {

            _status.text = @"Failed to create table";
        }
        else
        {

            if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
            {
                 sqlite3_stmt    *statement;

                // customCode
                _filePathXml = [[NSBundle mainBundle] pathForResource:@"quotes_tod" ofType:@"txt"];
                NSString * contentsQ = [NSString stringWithContentsOfFile:_filePathXml encoding:NSASCIIStringEncoding error:nil];



                NSArray * lines = [contentsQ componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
                //  NSString * firstLine = [lines firstObject];

                NSInteger countLines = lines.count;
                NSInteger newCheck = 0;


                while (newCheck < countLines) {


                   NSString *insertSQL = [NSString stringWithFormat:
                                           @"INSERT INTO testt (Quotes ,Author) VALUES ( \"%@\" , \"Default\")",
                                           lines[newCheck]];

                     const char *insert_stmt = [insertSQL UTF8String];
                    sqlite3_prepare_v2(_contactDB, insert_stmt,
                                       1, &statement, NULL);
                    newCheck = newCheck +1;
                 //   NSLog( @"%@" ,lines[newCheck]);
            //        newCheck = newCheck + 1;
                }

          //      sqlite3_finalize(statement);
                 sqlite3_exec(_contactDB, "COMMIT", 0, 0, 0);
                sqlite3_close(_contactDB);



          }

} }

2 个答案:

答案 0 :(得分:0)

在你的循环中,你似乎没有执行你创建的插入语句....

更改此

sqlite3_prepare_v2(_contactDB, insert_stmt,
                                   1, &statement, NULL);

对此...

int returnCode = sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, nil);

if (returnCode == SQLITE_OK) {
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        NSLog(@"SQL ERROR");
    }
} else {
    NSLog(@"Error %d in statement \"%@\"",returnCode, insertSQL);
}
sqlite3_finalize(stmt);

您还应该检查准备语句是否正常工作

答案 1 :(得分:0)

我忘了添加它,现在它正在运行

  while (newCheck < countLines) {

                       if( lines[newCheck] != SingleChar)
                       {
                       NSString *insertSQL = [NSString stringWithFormat:
                                               @"INSERT INTO testt (Quotes ,Author) VALUES ( \"%@\" , \"%@\")",
                                               lines[newCheck] , [NSString stringWithFormat: @"test"]];

                         const char *insert_stmt = [insertSQL UTF8String];
                        sqlite3_prepare_v2(_contactDB, insert_stmt,
                                           -1, &statement, NULL);
                        if (sqlite3_step(statement) != SQLITE_DONE) {
                            SuccessComp = 1;
                        }
                      sqlite3_finalize(statement);
                       }
                        newCheck = newCheck +1;

                     //   NSLog( @"%@" ,lines[newCheck]);
                //        newCheck = newCheck + 1;
                    }