diff --git a/services/ans/src/notification_rdb_data_mgr.cpp b/services/ans/src/notification_rdb_data_mgr.cpp index e26739af9834540b017cb7ee60c889a5b4b8b159..b19e606fa271851f71b3fc12a51ce5e37508dee2 100644 --- a/services/ans/src/notification_rdb_data_mgr.cpp +++ b/services/ans/src/notification_rdb_data_mgr.cpp @@ -17,6 +17,7 @@ #include "ans_log_wrapper.h" #include "os_account_manager_helper.h" #include "rdb_errno.h" +#include #include #include @@ -524,10 +525,17 @@ std::string NotificationDataMgr::GetUserTableName(const int32_t &userId, bool cr } if (!createTable) { ANS_LOGD("Do not need create user table."); - return notificationRdbConfig_.tableName; - } - int ret = NativeRdb::E_OK; - { + std::lock_guard lock(rdbStorePtrMutex_); + std::string queryTableSql = "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "'"; + auto absSharedResultSet = rdbStore_->QuerySql(queryTableSql); + int32_t rowCount = 0; + int32_t ret = absSharedResultSet->GetRowCount(rowCount); + if (ret == NativeRdb::E_OK && rowCount == 1) { + userTableInit_.insert(userId); + return tableName; + } + } else { + int ret = NativeRdb::E_OK; std::lock_guard lock(rdbStorePtrMutex_); if (rdbStore_ == nullptr) { return notificationRdbConfig_.tableName; @@ -535,13 +543,13 @@ std::string NotificationDataMgr::GetUserTableName(const int32_t &userId, bool cr std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + tableName + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);"; ret = rdbStore_->ExecuteSql(createTableSql); + if (ret == NativeRdb::E_OK) { + userTableInit_.insert(userId); + ANS_LOGD("createTable %{public}s succeed", tableName.c_str()); + return tableName; + } + ANS_LOGW("createTable %{public}s failed, code: %{code}d", tableName.c_str(), ret); } - if (ret == NativeRdb::E_OK) { - userTableInit_.insert(userId); - ANS_LOGD("createTable %{public}s succeed", tableName.c_str()); - return tableName; - } - ANS_LOGW("createTable %{public}s failed, code: %{code}d", tableName.c_str(), ret); return notificationRdbConfig_.tableName; } return tableName;