我是iphone开发的新手。我想在表sqlite的行中存储一个NSDictionary。 我用Google搜索并发现了很多结果,但它对我不起作用。
这是我的字典:
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"24",@"id",@"Folder",@"name",@"300 MB",@"size", nil];
我也在这个网站上搜索过,但是我对类似帖子中的任何内容都不了解。 请在表SQLite DB的行中向我解释更多关于添加(存储)NSDictionary的信息。
答案 0 :(得分:3)
您可以通过创建一个包含三列的表(使用create table query)来完成此操作。然后将字典中的这些对象插入到这些列中(使用插入查询)。
编辑:用于插入数据使用查询:
NSString *query = [NSString stringWithFormat:@"insert into tablename (column1, column2,column3) values('%@','%@','%@')",[dic objectForKey:@"id"],[dic objectForKey:@"name"],[dic objectForKey:@"size"]];
NSLog(@"query : %@",query);
[self executeQuery:query];
在.h类中创建一个sqlite实例:
sqlite3 *database; and import: #import<sqlite3.h>
在.m类中添加以下方法:
-(NSString *) dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"PATH %@",[documentsDirectory stringByAppendingPathComponent:DatabaseName]);
return [documentsDirectory stringByAppendingPathComponent:DatabaseName];
}
/*==================================================================
METHOD FOR INSERTING DATA IN DATABASE
==================================================================*/
-(void)executeQuery:(NSString *)query
{
//NSLog(@"QUERY : %@",query);
sqlite3_stmt *statement;
if(sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK)
{
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) != SQLITE_DONE)
{
sqlite3_finalize(statement);
}
}
else
{
NSLog(@"query Statement Not Compiled");
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
else
{
NSLog(@"Data not Opened");
}
}
阅读数据:
/*==================================================================
METHOD FOR Fetching Data FROM DATABASE
==================================================================*/
NSString *query = [NSString stringWithFormat:@"select * from table_name"];
NSMutableArray *tableData = [self fetchingDataFromTable:query];
//此处表数据数组包含所有记录
-(NSMutableArray *)fetchingDataFromTable:(NSString *)query;
{
NSLog(@"QUERY : %@",query);
NSString *idToReturn=@"";
NSMutableArray *returnArray = [NSMutableArray new];
if(sqlite3_open([[self dataFilePath] UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_ROW)
{
NSMutableDictionary *temp= [NSMutableDictionary new];
const char *s;
s=(char *)sqlite3_column_text(statement, 0);
if(s==NULL)
{
idToReturn=@"";
}
else
{
idToReturn =[NSString stringWithUTF8String:s];
}
[temp setObject:idToReturn forKey:@"id"];
s=(char *)sqlite3_column_text(statement, 1);
if(s==NULL)
{
idToReturn=@"";
}
else
{
idToReturn =[NSString stringWithUTF8String:s];
}
[temp setObject:idToReturn forKey:@"name"];
s=(char *)sqlite3_column_text(statement, 2);
if(s==NULL)
{
idToReturn=@"";
}
else
{
idToReturn =[NSString stringWithUTF8String:s];
}
[temp setObject:idToReturn forKey:@"size"];
if (temp != nil)
{
[returnArray addObject:temp];
temp = nil;
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
return returnArray;
}
希望它对你有所帮助。
答案 1 :(得分:1)
通过这个Link,这里有一个很好的例子
答案 2 :(得分:0)
- (NSMutableDictionary *)executeQuery:(NSString *)query
{
sqlite3_stmt *stmt;
const char *tail;
sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, &tail);
if (stmt == NULL)
return nil;
int status;
int num_cols;
int i;
int j = 1;
int type;
id obj;
NSString *key;
NSMutableArray *result;
NSMutableDictionary *row,*roww;
j = 0;
result = [NSMutableArray array];
roww = [NSMutableDictionary dictionary];
while ((status = sqlite3_step(stmt)) != SQLITE_DONE) {
if (status != SQLITE_ROW)
{
continue;
}
else
{
row = [NSMutableDictionary dictionary];
num_cols = sqlite3_data_count(stmt);
for (i = 0; i < num_cols; i++) {
obj = nil;
type = sqlite3_column_type(stmt, i);
switch (type) {
case SQLITE_INTEGER:
obj = [NSNumber numberWithLongLong:sqlite3_column_int64(stmt, i)];
break;
case SQLITE_FLOAT:
obj = [NSNumber numberWithDouble:sqlite3_column_double(stmt, i)];
break;
case SQLITE_TEXT:
obj = [NSString stringWithUTF8String:sqlite3_column_text(stmt, i)];
break;
case SQLITE_BLOB:
obj = [NSData dataWithBytes:sqlite3_column_blob(stmt, i)
length:sqlite3_column_bytes(stmt, i)];
break;
case SQLITE_NULL:
obj = [NSNull null];
break;
default:
break;
}
key = [NSString stringWithUTF8String:sqlite3_column_name(stmt, i)];
[row setObject:obj forKey:key];
}
NSString *keyq = [NSString stringWithFormat:@"%d",j];
[roww setObject:row forKey:keyq];
j++;
[roww setValue:@"1" forKey:@"Success"];
}
}
sqlite3_finalize(stmt);
// NSLog(@"%@",roww);
return roww;
}
答案 3 :(得分:0)
从iOS中的表格中获取数据:
-(void)getData:(NSString *) tablename
{
NSMutableDictionary * tabledata= [[NSMutableDictionary alloc] init];
NSString *str = @"select * from ";
NSString *strQuery = [str stringByAppendingString:tablename];
tabledata = [db executeQuery:strQuery];
NSDictionary *dic = [tabledata valueForKey:@"0"];
NSLog(@"dic is:%@",dic);
}
答案 4 :(得分:0)
我创建了动态代码,它将返回包含所有行的所有列的列表
-(NSMutableDictionary*)getRowsFromTableName:(NSString*)tableName
{
NSString *databasePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
// stringByAppendingPathComponent: @"sqlcipher.db"];
sqlite3 *db;
bool sqlcipher_valid = NO;
NSMutableDictionary *dbData = [[NSMutableDictionary alloc]init];
NSMutableArray *columnList = [[NSMutableArray alloc]init];
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
const char* key = [keyString UTF8String];
sqlite3_key(db, key, strlen(key));
if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
if(sqlite3_prepare_v2(db, "PRAGMA cipher_version;", -1, &stmt, NULL) == SQLITE_OK) {
if(sqlite3_step(stmt)== SQLITE_ROW) {
const unsigned char *ver = sqlite3_column_text(stmt, 0);
if(ver != NULL) {
sqlcipher_valid = YES;
// password is correct (or database initialize), and verified to be using sqlcipher
char *errMsg;
const char *query = [[NSString stringWithFormat:@"select * from %@",tableName] UTF8String]; //where name = 'abc'";
if (sqlite3_exec(db, query, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
// isSuccess = NO;
NSLog(@"Failed to open data into table");
[dbData setValue:@"Failed to open data into table" forKey:@"Message"];
[dbData setValue:@"Failed" forKey:@"status"];
return dbData;
}
else
{
NSLog(@"Data open Successfully");
int num_cols;
if(sqlite3_prepare_v2(db, query, -1, &stmt, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW)
{
num_cols = sqlite3_data_count(stmt);
NSMutableDictionary *columnDic = [[NSMutableDictionary alloc]init];
for (int i = 1; i < num_cols; i++) {
NSString *colValue = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, i)];
NSString *colName = [NSString stringWithUTF8String:(char*)sqlite3_column_name(stmt, i)];
[columnDic setValue:colValue forKey:colName];
NSLog(@"%@",columnDic);
}
[columnList addObject:columnDic];
columnDic = nil;
}
NSLog(@"%@",columnList);
[dbData setValue:columnList forKey:@"data"];
}
}
}
}
else
{
NSLog(@"Error while binding variable. '%s'", sqlite3_errmsg(db));
[dbData setValue:[NSString stringWithFormat:@"Error while binding variable. '%s'", sqlite3_errmsg(db)] forKey:@"Message"];
[dbData setValue:@"Failed" forKey:@"status"];
return dbData;
}
sqlite3_finalize(stmt);
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(db));
[dbData setValue:[NSString stringWithFormat:@"Error while creating update statement. '%s'", sqlite3_errmsg(db)] forKey:@"Message"];
[dbData setValue:@"Failed" forKey:@"status"];
return dbData;
}
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(db));
[dbData setValue:[NSString stringWithFormat:@"Error while creating update statement. '%s'", sqlite3_errmsg(db)] forKey:@"Message"];
[dbData setValue:@"Failed" forKey:@"status"];
return dbData;
}
sqlite3_close(db);
}
NSLog(@"%@",dbData);
return dbData;
}