无法将数据添加到本地数据库 sqlite IOS [英] cannot add data to local DB sqlite IOS

查看:30
本文介绍了无法将数据添加到本地数据库 sqlite IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

用于创建数据库,在我的 ViewDidLoad 中:

For creating the DB, in my 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"

然后我必须在那里添加我的数据:

Then where I have to add my data there I do have:

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");
                }

然后我看到 问题 - 未添加联系人" 消息.

有什么帮助吗?

推荐答案

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

you made a minor mistake as shown by @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");
}

你可以像这样插入数据,

You can insert data like this,

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");
}

谢谢...!!!

这篇关于无法将数据添加到本地数据库 sqlite IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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