【问题标题】:Save information from NSTextField in sqlite database (objective-c)将来自 NSTextField 的信息保存在 sqlite 数据库中(objective-c)
【发布时间】:2015-10-05 14:44:31
【问题描述】:

几个月以来,我对编码很感兴趣。目前我正在开发一个程序,它将文本字段中的信息保存在 sqlite3 数据库中。我在 Xcode for Mac 中的 Objective C 上对这个项目进行编程。不幸的是,我在将数据插入数据库时​​遇到了问题。当我插入它并在终端中打开我的数据库时,我只是在单元格中为字符串获取此信息:

这是我的代码:

NSString *Bt = [NSString stringWithFormat:@"%@", Text]; //Theres a IBOutlet NSTextField  *Text; in the Header data of the class


dbPath=@"/Users/Desktop/database.db";

if (sqlite3_open([dbPath UTF8String], &database)!=SQLITE_OK) {
    sqlite3_close(database);
    NSLog(@"Failed to open database");
} else {
    char *errorMessage;
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO table (table) VALUES (\"%@\")", Bt];
    const char *insert_stmt = [insertSQL UTF8String];
    if(sqlite3_exec(database, insert_stmt, NULL, NULL, &errorMessage)==SQLITE_OK){
        NSLog(@"Data inserted");
        [db_status setFloatValue:5];
    }

}

}

如果有人可以帮助我,那就太好了。我认为这只是一个小错误,但我不明白这个问题;(

最好的问候, 罗比

【问题讨论】:

    标签: objective-c cocoa sqlite


    【解决方案1】:

    从你的IBOUTLET 连接看,你的代码是怎样的,Bt 等于 TextField。但是您想要的是 TextField 中的文本值。所以你会使用这个:NSString *Bt = [NSString stringWithFormat:@"%@", Text.stringValue]; NSTextField 有一个名为 stringValue 的属性,这将返回包含文本字段内的值的 NSString文字

    允许在数据库中使用引号

    我能想到的最简单的方法是在 NSString 上使用类别方法。因此,无论您要将用户的字符串或文本保存到数据库中的任何位置,都可以使用此函数插入​​到您的格式语句中。示例如下:

    NSString 类别的头文件

    @interface NSString (STRING_)
    
    /**
     * Allowing Qoutation marks inside a string to be saved in a SQL column with the string format sourrounded by @"\"%@\"". this will not modify the string, only return a modified copy
     * @return NSString *
     */
    - (NSString *)reformatForSQLQuries;
    
    @end
    

    NSString类的植入文件

    @implementation NSString (STRING_)
    
    - (NSString *)reformatForSQLQuries {
        NSString *string = self;
        //Depending on how you "wrap" your strings to format the string values into your INSERT, UPDATE, or DELETE queries, you'll only have to use one of these statements. YOU DO NOT NEED BOTH
    
        //Use this if you have your format as: '%@'
        string = [string stringByReplacingOccurrencesOfString: @"'" withString: @"''"];
        //Use this if you have your format as: \"%@\"
        string = [string stringByReplacingOccurrencesOfString: @"\"" withString: @"\"\""];
    
        return string;
    
    }
    

    示例

    ...
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO table (table) VALUES (\"%@\")", [Bt reformatForSQLQuries]];
    ...
    

    【讨论】:

    • 感谢您的回复!当我添加您的代码时,我收到此错误消息:“NSString”没有可见的@interface 声明选择器“reformForSQLQuries”。
    • 现在可以了 ;) 我忘了导入类头 ;) 非常感谢 ;)
    【解决方案2】:

    我找到了解决方案!写错了:

    NSString *Bt = [NSString stringWithFormat:@"%@", Text];
    

    解决方法是从 NSTextField 中获取值:

    NSString *Bt = [NSTextField stringValue];
    

    我仍然不明白为什么无法保存包含此符号的字符串:“”。例如:

    数据库保存了这个:我喜欢编程。 但不是这个:我喜欢“编程”。

    谁能解释一下为什么?

    【讨论】:

    • 你只需要写一个字符串方法来用这个 "" 替换字符 "" 每次有一个 " 所以你要保存的字符串是:我喜欢 ""programming""
    • 为什么你不能只拥有:我喜欢“编程”是因为编译器认为这是字符串的结尾,当它看到一个 " 字符时。就像你写 NSString *words = @"hello there, "Bob""; 那不是去工作。相反,你会使用这个NSString *words = @"hello there, \"Bob\"";
    • 感谢您的回复!我不明白的是如何使用您的程序代码。我尝试从 NSTextField 获取文本。我不知道用户是否使用“”字符。我应该如何将您的解决方案写到我的代码中?
    • 哦,是的!我有一个你可以使用的分类功能,简单而且有效:)
    • 查看此问题中显示的答案以回答您关于在 SQL 中使用引号的问题。如果您需要任何解释,请告诉我:)
    猜你喜欢
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多