Repository navigation
Expand file tree
/
Copy pathDatabaseAPI.m
More file actions
119 lines (86 loc) · 3.61 KB
/
Copy pathDatabaseAPI.m
File metadata and controls
119 lines (86 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// DatabaseAPI.m
// Test
//
// Created by Fabio L Brandao FH on 13/11/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "DatabaseAPI.h"
@implementation DatabaseAPI
- (void) createDatabase{
NSString *docsDir;
NSArray *dirPaths;
//GET PATH OF DOCUMENTS DIRECTORY
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//ADD docsDir to array of paths
docsDir = [dirPaths objectAtIndex:0];
//NSLog(@"Diretorio do banco: %@", [dirPaths objectAtIndex:0]);
//BUILD THE PATH TO THE DATABASE FILE
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"images.db"]];
NSLog(@"Diretorio do banco: %@", _databasePath);
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:_databasePath] == NO) {
const char *dbPath = [_databasePath UTF8String];
if(sqlite3_open(dbPath, &_imagesDB) == SQLITE_OK){
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGES (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, URL TEXT, IMAGE TEXT)";
if(sqlite3_exec(_imagesDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK){
NSLog(@"Falha na criacao da tabela images");
}else{
NSLog(@"Tabela images criada com sucesso!");
}
NSLog(@"Fechando database...");
sqlite3_close(_imagesDB);
}else{
NSLog(@"Falha ao tentar abrir database _images.BD");
}
}else{
NSLog(@"Database ja existe -> Nao sera criado!");
}
}
- (void) saveImageToDirectory:(NSData *)image{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/%@",
documentsDirectory];
//create content - four lines of text
NSString *content = @"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
- (void) saveMetaDataToBD:(NSString *)name:(NSString *)url:(NSString *)imageDirectory{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_imagesDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO IMAGES (NAME, URL, IMAGE) VALUES (\"%@\", \"%@\", \"%@\")", name, url, imageDirectory];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_imagesDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
} else {
NSLog(@"Failed to add contact");
}
sqlite3_finalize(statement);
sqlite3_close(_imagesDB);
}
}
- (void) getData{
}
- (void) readFromPlist{
NSString *plistDir;
NSArray *plistPaths;
//GET PATH OF DOCUMENTS DIRECTORY
plistPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//ADD docsDir to array of paths
plistDir = [plistPaths objectAtIndex:0];
_plist = [[NSString alloc] initWithString:[plistDir stringByAppendingPathComponent:@"settings.plist"]];
}
- (void) writeOnPlist{
}
@end