Merge "Fixed int to vc_tts_utterance_status_e for vc_mgr_dbus_send_utterance_status...
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
index 30e0266..4450794 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "vc_cmd_db.h"
 #include "vc_main.h"
-#include "voice_control_command.h"
+#include "vc_command.h"
 #include "voice_control_command_expand.h"
 
 
@@ -56,16 +56,39 @@ const char* vc_db_tag()
 
 
 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
-static sqlite3* db_handle = NULL;
-char* path = NULL;
+static sqlite3* g_db_handle = NULL;
+static sqlite3* g_db_backup_handle = NULL;
+static char* g_path = NULL;
+static char* g_backup_path = NULL;
+static int g_ref_cnt = 0;
+
 int g_fpid = -1;
-int g_ref_cnt = 0;
+int g_db_cnt = 0;
+int g_backup_db_cnt = 0;
+
+static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
+static int __vc_db_begin_transaction(sqlite3* db_handle);
+static int __vc_db_create_table(sqlite3* db_handle, const char* table);
+static int __vc_db_rollback_transaction(sqlite3* db_handle);
+static int __vc_db_commit_transaction(sqlite3* db_handle);
+
+
+int __vc_db_reset_handle(void)
+{
+       if (!g_db_handle || !g_db_backup_handle) {
+               vc_db_finalize();
+               vc_db_initialize();
+       }
+       return 0;
+}
 
-static int __vc_db_transaction(const char* transaction)
+static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
 {
        sqlite3_stmt* pStmt = NULL;
+       int ret = -1;
 
-       int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
+       /* prepare db */
+       ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
                return VC_DB_ERROR_OPERATION_FAILED;
@@ -81,25 +104,25 @@ static int __vc_db_transaction(const char* transaction)
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_begin_transaction(void)
+static int __vc_db_begin_transaction(sqlite3* db_handle)
 {
-       int ret = __vc_db_transaction("BEGIN TRANSACTION");
+       int ret = __vc_db_transaction(db_handle, "BEGIN IMMEDIATE TRANSACTION");
        return ret;
 }
 
-static int __vc_db_rollback_transaction(void)
+static int __vc_db_rollback_transaction(sqlite3* db_handle)
 {
-       int ret = __vc_db_transaction("ROLLBACK TRANSACTION");
+       int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
        return ret;
 }
 
-static int __vc_db_commit_transaction(void)
+static int __vc_db_commit_transaction(sqlite3* db_handle)
 {
-       int ret = __vc_db_transaction("COMMIT TRANSACTION");
+       int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
        return ret;
 }
 
-static int __vc_db_exec_query(const char* sql)
+static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
 {
        char* err_msg = NULL;
 
@@ -107,12 +130,55 @@ static int __vc_db_exec_query(const char* sql)
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
                sqlite3_free(err_msg);
+               err_msg = NULL;
+               return VC_DB_ERROR_OPERATION_FAILED;
+       }
+       return VC_DB_ERROR_NONE;
+}
+
+static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist)
+{
+       char* sql = NULL;
+       *is_exist = false;
+       if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
+               sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_result';");
+       } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
+               sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_info';");
+       } else {
+               return VC_DB_ERROR_INVALID_PARAMETER;
+       }
+
+       if (NULL == sql) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
+
+       SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
+
+       char* err_msg = NULL;
+       char** results = NULL;
+       int ret = sqlite3_get_table(db_handle, sql, &results, NULL, NULL, &err_msg);
+       if (ret != SQLITE_OK) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_get_table return fail, ret(%d), err(%s)", ret, err_msg);
+               sqlite3_free(err_msg);
+               err_msg = NULL;
                return VC_DB_ERROR_OPERATION_FAILED;
        }
+
+       if (0 != atoi(results[1])) {
+               *is_exist = true;
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] table exists (%d)", atoi(results[1]));
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] table doesn't exist (%d)", atoi(results[1]));
+       }
+
+       sqlite3_free_table(results);
+       free(sql);
+       sql = NULL;
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appid)
+static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
 {
        sqlite3_stmt* stmt = NULL;
        char* sql = NULL;
@@ -130,6 +196,21 @@ static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appi
        }
 
        int ret = 0;
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+                       free(sql);
+                       sql = NULL;
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+       }
+
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -191,13 +272,27 @@ static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appi
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
+static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
 {
        SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
 
        sqlite3_stmt* stmt = NULL;
        const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        int ret = 0;
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
@@ -282,7 +377,7 @@ static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
        }
 
        SLOG(LOG_INFO, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
-       SLOG(LOG_INFO, vc_db_tag(), "[SQL] ==== appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
+       SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
 
        sqlite3_reset(stmt);
        sqlite3_clear_bindings(stmt);
@@ -290,7 +385,7 @@ static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
+static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
 {
        SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
 
@@ -310,6 +405,23 @@ static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
                return VC_DB_ERROR_OUT_OF_MEMORY;
        }
+
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+                       free(sql);
+                       sql = NULL;
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -361,10 +473,12 @@ static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
 
        while (SQLITE_ROW == ret) {
                int temp = 0;
+               int ret = -1;
                char* temp_text = NULL;
-               vc_cmd_s* temp_cmd = NULL;
-               temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
-               if (NULL == temp_cmd) {
+               vc_cmd_h temp_cmd;
+               ret = vc_cmd_create(&temp_cmd);
+
+               if (0 != ret) {
                        SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
                        if (NULL != appid) {
                                free(appid);
@@ -380,12 +494,19 @@ static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
 
                temp_text = (char*)sqlite3_column_text(stmt, 7);
                if (NULL != temp_text)
-                       temp_cmd->appid = strdup(temp_text);
+                       vc_cmd_set_appid(temp_cmd, temp_text);
 
-               if (NULL != appid && NULL != temp_cmd->appid) {
-                       if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_cmd->appid, strlen(appid))) {
+               ret = vc_cmd_get_appid(temp_cmd, &temp_text);
+               if (NULL != appid && 0 == ret) {
+                       if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
                                SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
 
+                               free(temp_text);
+                               temp_text = NULL;
+
+                               vc_cmd_destroy(temp_cmd);
+                               temp_cmd = NULL;
+
                                ret = sqlite3_step(stmt);
                                if (SQLITE_DONE == ret)
                                        break;
@@ -394,41 +515,50 @@ static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
                        }
                }
 
+               if (NULL != temp_text) {
+                       free(temp_text);
+                       temp_text = NULL;
+               }
+
                temp = sqlite3_column_int(stmt, 0);
-               temp_cmd->id = temp;
+               vc_cmd_set_id(temp_cmd, temp);
 
                temp = sqlite3_column_int(stmt, 1);
-               temp_cmd->pid = temp;
+               vc_cmd_set_pid(temp_cmd, temp);
 
                temp = sqlite3_column_int(stmt, 2);
-               temp_cmd->type = temp;
+               vc_cmd_set_type(temp_cmd, temp);
 
                temp = sqlite3_column_int(stmt, 3);
-               temp_cmd->format = temp;
+               vc_cmd_set_format(temp_cmd, temp);
 
                temp  = sqlite3_column_int(stmt, 4);
-               temp_cmd->domain = temp;
+               vc_cmd_set_domain(temp_cmd, temp);
 
                temp_text = (char*)sqlite3_column_text(stmt, 5);
                if (NULL != temp_text)
-                       temp_cmd->command = strdup(temp_text);
+                       vc_cmd_set_command(temp_cmd, temp_text);
 
                temp_text = (char*)sqlite3_column_text(stmt, 6);
                if (NULL != temp_text)
-                       temp_cmd->parameter = strdup(temp_text);
+                       vc_cmd_set_unfixed_command(temp_cmd, temp_text);
 
                temp_text = (char*)sqlite3_column_text(stmt, 8);
                if (NULL != temp_text)
-                       temp_cmd->invocation_name = strdup(temp_text);
+                       vc_cmd_set_invocation_name(temp_cmd, temp_text);
 
                temp_text = (char*)sqlite3_column_text(stmt, 9);
                if (NULL != temp_text)
-                       temp_cmd->fixed = strdup(temp_text);
+                       vc_cmd_set_fixed_command(temp_cmd, temp_text);
 
-               if (type == temp_cmd->type) {
+               ret = vc_cmd_get_type(temp_cmd, &temp);
+               if (0 == ret && type == temp) {
                        *cmd_list = g_slist_append(*cmd_list, temp_cmd);
                } else {
-                       SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
+                       SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
+
+                       vc_cmd_destroy(temp_cmd);
+                       temp_cmd = NULL;
                }
                ret = sqlite3_step(stmt);
                if (SQLITE_DONE == ret)
@@ -482,13 +612,28 @@ static int __vc_db_get_pid(const char* appid, int* pid)
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_insert_result(const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
+static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
 {
        SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
 
        sqlite3_stmt* stmt = NULL;
        const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        int ret = 0;
+
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -646,11 +791,10 @@ static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_
                SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
                return VC_DB_ERROR_INVALID_PARAMETER;
        }
-
        if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
                *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
        }
-       SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
+       SLOG(LOG_INFO, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, (*new_cmd) ? *new_cmd : "NULL", invocation_name);
        return VC_DB_ERROR_NONE;
 }
 
@@ -672,12 +816,17 @@ static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** te
        }
 
        char* temp = (char*)calloc(256, sizeof(char));
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
        if (0 == strncasecmp(command, fixed, strlen(fixed))) {
                strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
-               SLOG(LOG_WARN, vc_db_tag(), "==");
-       } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
-               strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
-               SLOG(LOG_WARN, vc_db_tag(), "==");
+               SLOG(LOG_WARN, vc_db_tag(), "@@@");
+       } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
+               command[strlen(command) - strlen(fixed) - 1] = '\0';
+               snprintf(temp, 256, "%s", command);
+               SLOG(LOG_WARN, vc_db_tag(), "@@@");
        }
 
        SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
@@ -689,7 +838,7 @@ static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** te
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_get_result(char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
+static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
 {
        int ret = 0;
        sqlite3_stmt* stmt = NULL;
@@ -706,6 +855,22 @@ static int __vc_db_get_result(char** result_text, int* event, char** msg, int pi
                return VC_DB_ERROR_OUT_OF_MEMORY;
        }
 
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+                       free(sql);
+                       sql = NULL;
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -745,7 +910,7 @@ static int __vc_db_get_result(char** result_text, int* event, char** msg, int pi
        if (-1 == pid)
                SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
        else if (NULL != appid)
-               SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %d;", appid);
+               SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
 
        else
                SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
@@ -848,7 +1013,7 @@ static int __vc_db_get_result(char** result_text, int* event, char** msg, int pi
                                vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
                                if (NULL != temp_unfixed) {
                                        free(temp_unfixed);
-                                       temp_unfixed = NULL;
+                               temp_unfixed = NULL;
                                }
                        } else {
                                vc_cmd_set_command(temp_cmd, temp_command);
@@ -865,6 +1030,7 @@ static int __vc_db_get_result(char** result_text, int* event, char** msg, int pi
                        SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
                        vc_cmd_destroy(temp_cmd);
                        vc_cmd_list_destroy(vc_cmd_list, true);
+                       vc_cmd_list = NULL;
                        if (NULL != *result_text) {
                                free(*result_text);
                                *result_text = NULL;
@@ -911,7 +1077,7 @@ void __vc_db_demandable_client_free(void* data)
        }
 }
 
-static int __vc_db_get_appid(const char* result, GSList** app_list)
+static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
 {
        GSList* temp_app_list = NULL;
 
@@ -919,6 +1085,20 @@ static int __vc_db_get_appid(const char* result, GSList** app_list)
        sqlite3_stmt* stmt = NULL;
        const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
 
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -951,7 +1131,7 @@ static int __vc_db_get_appid(const char* result, GSList** app_list)
                vc_deactivated_app_s* temp_app = NULL;
                temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
                if (NULL == temp_app) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
 
                        if (NULL != temp_app_list) {
                                g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
@@ -983,7 +1163,7 @@ static int __vc_db_get_appid(const char* result, GSList** app_list)
        return VC_DB_ERROR_NONE;
 }
 
-int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
+int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
 {
        GSList* temp_pid_list = NULL;
 
@@ -992,6 +1172,20 @@ int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
        const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
 
 
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -1018,7 +1212,7 @@ int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
                vc_cmd_s* temp_cmd = NULL;
                temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
                if (NULL == temp_cmd) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
 
                        if (NULL != temp_pid_list) {
                                g_slist_free_full(temp_pid_list, free);
@@ -1052,7 +1246,7 @@ int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
        return VC_DB_ERROR_NONE;
 }
 
-static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
+static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
 {
        SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
 
@@ -1060,6 +1254,20 @@ static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
        sqlite3_stmt* stmt = NULL;
        const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
 
+       /* check whether there is a table or not */
+       bool is_exist = false;
+       ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
+       }
+
+       /* prepare db */
        ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
@@ -1140,6 +1348,7 @@ static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
                        SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
                        vc_cmd_destroy(temp_cmd);
                        vc_cmd_list_destroy(vc_cmd_list, true);
+                       vc_cmd_list = NULL;
                        sqlite3_reset(stmt);
                        sqlite3_clear_bindings(stmt);
                        sqlite3_finalize(stmt);
@@ -1180,8 +1389,8 @@ static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
 
        if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
        else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
+       else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
        else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
-       else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
        else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
        else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
 
@@ -1211,74 +1420,454 @@ static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
        return temp_cmd;
 }
 
-int vc_db_initialize(void)
+static int __vc_db_command_destroy(vc_cmd_h vc_command)
 {
-       SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
+       if (NULL == vc_command) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Input parameter is NULL");
+               return VC_ERROR_INVALID_PARAMETER;
+       }
 
-       if (0 < g_ref_cnt) {
-               g_ref_cnt++;
-               return VC_DB_ERROR_NONE;
+       vc_cmd_s* command = NULL;
+       command = (vc_cmd_s*)vc_command;
+
+       SLOG(LOG_DEBUG, vc_db_tag(), "[Destroy command][%p]", command);
+
+       if (NULL != command) {
+               if (NULL != command->command) {
+                       free(command->command);
+                       command->command = NULL;
+               }
+               if (NULL != command->parameter) {
+                       free(command->parameter);
+                       command->parameter = NULL;
+               }
+               if (NULL != command->invocation_name) {
+                       free(command->invocation_name);
+                       command->invocation_name = NULL;
+               }
+               if (NULL != command->appid) {
+                       free(command->appid);
+                       command->appid = NULL;
+               }
+               if (NULL != command->fixed) {
+                       free(command->fixed);
+                       command->fixed = NULL;
+               }
+               if (NULL != command->coordinates) {
+                       free(command->coordinates);
+                       command->coordinates = NULL;
+               }
+               free(command);
+               command = NULL;
        }
 
-       path = (char*)calloc(256, sizeof(char));
-       if (NULL == path) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
-               return VC_DB_ERROR_OUT_OF_MEMORY;
+       return VC_ERROR_NONE;
+}
+
+
+static int __vc_db_create_table(sqlite3* db_handle, const char* table)
+{
+       SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table);
+       char* sql = NULL;
+
+       if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
+               sql = strdup("CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
+                               pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
+       } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
+               sql = strdup("CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
+                               command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
+       } else {
+               return VC_DB_ERROR_INVALID_PARAMETER;
        }
 
-       /* This should be changed to general DB space - TZ_USER_DB */
-       snprintf(path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
+       int ret = __vc_db_exec_query(db_handle, sql);
+       if (ret != VC_DB_ERROR_NONE) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
+               free(sql);
+               sql = NULL;
+               return VC_DB_ERROR_OPERATION_FAILED;
+       }
+       SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
 
+       free(sql);
+       sql = NULL;
+       return VC_DB_ERROR_NONE;
+}
+
+int __vc_db_open_db_for_daemon(char** path, sqlite3** db_handle)
+{
        struct stat stat;
-       int ret = db_util_open(path, &db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
+       int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
        if (ret != SQLITE_OK) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", path, ret, sqlite3_errmsg(db_handle));
-               if (db_handle) {
-                       db_util_close(db_handle);
-                       db_handle = NULL;
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
+               if (*db_handle) {
+                       db_util_close(*db_handle);
+                       *db_handle = NULL;
                }
 
-               free(path);
-               path = NULL;
+               free(*path);
+               *path = NULL;
                return VC_DB_ERROR_OPERATION_FAILED;
        }
 
-       if (lstat(path, &stat) < 0) {
+       if (lstat(*path, &stat) < 0) {
                char buf_err[256];
-               SLOG(LOG_ERROR, vc_db_tag(), "%s", strerror_r(errno, buf_err, sizeof(buf_err)));
-               if (db_handle)
-                       db_util_close(db_handle);
-               db_handle = NULL;
+               SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
+               if (*db_handle)
+                       db_util_close(*db_handle);
+               *db_handle = NULL;
 
-               free(path);
-               path = NULL;
+               free(*path);
+               *path = NULL;
                return VC_DB_ERROR_OPERATION_FAILED;
        }
 
        if (!S_ISREG(stat.st_mode)) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
-               if (db_handle)
-                       db_util_close(db_handle);
-               db_handle = NULL;
+               if (*db_handle)
+                       db_util_close(*db_handle);
+               *db_handle = NULL;
 
-               free(path);
-               path = NULL;
+               free(*path);
+               *path = NULL;
                return VC_DB_ERROR_OPERATION_FAILED;
        }
 
        if (!stat.st_size) {
-           vc_db_create_table();
+               __vc_db_begin_transaction(*db_handle);
+
+               int ret = -1;
+               ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
+               if (ret != VC_DB_ERROR_NONE) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+                       __vc_db_rollback_transaction(*db_handle);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+               ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
+               if (ret != VC_DB_ERROR_NONE) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+                       __vc_db_rollback_transaction(*db_handle);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+
+               __vc_db_commit_transaction(*db_handle);
        }
 
-       if (db_handle) {
+       if (*db_handle) {
                char* err_msg = NULL;
-               static const const char* sql = "PRAGMA journal_mode = WAL";
-               int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
+               static const char* sql = "PRAGMA journal_mode = WAL";
+               int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
                if (ret != SQLITE_OK) {
                        SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
+                       sqlite3_free(err_msg);
+                       err_msg = NULL;
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       }
+       return VC_DB_ERROR_NONE;
+}
+
+static int __vc_db_restore_table(sqlite3* db_handle, const char* table)
+{
+       /* check whether there is a vc_info table or not */
+       bool is_exist = false;
+       int ret = __vc_db_check_table(db_handle, table, &is_exist);
+       if (VC_DB_ERROR_NONE != ret || false == is_exist) {
+               SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", table, ret);
+               if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, table)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
+                       return VC_DB_ERROR_OPERATION_FAILED;
                }
+       } else {
+               SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", table);
        }
+       return VC_DB_ERROR_NONE;
+}
+
+bool __vc_db_connect_db_for_daemon(char** path, sqlite3** db_handle)
+{
+       bool is_connect = false;
+       int ret = __vc_db_open_db_for_daemon(path, db_handle);
+       if (0 != ret) {
+               int cnt = 0;
+               while (cnt <= 5) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
+
+                       usleep(100000);
+                       if (0 == __vc_db_open_db_for_daemon(path, db_handle)) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
+                               is_connect = true;
+                               break;
+                       }
+                       cnt++;
+               }
+       } else {
+               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB for daemon");
+               is_connect = true;
+       }
+       return is_connect;
+}
+
+static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
+{
+       SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
+
+       int ret;
+       char *check_str = "ok";
+       if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Doesn't have integrity(%s), retry to connect after removing", argv[0]);
+               if (0 != remove(g_path)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
+                       g_db_cnt = (g_db_cnt + 1) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
+               }
+               bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
+                       ret = __vc_db_restore_table(g_db_handle, VC_INFO_TABLE);
+                       if (0 != ret) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_INFO_TABLE);
+                       }
+                       ret = __vc_db_restore_table(g_db_handle, VC_RESULT_TABLE);
+                       if (0 != ret) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_RESULT_TABLE);
+                       }
+                       is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                               }
+                       }
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
+       return VC_DB_ERROR_NONE;
+}
+
+int vc_db_initialize_for_daemon(void)
+{
+       SLOG(LOG_INFO, vc_db_tag(), "DB on initialization for daemon");
+
+       if (0 < g_ref_cnt) {
+               g_ref_cnt++;
+               return VC_DB_ERROR_NONE;
+       }
+
+       /* For voice control DB */
+       g_path = (char*)calloc(256, sizeof(char));
+       if (NULL == g_path) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
+       /* This should be changed to general DB space - TZ_USER_DB */
+       snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
+
+       /* For Backup DB */
+       g_backup_path = (char*)calloc(256, sizeof(char));
+       if (NULL == g_backup_path) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
+       snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
+
+       bool is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
+               if (0 != remove(g_path)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
+                       g_db_cnt = (g_db_cnt + 1) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
+               }
+               is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
+                       is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                               }
+                       }
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
+       if (SQLITE_CORRUPT == ret) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
+
+               ret = db_util_close(g_db_handle);
+               if (ret != SQLITE_OK) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s for daemon", ret, sqlite3_errmsg(g_db_handle));
+               }
+               g_db_handle = NULL;
+
+               if (0 != remove(g_path)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
+                       g_db_cnt = (g_db_cnt + 1) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
+               }
+               is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
+                       is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
+                               }
+                       }
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+
+               g_ref_cnt++;
+               SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore for daemon");
+               return 0;
+       }
+
+       is_connect = __vc_db_connect_db_for_daemon(&g_backup_path, &g_db_backup_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file for daemon");
+               if (0 != remove(g_backup_path)) {
+                       g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
+                       snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
+               }
+               is_connect = __vc_db_connect_db_for_daemon(&g_path, &g_db_backup_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
+                       if (0 != vc_db_restore_command()) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
+                       }
+               }
+       }
+
+       g_ref_cnt++;
+
+       SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization for daemon");
+       return VC_DB_ERROR_NONE;
+}
+
+int __vc_db_open_db(char** path, sqlite3** db_handle)
+{
+       int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
+       if (ret != SQLITE_OK) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db for daemon, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
+               if (*db_handle) {
+                       db_util_close(*db_handle);
+                       *db_handle = NULL;
+               }
+
+               free(*path);
+               *path = NULL;
+               return VC_DB_ERROR_OPERATION_FAILED;
+       }
+       return VC_DB_ERROR_NONE;
+}
+
+bool __vc_db_connect_db(char** path, sqlite3** db_handle)
+{
+       bool is_connect = false;
+       int ret = __vc_db_open_db(path, db_handle);
+       if (0 != ret) {
+               int cnt = 0;
+               while (cnt <= 5) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+
+                       usleep(100000);
+                       if (0 == __vc_db_open_db(path, db_handle)) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+                               is_connect = true;
+                               break;
+                       }
+                       cnt++;
+               }
+       } else {
+               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
+               is_connect = true;
+       }
+       return is_connect;
+}
+
+int vc_db_initialize(void)
+{
+       SLOG(LOG_INFO, vc_db_tag(), "DB on initialization");
+
+       if (0 < g_ref_cnt) {
+               g_ref_cnt++;
+               return VC_DB_ERROR_NONE;
+       }
+
+       /* For voice control DB */
+       g_path = (char*)calloc(256, sizeof(char));
+       if (NULL == g_path) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
+       /* This should be changed to general DB space - TZ_USER_DB */
+       snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
+
+       /* For Backup DB */
+       g_backup_path = (char*)calloc(256, sizeof(char));
+       if (NULL == g_backup_path) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
+               return VC_DB_ERROR_OUT_OF_MEMORY;
+       }
+       snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
+
+       bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing");
+               if (0 != remove(g_path)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
+                       g_db_cnt = (g_db_cnt + 1) % 1000;
+                       snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
+               }
+               is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
+                       is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+                       if (true == is_connect) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
+                               if (0 != vc_db_restore_command()) {
+                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                               }
+                       }
+               } else {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
+       if (false == is_connect) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
+               if (0 != remove(g_backup_path)) {
+                       g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
+                       snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
+               }
+               is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
+               if (true == is_connect) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup");
+                       if (0 != vc_db_restore_command()) {
+                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
+                       }
+               }
+       }
+
        g_ref_cnt++;
+
+       SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
        return VC_DB_ERROR_NONE;
 }
 
@@ -1288,56 +1877,58 @@ int vc_db_finalize(void)
        if (0 != --g_ref_cnt)
                return VC_DB_ERROR_NONE;
 
-       if (NULL != path) {
-               free(path);
-               path = NULL;
+       if (NULL != g_path) {
+               free(g_path);
+               g_path = NULL;
+       }
+
+       if (NULL != g_backup_path) {
+               free(g_backup_path);
+               g_backup_path = NULL;
        }
 
-       if (!db_handle)
+       if (!g_db_handle)
                return 0;
+       db_util_close(g_db_handle);
+       g_db_handle = NULL;
 
-       db_util_close(db_handle);
+       if (!g_db_backup_handle)
+               return 0;
+       db_util_close(g_db_backup_handle);
+       g_db_backup_handle = NULL;
 
-       db_handle = NULL;
        return VC_DB_ERROR_NONE;
 }
 
-int vc_db_create_table(void)
+int vc_db_create_table()
 {
-       __vc_db_begin_transaction();
-
-       const char* vc_info_sql = "CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
-                                       command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
-       const char* vc_result_sql = "CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
-                                       pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_exec_query(vc_info_sql);
+       int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
        if (ret != VC_DB_ERROR_NONE) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
-               __vc_db_rollback_transaction();
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
+               __vc_db_rollback_transaction(g_db_handle);
                return VC_DB_ERROR_OPERATION_FAILED;
        }
-       SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
-
-       ret = __vc_db_exec_query(vc_result_sql);
+       ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
        if (ret != VC_DB_ERROR_NONE) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
-               __vc_db_rollback_transaction();
-               return ret;
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
+               __vc_db_rollback_transaction(g_db_handle);
+               return VC_DB_ERROR_OPERATION_FAILED;
        }
-       SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
 
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
-int vc_db_delete_table(const char* table)
+int __vc_db_delete_table(sqlite3* db_handle, const char* table)
 {
        char* sql = NULL;
-       if (0 == strncmp(table, "result", strlen(table))) {
+       if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
                sql = strdup("DELETE FROM vc_result;");
-       } else if (0 == strncmp(table, "command", strlen(table))) {
-               sql = strdup("DELETE_FROM vc_info;");
+       } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
+               sql = strdup("DELETE FROM vc_info;");
        } else {
                return VC_DB_ERROR_INVALID_PARAMETER;
        }
@@ -1347,12 +1938,9 @@ int vc_db_delete_table(const char* table)
                return VC_DB_ERROR_OUT_OF_MEMORY;
        }
 
-       __vc_db_begin_transaction();
-
-       int ret = __vc_db_exec_query(sql);
+       int ret = __vc_db_exec_query(db_handle, sql);
        if (ret != VC_DB_ERROR_NONE) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
-               __vc_db_rollback_transaction();
                free(sql);
                sql = NULL;
                return ret;
@@ -1361,9 +1949,9 @@ int vc_db_delete_table(const char* table)
        free(sql);
        sql = NULL;
 
-       if (0 == strncmp(table, "result", strlen(table))) {
+       if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
                sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
-       } else if (0 == strncmp(table, "command", strlen(table))) {
+       } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
                sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
        }
 
@@ -1372,38 +1960,56 @@ int vc_db_delete_table(const char* table)
                return VC_DB_ERROR_OUT_OF_MEMORY;
        }
 
-       ret = __vc_db_exec_query(sql);
+       ret = __vc_db_exec_query(db_handle, sql);
        if (ret != VC_DB_ERROR_NONE) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
-               __vc_db_rollback_transaction();
                free(sql);
                sql = NULL;
                return ret;
        }
 
        SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
-       __vc_db_commit_transaction();
 
        free(sql);
        sql = NULL;
        return VC_DB_ERROR_NONE;
 }
 
+int vc_db_delete_table(const char* table)
+{
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
+
+       int ret = __vc_db_delete_table(g_db_handle, table);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
+               __vc_db_rollback_transaction(g_db_handle);
+               return ret;
+       }
+
+       __vc_db_commit_transaction(g_db_handle);
+
+       return VC_DB_ERROR_NONE;
+}
+
 int vc_db_begin_transaction(void)
 {
-       int ret = __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       int ret = __vc_db_begin_transaction(g_db_handle);
        return ret;
 }
 
 int vc_db_rollback_transaction(void)
 {
-       int ret = __vc_db_rollback_transaction();
+       __vc_db_reset_handle();
+       int ret = __vc_db_rollback_transaction(g_db_handle);
        return ret;
 }
 
 int vc_db_commit_transaction(void)
 {
-       int ret = __vc_db_commit_transaction();
+       __vc_db_reset_handle();
+       int ret = __vc_db_commit_transaction(g_db_handle);
        return ret;
 }
 
@@ -1416,7 +2022,7 @@ static void __vc_db_remove_space(char** string)
 
        //remove previous space
        if (' ' == temp[0])
-               strncpy(temp, temp + 1, strlen(temp));
+               memmove(temp, temp + 1, strlen(temp));
 
        // remove next space
        if (' ' == temp[strlen(temp) - 1])
@@ -1475,17 +2081,21 @@ static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cm
                // extract fixed command and remove space in front of  '{'
                char *tok_ptr = NULL;
                temp = strtok_r(src_cmd, "{", &tok_ptr);
-               __vc_db_remove_space(&temp);
-               *fixed_cmd = strdup(temp);
-
-               // merge command with fixed and vfixed
-               while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
+               if (NULL != temp) {
                        __vc_db_remove_space(&temp);
+                       *fixed_cmd = strdup(temp);
 
-                       snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
-                       dst_cmd = strdup(merge_cmd);
-                       temp_list = g_slist_append(temp_list, dst_cmd);
-                       SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
+                       // merge command with fixed and vfixed
+                       while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
+                               __vc_db_remove_space(&temp);
+
+                               snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
+                               dst_cmd = strdup(merge_cmd);
+                               temp_list = g_slist_append(temp_list, dst_cmd);
+                               SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
+                       }
+               } else {
+                       *fixed_cmd = strdup(cmd->command);
                }
        } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
                // check string validation
@@ -1544,18 +2154,18 @@ static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cm
        return VC_DB_ERROR_NONE;
 }
 
-int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
+static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
 {
        GSList* cmd_list = NULL;
        char* fixed_cmd = NULL;
        vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
 
        int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
-       if (0 != ret) {
+       if (VC_DB_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
 
                if (NULL != tmp_cmd) {
-                       vc_cmd_destroy((vc_cmd_h)tmp_cmd);
+                       __vc_db_command_destroy((vc_cmd_h)tmp_cmd);
                }
                return ret;
        }
@@ -1579,17 +2189,12 @@ int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
                                else
                                        tmp_cmd->fixed = NULL;
 
-                               ret = __vc_db_insert_commands(pid, type, tmp_cmd);
+                               ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
                                if (ret != VC_DB_ERROR_NONE) {
-                                       if (NULL != fixed_cmd) {
-                                               free(fixed_cmd);
-                                               fixed_cmd = NULL;
-                                       }
-                                       vc_cmd_destroy((vc_cmd_h)tmp_cmd);
                                        break;
                                }
 
-                               if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name) {
+                               if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
                                        char temp[256] = {0, };
                                        snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
                                        if (NULL != tmp_cmd->command)
@@ -1597,13 +2202,8 @@ int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
 
                                        tmp_cmd->command = strdup(temp);
 
-                                       ret = __vc_db_insert_commands(pid, type, tmp_cmd);
+                                       ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
                                        if (ret != VC_DB_ERROR_NONE) {
-                                               if (NULL != fixed_cmd) {
-                                                       free(fixed_cmd);
-                                                       fixed_cmd = NULL;
-                                               }
-                                               vc_cmd_destroy((vc_cmd_h)tmp_cmd);
                                                break;
                                        }
                                }
@@ -1629,9 +2229,8 @@ int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
 
                        g_slist_free(cmd_list);
                        SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
-
-                       return ret;
                }
+
                cmd_list = NULL;
        }
 
@@ -1639,11 +2238,29 @@ int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
                free(fixed_cmd);
                fixed_cmd = NULL;
        }
-       vc_cmd_destroy((vc_cmd_h)tmp_cmd);
-       return VC_DB_ERROR_NONE;
+
+       if (NULL != tmp_cmd) {
+               __vc_db_command_destroy((vc_cmd_h)tmp_cmd);
+               tmp_cmd = NULL;
+               SLOG(LOG_DEBUG, vc_db_tag(), "[DEBUG] command destroy");
+       }
+
+       return ret;
+}
+
+int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
+{
+       int cnt = 0;
+       int ret = -1;
+       while (cnt < 5 && (ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation))) {
+               usleep(100000);
+               SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command(%d), retry cnt(%d)", ret, cnt);
+               cnt++;
+       }
+       return ret;
 }
 
-int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name)
+static int __vc_db_insert_commands_list(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
 {
        GSList *iter = NULL;
        vc_cmd_s *temp_cmd;
@@ -1654,8 +2271,6 @@ int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, ch
 
        SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
 
-       __vc_db_begin_transaction();
-
        for (i = 0; i < count; i++) {
                if (NULL == iter)
                        break;
@@ -1663,7 +2278,7 @@ int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, ch
                temp_cmd = iter->data;
 
                if (NULL == temp_cmd) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
+                       SLOG(LOG_ERROR, vc_db_tag(), "command is NULL");
                        break;
                }
 
@@ -1671,10 +2286,9 @@ int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, ch
                        if (NULL != invocation_name)
                                temp_cmd->invocation_name = strdup(invocation_name);
 
-                       int ret = vc_db_insert_command(pid, type, temp_cmd);
+                       int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
                        if (ret != VC_DB_ERROR_NONE) {
                                SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
-                               __vc_db_rollback_transaction();
                                return ret;
                        }
                } else {
@@ -1683,22 +2297,37 @@ int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, ch
                iter = g_slist_next(iter);
        }
 
-       __vc_db_commit_transaction();
+       return VC_DB_ERROR_NONE;
+}
+
+int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
+{
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
+
+       int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
+       if (ret != VC_DB_ERROR_NONE) {
+               __vc_db_rollback_transaction(g_db_handle);
+               return ret;
+       }
+
+       __vc_db_commit_transaction(g_db_handle);
 
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_get_commands(pid, type, cmd_list);
+       int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               __vc_db_rollback_transaction(g_db_handle);
                return ret;
        }
 
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
@@ -1709,18 +2338,19 @@ int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_
                return VC_DB_ERROR_INVALID_PARAMETER;
        }
 
-       int ret = vc_db_delete_table("result");
+       int ret = vc_db_delete_table(VC_RESULT_TABLE);
        if (0 != ret)
                LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
 
        if (NULL == vc_cmd_list) {
-               __vc_db_begin_transaction();
-               int ret = __vc_db_insert_result(result_text, event, msg, exclusive, NULL);
+               __vc_db_reset_handle();
+               __vc_db_begin_transaction(g_db_handle);
+               int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
                if (ret != VC_DB_ERROR_NONE) {
-                       __vc_db_rollback_transaction();
+                       __vc_db_rollback_transaction(g_db_handle);
                        return ret;
                }
-               __vc_db_commit_transaction();
+               __vc_db_commit_transaction(g_db_handle);
                return VC_DB_ERROR_NONE;
        }
 
@@ -1742,13 +2372,14 @@ int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_
                vc_cmd_s* temp_cmd = NULL;
                temp_cmd = (vc_cmd_s*)vc_command;
 
-               __vc_db_begin_transaction();
-               ret = __vc_db_insert_result(result_text, event, msg, exclusive, temp_cmd);
+               __vc_db_reset_handle();
+               __vc_db_begin_transaction(g_db_handle);
+               ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
                if (ret != VC_DB_ERROR_NONE) {
-                       __vc_db_rollback_transaction();
+                       __vc_db_rollback_transaction(g_db_handle);
                        return ret;
                }
-               __vc_db_commit_transaction();
+               __vc_db_commit_transaction(g_db_handle);
 
                ret = vc_cmd_list_next(vc_cmd_list);
        }
@@ -1758,11 +2389,12 @@ int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_
 
 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_get_result(result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
+       int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               __vc_db_rollback_transaction(g_db_handle);
                return VC_DB_ERROR_OPERATION_FAILED;
        }
 
@@ -1779,9 +2411,9 @@ int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd
                        if (APP_MANAGER_ERROR_NONE != ret) {
                                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
                        }
-                       ret = __vc_db_get_result(result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
+                       ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
                        if (ret != VC_DB_ERROR_NONE) {
-                               __vc_db_rollback_transaction();
+                               __vc_db_rollback_transaction(g_db_handle);
                                return ret;
                        }
                        if (NULL != appid) {
@@ -1790,63 +2422,128 @@ int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd
                        }
                }
        }
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_get_appid_list(const char* result, GSList** app_list)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_get_appid(result, app_list);
+       int ret = __vc_db_get_appid(g_db_handle, result, app_list);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               __vc_db_rollback_transaction(g_db_handle);
                return ret;
        }
 
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_get_result_pid_list(result, pid_list);
+       int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               __vc_db_rollback_transaction(g_db_handle);
                return ret;
        }
 
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
-       int ret = __vc_db_append_commands(pid, type, vc_cmd_list);
+       int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               __vc_db_rollback_transaction(g_db_handle);
                return ret;
        }
 
-       __vc_db_commit_transaction();
+       __vc_db_commit_transaction(g_db_handle);
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
 {
-       __vc_db_begin_transaction();
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_handle);
 
        int ret = 0;
-       ret = __vc_db_delete_commands(pid, type, appid);
+       ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
+       if (ret != VC_DB_ERROR_NONE) {
+               __vc_db_rollback_transaction(g_db_handle);
+               return ret;
+       }
+
+       __vc_db_commit_transaction(g_db_handle);
+       return VC_DB_ERROR_NONE;
+}
+
+int vc_db_backup_command()
+{
+       GSList* list = NULL;
+
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_backup_handle);
+
+       int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
+               __vc_db_rollback_transaction(g_db_backup_handle);
+               return ret;
+       }
+       __vc_db_commit_transaction(g_db_backup_handle);
+
+       ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
+       if (ret != VC_DB_ERROR_NONE) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
+               return ret;
+       }
+
+       __vc_db_begin_transaction(g_db_backup_handle);
+
+       ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
+       if (ret != VC_DB_ERROR_NONE) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
+               __vc_db_rollback_transaction(g_db_backup_handle);
+               return ret;
+       }
+
+       __vc_db_commit_transaction(g_db_backup_handle);
+
+       SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
+       return VC_DB_ERROR_NONE;
+}
+
+int vc_db_restore_command()
+{
+       GSList* list = NULL;
+
+       __vc_db_reset_handle();
+       __vc_db_begin_transaction(g_db_backup_handle);
+
+       int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
+       if (ret != VC_DB_ERROR_NONE) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
+               __vc_db_rollback_transaction(g_db_backup_handle);
+               return ret;
+       }
+       __vc_db_commit_transaction(g_db_backup_handle);
+
+       ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
        if (ret != VC_DB_ERROR_NONE) {
-               __vc_db_rollback_transaction();
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
                return ret;
        }
 
-       __vc_db_commit_transaction();
+       SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
        return VC_DB_ERROR_NONE;
 }