-(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;
}wie einfügen und json Array in SQLite ios
i eine Datenbank erstellt haben, speichern oben und ich versuche, das JSON-Objekt (Array) in meine SQLite hinzuzufügen. mein JSON-Objekt sieht so aus - {"Stimmungen_Name": "Stimmungs2", "Beschreibung": "Das ist Stimmung 2", "C1": "D4FF38", "C2": "FFA83D", "C3": " FFFA9E "," c4 ":" 66FFBA "," c5 ":" 63FFE8 "," font_name ":" Standard "," font_size ":" 10 "," font_color ":" 363636 "} und ich möchte das holen
Array-(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;
}
Update: Code, um Daten abzurufen
-(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);
}
mir bitte helfen.
Was ist das Problem Sie konfrontiert sind? –
Ich weiß nicht, wie man JSON-Array in SQLite speichern Daten Methode speichern. Ich habe eine andere Klasse für die Datenbank –
und wo ich json rufe diese Klasse ist anders. so, wie diese JSON-Daten in SQLite einfügen –