2

这是我的代码:

为了创建数据库,在我的 ViewDidLoad 中:

 NSString *docsDir;
    NSArray *dirPaths;

    dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];

    NSFileManager *filemgr=[NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath:databasePath]==NO)
    {
        NSLog(@"Creating DB");
        const char *dbpath=[databasePath UTF8String];

        if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
        {
            char *error_msg;
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, EMAIL TEXT, CODE TEXT, FULL TEXT)";

            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &error_msg) != SQLITE_OK)
            {
               NSLog(@"Failed to create table");
            }

            sqlite3_close(contactDB);

        } else {
            NSLog(@"Failed to open/create database");
        }
    } else {
        NSLog(@"DB exists");
    }

我可以看到输出消息"Creating DB"

然后我必须在那里添加我的数据,我确实有:

sqlite3_stmt    *statement;

                const char *dbpath = [databasePath UTF8String];

                if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
                {
                    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (url, email, code, full) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")", url_2, mail, keycode,full_2 ];

                    const char *insert_stmt = [insertSQL UTF8String];

                    sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
                    if (sqlite3_step(statement) == SQLITE_DONE)
                    {
                        NSLog(@"Contact added");
                    } else {
                        NSLog(@"Failed to add contact");
                    }
                    sqlite3_finalize(statement);
                    sqlite3_close(contactDB);
                } else {
                    NSLog(@"PROBLEM - CONTACT NOT ADDED");
                }

我看到了"PROBLEM - CONTACT NOT ADDED"消息。

有什么帮助吗?

4

2 回答 2

3

问题是您正在打开数据库NSDocumentationDirectory,而不是NSDocumentDirectory.

一些不相​​关的观察:

  1. 如果sqlite3_open()失败,您应该查看有助于诊断问题根源的数字返回代码。sqlite3.h您可以在头文件中查找值。

  2. 与您的代码示例无关的观察:您应该检查您的sqlite3_prepare_v2()返回代码,因为 SQL 错误通常在那里被识别,而不是在sqlite3_step(). 如果它返回的不是SQLITE_OK,则立即调用sqlite3_errmsg()以准确确定失败的原因。

  3. 您不应该使用stringWithFormatINSERTSQL 语句添加值。相反,使用?占位符,然后使用sqlite3_bind_XXX()函数将值绑定到这些 SQL 值。事实上,如果您插入的任何值包含引号,您的 SQL 就会失败(并且容易受到 SQL 注入攻击)。

于 2013-01-04T02:43:51.323 回答
1

如@Rob所示,您犯了一个小错误...

代替

dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

你应该使用

dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

以下是更正后的代码,

NSString *docsDir;
NSArray *dirPaths;

dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];

NSFileManager *filemgr=[NSFileManager defaultManager];

if ([filemgr fileExistsAtPath:databasePath]==NO)
{
    NSLog(@"Creating Database...");
    const char *dbpath=[databasePath UTF8String];

    if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
    {
        char *error_msg;
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, EMAIL TEXT, CODE TEXT, FULL TEXT)";

        if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &error_msg) != SQLITE_OK)
        {
           NSLog(@"Failed to create table");
        }

        sqlite3_close(contactDB);

    } 
    else 
    {
        NSLog(@"Failed to open/create database");
    }
} 
else 
{
    NSLog(@"DB exists");
}

你可以像这样插入数据,

sqlite3_stmt *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (url, email, code, full) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")", url_2, mail, keycode,full_2 ];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog(@"Contact added");
    } 
    else
    {
        NSLog(@"Failed to add contact");
    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
} 
else
{
    NSLog(@"PROBLEM - CONTACT NOT ADDED");
}

谢谢你...!!!

于 2014-03-18T11:23:14.690 回答