<p>sqlite3 database;
if
(sqlite3_open([[
self
dataFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert
(0, @
"Failed to open database"
);
}
else
{
char
*errorMsg;
NSString
*createSQL1 = @"CREATE TABLE IF NOT EXISTS LOGS (ID INTEGER PRIMARY KEY
AUTOINCREMENT, PHONENUMBER_DATA TEXT, NOW_DATE DATE, CALL_TIME TEXT);";
if
(sqlite3_exec (database, [createSQL1 UTF8String],
NULL
,
NULL
, &errorMsg) != SQLITE_OK) {
sqlite3_close(database);
NSLog
(@
"Error creating table: %s"
, errorMsg);
}
NSString
*createSQL2 = @"CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY
AUTOINCREMENT, NAME_DATA TEXT, PHONENUMBER_DATA TEXT, MEMO_DATA TEXT);";
if
(sqlite3_exec (database, [createSQL2 UTF8String],
NULL
,
NULL
, &errorMsg) != SQLITE_OK) {
sqlite3_close(database);
NSLog
(@
"Error creating table: %s"
, errorMsg);
}
}
NSMutableArray
*mutableLogData = [
NSMutableArray
array];
NSString
*query = @"SELECT ID ,PHONENUMBER_DATA, NOW_DATE, CALL_TIME FROM LOGS ORDER
BY ID DESC";
sqlite3_stmt *statement;
if
(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement,
nil
) == SQLITE_OK) {
while
(sqlite3_step(statement) == SQLITE_ROW) {
int
idData = sqlite3_column_int(statement, 0);
char
*numberData = (
char
*)sqlite3_column_text(statement, 1);
double
dateData = sqlite3_column_double(statement, 2);
char
*callData = (
char
*)sqlite3_column_text(statement, 3);
if
(numberData !=
nil
&& dateData != 0 && idData != 0){
NSNumber
*idValue = [
NSNumber
numberWithInt:idData];
NSString
*numberValue = [[
NSString
alloc] initWithUTF8String:numberData];
NSDate
*dateValue = [
NSDate
dateWithTimeIntervalSince1970:dateData];
NSString
*callTimeValue = [[
NSString
alloc] initWithUTF8String:callData];
NSArray
*dataArray = [
NSArray
arrayWithObjects:idValue,
numberValue,
dateValue,
callTimeValue,
nil
];
[mutableLogData addObject:dataArray];
[numberValue release];
[callTimeValue release];
}
}
}
sqlite3_finalize(statement);
char
*errorMsg;
char
*
delete
=
"DELETE FROM ADDRESS WHERE ID = ?"
;
sqlite3_stmt *stmt;
if
( sqlite3_prepare_v2(database,
delete
, -1, &stmt,
nil
) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, [idData intValue]);
}
if
(sqlite3_step(stmt) != SQLITE_DONE) {
NSLog
(@
"Error deleting table: %s"
, errorMsg);
}
sqlite3_finalize(stmt);
char
*errorMsg;
char
*update =
"INSERT INTO LOGS (PHONENUMBER_DATA, NOW_DATE, CALL_TIME) VALUES (?,?,?);"
;
if
(sqlite3_open([[
self
dataFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert
(0, @
"Failed to open database"
);
}
sqlite3_stmt *stmt;
if
( sqlite3_prepare_v2(database, update, -1, &stmt,
nil
) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [callNum UTF8String], -1,
NULL
);
sqlite3_bind_double(stmt, 2, [nowDate timeIntervalSince1970]);
sqlite3_bind_text(stmt, 3, [callTime UTF8String], -1,
NULL
);
};
if
( sqlite3_step(stmt) != SQLITE_DONE)
NSLog
(@
"Error updating table: %s"
, errorMsg);
sqlite3_finalize(stmt);
char
*errorMsg;
char
*update =
"UPDATE ADDRESS SET NAME_DATA=? , PHONENUMBER_DATA=? , MEMO_DATA=? WHERE ID=?;"
;
sqlite3_stmt *stmt;
if
( sqlite3_prepare_v2(database, update, -1, &stmt,
nil
) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [name UTF8String], -1,
NULL
);
sqlite3_bind_text(stmt, 2, [phoneNum UTF8String], -1,
NULL
);
sqlite3_bind_text(stmt, 3, [memo UTF8String], -1,
NULL
);
sqlite3_bind_int(stmt, 4, idNum);
}
if
( sqlite3_step(stmt) != SQLITE_DONE)
NSLog
(@
"Error updating table: %s"
, errorMsg);
sqlite3_finalize(stmt);
</p>