Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sqlite数据库(objective-c)中保存NSTextField中的信息_Objective C_Cocoa_Sqlite - Fatal编程技术网

在sqlite数据库(objective-c)中保存NSTextField中的信息

在sqlite数据库(objective-c)中保存NSTextField中的信息,objective-c,cocoa,sqlite,Objective C,Cocoa,Sqlite,几个月以来,我对编码很感兴趣。目前,我正在编写一个程序,该程序将文本字段中的信息保存在sqlite3数据库中。我正在为Mac的Xcode中的Objective C编程这个项目。不幸的是,我在将数据插入数据库时遇到了问题。当我在终端中插入它并打开数据库时,我只是在单元格中为字符串获取以下信息: 这是我的密码: NSString *Bt = [NSString stringWithFormat:@"%@", Text]; //Theres a IBOutlet NSTextField *Text;

几个月以来,我对编码很感兴趣。目前,我正在编写一个程序,该程序将文本字段中的信息保存在sqlite3数据库中。我正在为Mac的Xcode中的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];
    }

}
}

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

致以最良好的祝愿,
Robby

我找到了解决办法!这样写是错误的:

NSString *Bt = [NSString stringWithFormat:@"%@", Text];
解决方案是从NSTEXT字段获取值:

NSString *Bt = [NSTextField stringValue];
我仍然不明白的是,为什么不能保存包含以下符号的字符串:“”。例如:

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


有人能解释一下原因吗?

从你的
IBOUTLET
连接的外观来看,你的代码,Bt等于TextField。但是你想要的是TextField中的文本值。所以你可以使用这个:
NSString*Bt=[nsstringwithformat:@“%@”,text.stringValue];
NSTextField有一个名为stringValue的属性,这将返回NSString,其中包含文本字段Text中的值

允许在数据库中使用引号 我能想到的最简单的方法是在NSString上有一个category方法。因此,无论您要将用户的字符串或文本保存到数据库中,都可以使用此函数插入到format语句中。示例如下:

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
@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类别的植入文件

@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
@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]];
...

您只需编写一个string方法,在每次出现“时”时将字符“this”替换为“this”,因此您要保存的字符串将是:I like“programming”为什么您不能只有:I like“programming”是bc编译器在看到“字符。比如当你写
NSString*words=@”你好,鲍勃“;
这是行不通的。你可以用这个
NSString*words=@“你好,鲍勃”;
谢谢您的回复!我不明白的是如何使用您的程序代码。我试图从NSTextField中获取文本。我不知道用户是否使用“”字符。我应该如何将您的解决方案写入我的代码中?哦,是的!我有一个您可以使用的分类函数,很简单,它可以工作:)查看此问题中显示的答案,以回答有关在SQL中使用引号的问题。如果您需要任何解释,请告诉我:)谢谢您的回复!当我添加代码时,会收到以下错误消息:没有可见的@interface for`NSString`声明选择器`reformForSQLQuries`。现在它可以工作了;)我忘记导入类标题;)非常感谢;)