【发布时间】:2016-09-01 07:51:43
【问题描述】:
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"oiwii.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt ="create table if not exists jsonData (status text, message text, mood_name text, description text, c1 text, c2 text c3 text, c4 text, c5 text, font_name text, font_size text, font_color text)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
我已经在上面创建了一个数据库,我正在尝试将 json 对象(数组)添加到我的 sqlite 中。 我的 json 对象看起来像这样- {"moods_name":"mood2","description":"这是心情 2","c1":"D4FF38","c2":"FFA83D","c3":"FFFA9E","c4":"66FFBA ","c5":"63FFE8","font_name":"Default","font_size":"10","font_color":"363636"} 我想获取这个数组
-(BOOL)saveData:(NSString *)status message:(NSString *)message mood_name:(NSString *)mood_name description:(NSString *)description c1:(NSString *)c1 c2:(NSString *)c2 c3:(NSString *)c3 c4:(NSString *)c4 c5:(NSString *)c5 font_name:(NSString *)font_name font_size:(NSString *)font_size font_color:(NSString *)font_color{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into jsonData (status,message, description, c1,c2,c3,c4,c5,font_name,font_size,font_color) values (\"%@\",\"%@\", \"%@\", \"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",status, message, description, c1,c2,c3,c4,c5,font_name,font_size,font_color];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{ NSLog(@"data saved");
return YES;
}
else {
return NO;
}
}
sqlite3_reset(statement);
return NO;
}
更新: 获取数据的代码
-(void)fetchdata
{
arrayfetched = [[NSMutableArray alloc] init];
// Setup the database object
sqlite3 *database;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
//SQLIte Statement
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from moodsdata"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the fetchedarray
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Init the Data Dictionary
NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
NSString *msg = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *status = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *data = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",msg] forKey:@"message"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",status] forKey:@"status"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",data] forKey:@"data"];
[arrayfetched addObject:_dataDictionary];
NSLog(@"array fetched%@",arrayfetched);
}
}
else
{
NSLog(@"No Data Found");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
NSLog(@" fetched array%@",arrayfetched);
}
请帮帮我。
【问题讨论】:
-
您面临的问题是什么?
-
我不知道如何将 json 数组保存到 sqlite 保存数据的方法中。我有一个不同的数据库类
-
而我在哪里调用 json 那个类是不同的。那么如何将该json数据插入sqlite
-
@AmrutGaikwad 看到我的回答。希望这会帮助你。随时问。
标签: ios arrays json sqlite jsonobject