[Bugfix]fix data init fail when database corruption.

Only Configure: No
Affected branch: master
Affected module: data
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I9fc5d1ffa754bfc349cdc0e8ee6d3c74eccf0a06
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
index 995b7e4..421383b 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
@@ -17,6 +17,8 @@
 #define APN_DB_READED 0
 #define APN_DB_ERR 1
 #define APN_DB_CREATE 2
+#define APN_TABLE_CREATE 3
+#define APN_TABLE_NEED_CREATE 4
 #define RESULT_OK (0)
 #define RESULT_ERROR (-1)
 
@@ -721,9 +723,69 @@
     pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
+check_apn_table_cb(void *data, int argc, char **argv, char **azColName)
+{
+    if(data==NULL)
+    {
+        return 1;
+    }
+    int *temp = (int *)data;
+    for(int i=0; i<argc; i++){
+       LYINFLOG("[%s]%s = %s", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    *temp = atoi(argv[0]);
+    LYINFLOG("\nThe number is %d\n", *temp);
+    return 0;
+}
+int check_apn_table(char*path)
+{
+    char *zErrMsg = 0;
+    int rc;
+    char *sql;
+    int num = -1;
+
+    /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
+    rc = sqlite3_open(path, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
+        return 1;
+    }
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    sql = "SELECT COUNT(*) FROM sqlite_master where type ='table' and name = 'LYNQAPN'";
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, check_apn_table_cb, &num, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
+        return 1;
+    }
+    LYINFLOG("Table LYNQAPN check successfully\n");
+    sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
+    if(num <= 0)
+    {
+        LYINFLOG("No table LYNQAPN, num = %d\n", num);
+        return APN_TABLE_NEED_CREATE;
+    }
+    else
+    {
+        LYINFLOG("The table LYNQAPN exist, num = %d\n", num);
+        return APN_TABLE_CREATE;
+    }
+
+    return 0;
+}
 static int init_apn_db()
 {
     int db_ret=0;
+    int check_ret=0;
     int table_ret=0;
 
     pthread_mutex_init(&s_qser_data_database_mutex, NULL);   
@@ -741,7 +803,26 @@
     }
     else if(db_ret==APN_DB_READED)
     {
-        LYERRLOG("[%s] apn db has be build!!!",__FUNCTION__);
+        LYINFLOG("[%s] apn db has be build!!!",__FUNCTION__);
+        check_ret = check_apn_table(APN_DB_PATH);
+        if(check_ret == APN_TABLE_NEED_CREATE)
+        {
+            table_ret = create_apn_table(APN_DB_PATH);
+            if(table_ret!=0)
+            {
+                LYERRLOG("create apn table fail!!!");
+                return 1;
+            }
+            apn_db_add(0, &default_apn);
+        }
+        else if(check_ret == APN_TABLE_CREATE)
+        {
+            LYINFLOG("Table LYNQAPN has created");
+        }
+        else
+        {
+            LYERRLOG("check table fail!!!, ret = %d", check_ret);
+        }
     }
     else
     {