我无法将 NSMutableString
插入到 sqlite。
我从网络服务器成功获取信息并成功创建了 sqlite 文件,但我无法将数据从网络插入到 sqlite。
我觉得问题就出在这里
NSString *insertSQL = @"INSERT INTO Questions VALUES (result)";
但我不确定,我无法解决这个问题。谁能帮帮我?
- (void)getInforamtionFromWeb {
NSURL *url = [NSURL URLWithString:kGetUrl];
data = [NSData dataWithContentsOfURL:url];
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
result = [[NSMutableString alloc] init];
for (NSObject * obj in json)
{
[result appendString:[obj description]];
}
}
-(void)initiatSqlite{
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"Questions.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 Questions (ID INTEGER PRIMARY KEY AUTOINCREMENT, Question Text, AnswerA Text, AnswerB Text, AnswerC Text, AnswerD Text, CorrectAnswer Text, Explanation Text)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
[self Worningtitle:@"Error" Message:@"Failed to create table"];
}
sqlite3_close(_contactDB);
} else {
[self Worningtitle:@"Error" Message:@"Failed to open/create database"];
}
}
}
- (void) insertData
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL = @"INSERT INTO Questions VALUES (result)";
const char *insert_stmt = [insertSQL UTF8String];
NSLog(@"%s",insert_stmt);
sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [result UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Product added");
} else {
NSLog(@"Failed to add Product");
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
最佳答案
你是对的。您的 INSERT
语句不正确。它必须是:
NSString *insertSQL = @"INSERT INTO Questions VALUES (?)";
?
是应用 sqlite3_bind_xxx
的地方。
请记住,您的 Questions
表有很多列,但您只插入了一个值。您应该指定应将值插入到哪一列。示例:
NSString *insertSQL = @"INSERT INTO Questions (Question) VALUES (?)";
将 Question
替换为您的 result
值应放入的正确列名。
顺便说一句 - 您的代码需要做更多的工作才能变得更好。您需要添加更多的错误检查,尤其是 sqlite3_prepare_v2
。
基于想要插入更多数据,你会做这样的事情:
- (BOOL)insertData {
BOOL result = NO; // failed
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) {
const char *insert_stmt = "INSERT INTO Questions(AnswerA,AnswerB,AnswerC,AnswerD,CorrectAnswer,Question) VALUES (?,?,?,?,?,?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [answerA UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [answerB UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [answerC UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [answerD UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [correctAnswer UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [question UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"Product added");
result = YES;
} else {
NSLog(@"Failed to add Product: %s", sqlite3_errmsg(_contactDB));
}
sqlite3_finalize(statement);
} else {
NSLog(@"Unable to prepare statement: %s", sqlite3_errmsg(_contactDB);
}
sqlite3_close(_contactDB);
} else {
NSLog(@"Unable to open database: %@", sqlite3_errmsg(_contactDB));
}
return result;
}
请注意,您需要使用对包含每列数据的实际变量/值的引用来更新每个 sqlite3_bind_text
调用。
您也不需要为 ID
绑定(bind)值,因为它已设置为自动填充。
注意所有正确的错误检查。我也这样做了 insertData
返回一个 BOOL
指示数据是否已成功添加。
关于ios - 如何在 Objective-C 中将字符串插入到 sqlite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36950599/