找回密码
 立即注册
查看: 4655|回复: 0

Tizen应用开发数据控制:特定的应用程序之间的数据交换

[复制链接]
发表于 2016-5-7 23:51:20 | 显示全部楼层 |阅读模式
本教程演示了如何获取、设置添加和删除地图数据,和插入、选择、更新和删除SQL数据。
热身
熟悉数据控制API通过学习基本知识:
初始化数据控件
初始化数据控件使用。
处理映射类型数据控件
使用映射类型数据控件。
使用sql类型数据控件
使用sql类型数据控件。

初始化数据控件
初始化数据控件:
  • 使用功能和数据类型的数据(在控制API mobile可穿戴应用程序),包括 < data_control.h > 头文件在您的应用程序:
    1. #include <data_control.h>

    2. #include <sqlite3.h>
    3. #include <stdlib.h>
    4. #include <glib.h>
    5. #include <string.h>
    复制代码
    执行这个应用程序, < sqlite3.h > , < stdlib.h > , < glib.h > , < string.h > 头文件必须包括。
  • 数据控制用例运行2应用程序。 每个应用程序中扮演着不同的角色:一个消费者,另一个提供者。
  • 用户应用程序,添加 http : / /tizen.org/privilege/datasharing http : / /tizen.org/privilege/appManager.launch 应用程序的权限清单文件。
  • 对于提供者应用程序,在IDE中,双击 tizen-manifest.xml ,Tizen清单编辑器中去 先进>数据控制>添加 。 添加 访问权限都 SQL 地图 类型。

处理映射类型数据控件
,添加、删除和使用数据控制API映射类型数据集:
  • 实现提供者应用程序。
    提供者应用程序商店和消费者应用程序提供数据。 提供者应用程序有4个操作:get、添加、删除和设置。使用映射类型数据控制API,这四个操作必须实现回调。
    1. add_value_request_cb(int request_id, data_control_h provider, const char *key,
    2.                      const char *value, void *user_data)
    3. {
    4.    map_data_s* map_data = (map_data_s*)g_hash_table_lookup(map_repository_test, key);

    5.    if (map_data == NULL)
    6.    {
    7.       map_data = (map_data_s*)(g_malloc(sizeof(*map_data)));
    8.       map_data->arr_size = 0;
    9.       map_data->str_arr = (char**) calloc(1, sizeof(char*));
    10.       map_data->str_arr[0] = g_strdup(value);
    11.       g_hash_table_insert(map_repository_test, g_strdup(key), map_data);
    12.    }
    13.    else
    14.    {
    15.       char **new_arr = (char**) calloc(map_data->arr_size+2, sizeof(char*));
    16.       for (int i = 0; i < map_data->arr_size; i++)
    17.       {
    18.          new_arr[i] = g_strdup(map_data->str_arr[i]);
    19.       }
    20.       free(map_data->str_arr);
    21.       new_arr[map_data->arr_size] = g_strdup(value);
    22.       map_data->str_arr = g_strdupv(new_arr);
    23.       free(new_arr);
    24.    }
    25.    map_data->arr_size += 1;

    26.    int ret = data_control_provider_send_map_result(request_id);
    27.    if (ret != DATA_CONTROL_ERROR_NONE)
    28.    {
    29.       dlog_print(DLOG_ERROR, LOG_TAG, "send_map_result failed with error: %d", ret);
    30.    }
    31.    else
    32.    {
    33.       dlog_print(DLOG_INFO, LOG_TAG, "Add value success request_id: %d %d %s",
    34.                  request_id, map_data->arr_size, map_data->str_arr[0]);
    35.    }
    36. }

    37. void
    38. remove_value_request_cb(int request_id, data_control_h provider, const char *key,
    39.                         const char *value, void *user_data)
    40. {
    41.    map_data_s* map_data = (map_data_s*)g_hash_table_lookup(map_repository_test, key);

    42.    if (map_data != NULL)
    43.    {
    44.       int size = map_data->arr_size;
    45.       for (int i = 0; i < size; i++)
    46.       {
    47.          if (strcmp(map_data->str_arr[i], value) == 0)
    48.          {
    49.             free(map_data->str_arr[i]);
    50.             map_data->arr_size--;
    51.          }
    52.       }
    53.       if (map_data->arr_size == 0)
    54.       {
    55.          if (!g_hash_table_remove(map_repository_test, key))
    56.          {
    57.             dlog_print(DLOG_ERROR, LOG_TAG, "remove value failed -%s", key);

    58.             return;
    59.          }
    60.       }
    61.    }

    62.    int ret = data_control_provider_send_map_result(request_id);
    63.    if (ret != DATA_CONTROL_ERROR_NONE)
    64.    {
    65.       dlog_print(DLOG_ERROR, LOG_TAG, "send_map_result failed with error: %d", ret);
    66.    }
    67.    else
    68.    {
    69.       dlog_print(DLOG_INFO, LOG_TAG, "Remove value Success");
    70.    }
    71. }
    复制代码

  • 注册回调中 app_create () 生成的函数(IDE)使用 data_control_provider_map_register_cb () 功能:
    1. void
    2. __free_key(gpointer data)
    3. {
    4.    if (data)
    5.    {
    6.       g_free(data);
    7.       data = NULL;
    8.       dlog_print(DLOG_INFO, LOG_TAG, "Remove key");
    9.    }
    10. }

    11. void
    12. __free_data(gpointer data)
    13. {
    14.    if (data)
    15.    {
    16.       g_free(data);
    17.       data = NULL;
    18.       dlog_print(DLOG_INFO, LOG_TAG, "Remove value");
    19.    }
    20. }

    21. data_control_provider_map_cb map_callback;
    22. void
    23. initialize_datacontrol_provider()
    24. {
    25.    map_repository_test = g_hash_table_new_full(g_str_hash, g_str_equal, __free_key, __free_data);

    26.    map_callback.get_cb = get_value_request_cb;
    27.    map_callback.add_cb = add_value_request_cb;
    28.    map_callback.remove_cb = remove_value_request_cb;
    29.    map_callback.set_cb = set_value_request_cb;

    30.    int result = data_control_provider_map_register_cb(&map_callback);
    31.    if (result != DATA_CONTROL_ERROR_NONE)
    32.    {
    33.       dlog_print(DLOG_ERROR, LOG_TAG, "data_control_provider_map_register_cb failed with error: %d", result);
    34.    }
    35.    else
    36.    {
    37.       dlog_print(DLOG_INFO, LOG_TAG, "Provider map register success");
    38.    }
    39. }       

    40. static bool
    41. app_create(void *data)
    42. {
    43.    // Take necessary actions before main event loop starts
    44.    // Initialize UI resources and application data
    45.    // If this function returns true, the main loop of application starts
    46.    // If this function returns false, the application is terminated
    47.    appdata_s *ad = data;

    48.    create_base_gui(ad);
    49.    initialize_datacontrol_provider()

    50.    return true;
    51. }
    复制代码

  • 实现 用户应用程序。
    用户应用程序请求得到设置,添加和删除功能提供者应用程序和接收提供者应用程序的结果。
    实现回调函数的响应。 回调函数接收请求的响应结果与数据提供者。
    1. // Callback functions
    2. void
    3. map_get_response_cb(int request_id, data_control_h provider,
    4.                     char **ret_value_list, int ret_value_count, bool provider_ret,
    5.                     const char *error, void *user_data)
    6. {
    7.    if (provider_ret)
    8.    {

    9.       dlog_print(DLOG_INFO, LOG_TAG, "The get operation is successful. Value count: %d ", ret_value_count);
    10.       for (int i = 0; i < ret_value_count; i++)
    11.          dlog_print(DLOG_INFO, LOG_TAG, "(%d) Return value: %s ", i, ret_value_list[i]);

    12.    }
    13.    else
    14.    {
    15.       dlog_print(DLOG_ERROR, LOG_TAG, "The get operation for the request %d failed. error message: %s",
    16.                  request_id, error);
    17.    }
    18. }

    19. void
    20. map_set_response_cb(int request_id, data_control_h provider, bool provider_ret,
    21.                     const char *error, void *user_data)
    22. {
    23.    if (provider_ret)
    24.    {
    25.       dlog_print(DLOG_INFO, LOG_TAG, "The set operation is successful");
    26.    }
    27.    else
    28.    {
    29.       dlog_print(DLOG_ERROR, LOG_TAG, "The set operation for the request %d failed. error message: %s",
    30.                  request_id, error);
    31.    }
    32. }

    33. void
    34. map_add_response_cb(int request_id, data_control_h provider, bool provider_ret,
    35.                     const char *error, void *user_data)
    36. {
    37.    if (provider_ret)
    38.    {
    39.       dlog_print(DLOG_INFO, LOG_TAG, "The add operation is successful");
    40.    }
    41.    else
    42.    {
    43.       dlog_print(DLOG_ERROR, LOG_TAG, "The add operation for the request %d failed. error message: %s",
    44.                  request_id, error);
    45.    }
    46. }

    47. void
    48. map_remove_response_cb(int request_id, data_control_h provider, bool provider_ret,
    49.                        const char *error, void *user_data)
    50. {
    51.    if (provider_ret)
    52.    {
    53.       dlog_print(DLOG_INFO, LOG_TAG, "The remove operation is successful");
    54.    }
    55.    else
    56.    {
    57.       dlog_print(DLOG_ERROR, LOG_TAG, "The remove operation for the request %d failed. error message: %s",
    58.                  request_id, error);
    59.    }
    60. }
    复制代码

  • 确定供应商和数据,初始化数据控制处理程序。 必须在执行的初始化过程 app_create () 函数生成的IDE。
    1. data_control_map_response_cb map_callback;
    2. void
    3. initialize_datacontrol_consumer(appdata_s *ad)
    4. {
    5.    const char *provider_id = Your Provider ID;
    6.    const char *data_id = "table";
    7.    int ret;

    8.    // Create data control handler
    9.    ret = data_control_map_create(&(ad->provider_h));
    10.    if (ret != DATA_CONTROL_ERROR_NONE)
    11.    {
    12.       dlog_print(DLOG_ERROR, LOG_TAG, "creating data control provider failed with error: %d", ret);
    13.    }
    14.    ret = data_control_map_set_provider_id(ad->provider_h, provider_id);
    15.    if (ret != DATA_CONTROL_ERROR_NONE)
    16.    {
    17.       dlog_print(DLOG_ERROR, LOG_TAG, "setting provider id failed with error: %d", ret);
    18.    }
    19.    ret = data_control_map_set_data_id(ad->provider_h, data_id);
    20.    if (ret != DATA_CONTROL_ERROR_NONE)
    21.    {
    22.       dlog_print(DLOG_ERROR, LOG_TAG, "setting data id failed with error: %d", ret);
    23.    }

    24.    // Set response callback
    25.    map_callback.get_cb = map_get_response_cb;
    26.    map_callback.set_cb = map_set_response_cb;
    27.    map_callback.add_cb = map_add_response_cb;
    28.    map_callback.remove_cb = map_remove_response_cb;

    29.    // Register response callback
    30.    ret = data_control_map_register_response_cb(ad->provider_h, &map_callback, NULL);
    31.    if (ret != DATA_CONTROL_ERROR_NONE)
    32.    {
    33.       dlog_print(DLOG_ERROR, LOG_TAG, "Registering the callback function failed with error: %d", ret);

    34.       if (ret == DATA_CONTROL_ERROR_IO_ERROR)
    35.       {
    36.          dlog_print(DLOG_ERROR, LOG_TAG, "I/O error");
    37.       }
    38.       else
    39.       {
    40.          dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
    41.       }
    42.    }

    43.    int req_id = 0;

    44.    // Add value
    45.    const char *key = "key";
    46.    const char *value = "value";
    47.    data_control_map_add(provider_map, key, value, &req_id);

    48.    // Get value
    49.    data_control_map_get(provider_map, key, &req_id);

    50.    // Set value
    51.    const char *old_value = "old value";
    52.    const char *new_value = "new value";
    53.    data_control_map_set(provider_map, key, old_value, new_value, &req_id);
    54.    
    55.    // Remove value
    56.    data_control_map_remove(provider_map, key, value, &req_id);
    57. }

    58. static bool
    59. app_create(void *data)
    60. {
    61.    // Take necessary actions before main event loop starts
    62.    // Initialize UI resources and application data
    63.    // If this function returns true, the main loop of application starts
    64.    // If this function returns false, the application is terminated
    65.    appdata_s *ad = data;

    66.    create_base_gui(ad);
    67.    initialize_datacontrol_consumer(ad);

    68.    return true;
    69. }
    复制代码


使用sql类型数据控件
选择插入,更新和删除sql类型数据使用数据控制API:
  • 实现提供者应用程序。
    提供者应用程序商店和消费者应用程序提供数据。 提供者应用程序有4个操作:插入、选择、更新和删除。 使用sql类型数据控制API,这些操作必须实现回调。
    复制代码

  • 注册回调函数和创建数据库:
    1. int
    2. create_database()
    3. {
    4.    dlog_print(DLOG_INFO, LOG_TAG, "%s%s", app_get_data_path(), "test.db");

    5.    int open_flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);

    6.    int ret = sqlite3_open_v2(Your DB Path, &db, open_flags, NULL);
    7.    if (ret != SQLITE_OK)
    8.    {
    9.       dlog_print(DLOG_ERROR, LOG_TAG, "database creation failed with error: %d", ret);

    10.       return ret;
    11.    }

    12.    char* sql_command = "CREATE TABLE IF NOT EXISTS Dictionary (WORD VARCHAR(30), WORD_DESC TEXT, WORD_NUM INT, Point INT)";
    13.    ret = sqlite3_exec(db, sql_command, NULL, NULL, NULL);
    14.    if (ret != SQLITE_OK)
    15.    {
    16.       dlog_print(DLOG_ERROR, LOG_TAG, "database table creation failed with error: %d", ret);
    17.    }
    18.    dlog_print(DLOG_INFO, LOG_TAG, "DB init Success.");

    19.    return ret;
    20. }

    21. void
    22. initialize_datacontrol_provider()
    23. {
    24.    dlog_print(DLOG_INFO, LOG_TAG, "initialize_datacontrol_provider");

    25.    int result = create_database();
    26.    if (result != SQLITE_OK)
    27.       return;

    28.    sql_callback = (data_control_provider_sql_cb *) malloc(sizeof(data_control_provider_sql_cb));
    29.    sql_callback->select_cb = select_request_cb;
    30.    sql_callback->insert_cb = insert_request_cb;
    31.    sql_callback->delete_cb = delete_request_cb;
    32.    sql_callback->update_cb = update_request_cb;
    33.    result = data_control_provider_sql_register_cb(sql_callback, NULL);
    34.    if (result != DATA_CONTROL_ERROR_NONE)
    35.    {
    36.       dlog_print(DLOG_ERROR, "data_control_sql_response_c failed with error: %d", result);
    37.    }
    38.    else
    39.    {
    40.       dlog_print(DLOG_INFO, LOG_TAG, "Provider SQL register success");
    41.    }
    42. }               
    复制代码

  • 实现 用户应用程序
    用户应用程序请求插入、选择、更新和删除功能提供者应用程序提供者应用程序并接收结果。
    实现回调函数的响应。 回调函数接收请求的响应结果与数据提供者。
    1. void
    2. sql_delete_response_cb(int request_id, data_control_h provider, bool provider_result,
    3.                        const char *error, void *user_data)
    4. {
    5.    if (provider_result)
    6.    {
    7.       dlog_print(DLOG_INFO, LOG_TAG, "The delete operation is successful");
    8.    }
    9.    else
    10.    {
    11.       dlog_print(DLOG_ERROR, LOG_TAG, "The delete operation for the request %d failed. error message: %s",
    12.                  request_id, error);
    13.    }
    14. }

    15. void
    16. sql_insert_response_cb(int request_id, data_control_h provider, long long inserted_row_id,
    17.                        bool provider_result, const char *error, void *user_data)
    18. {
    19.    if (provider_result)
    20.    {
    21.       dlog_print(DLOG_INFO, LOG_TAG, "The insert operation is successful");
    22.    }
    23.    else
    24.    {
    25.       dlog_print(DLOG_ERROR, LOG_TAG, "The insert operation for the request %d failed. error message: %s",
    26.                  request_id, error);
    27.    }
    28. }

    29. void
    30. sql_select_response_cb(int request_id, data_control_h provider, result_set_cursor cursor,
    31.                        bool provider_result, const char *error, void *user_data)
    32. {
    33.    if (provider_result)
    34.    {
    35.       dlog_print(DLOG_INFO, LOG_TAG, "The select operation is successful");
    36.    }
    37.    else
    38.    {
    39.       dlog_print(DLOG_ERROR, LOG_TAG, "The select operation for the request %d failed. error message: %s",
    40.                  request_id, error);
    41.    }

    42.    while (data_control_sql_step_next(cursor) == DATA_CONTROL_ERROR_NONE)
    43.    {

    44.       char word[32] = {0,};
    45.       char word_desc[32] = {0,};
    46.       long long word_number = -1;

    47.       data_control_sql_get_text_data(cursor, 0, word);
    48.       data_control_sql_get_text_data(cursor, 1, word_desc);
    49.       data_control_sql_get_int64_data(cursor, 2, &word_number);

    50.       dlog_print(DLOG_INFO, LOG_TAG, "Word: %s, Word DESC: %s, Word NUM: %ld ",
    51.                  word, word_desc, word_number);
    52.    }
    53. }

    54. void
    55. sql_update_response_cb(int request_id, data_control_h provider, bool provider_result,
    56.                        const char *error, void *user_data)
    57. {
    58.    if (provider_result)
    59.    {
    60.       dlog_print(DLOG_INFO, LOG_TAG, "The update operation is successful");
    61.    }
    62.    else
    63.    {
    64.       dlog_print(DLOG_ERROR, LOG_TAG, "The update operation for the request %d failed. error message: %s",
    65.                  request_id, error);
    66.    }
    67. }
    复制代码

  • 确定供应商和数据,初始化数据控制处理程序。 必须在执行的初始化过程 app_create () 函数生成的IDE。
    1. data_control_sql_response_cb sql_callback;
    2. void
    3. initialize_datacontrol_consumer(appdata_s *ad)
    4. {
    5.    int ret;

    6.    const char *provider_id = Your Provider ID;
    7.    const char *data_id = "Dictionary";

    8.    ret = data_control_sql_create(&(ad->provider_h));
    9.    if (ret != DATA_CONTROL_ERROR_NONE)
    10.    {
    11.       dlog_print(DLOG_ERROR, LOG_TAG, "creating data control provider failed with error: %d", ret);
    12.    }

    13.    ret = data_control_sql_set_provider_id(ad->provider_h, provider_id);
    14.    if (ret != DATA_CONTROL_ERROR_NONE)
    15.    {
    16.       dlog_print(DLOG_ERROR, LOG_TAG, "setting provider id failed with error: %d", ret);
    17.    }

    18.    ret = data_control_sql_set_data_id(ad->provider_h, data_id);
    19.    if (ret != DATA_CONTROL_ERROR_NONE)
    20.    {
    21.       dlog_print(DLOG_ERROR, LOG_TAG, "setting data id failed with error: %d", ret);
    22.    }

    23.    sql_callback.delete_cb = sql_delete_response_cb;
    24.    sql_callback.insert_cb = sql_insert_response_cb;
    25.    sql_callback.select_cb = sql_select_response_cb;
    26.    sql_callback.update_cb = sql_update_response_cb;

    27.    ret = data_control_sql_register_response_cb(ad->provider_h, &sql_callback, NULL);
    28.    if (ret != DATA_CONTROL_ERROR_NONE)
    29.    {
    30.       dlog_print(DLOG_ERROR, LOG_TAG, "Registering the callback function failed with error: %d", ret);
    31.    }

    32.    dlog_print(DLOG_INFO, LOG_TAG, "Init data control success");

    33.    int req_id = 0;
    34.    
    35.    // Insert row
    36.    bundle *b = bundle_create();
    37.    bundle_add_str(b, "WORD", "'test'");
    38.    bundle_add_str(b, "WORD_DESC", "'test desc'");

    39.    data_control_sql_insert(provider_sql, b, &req_id);

    40.    // Select row
    41.    char *column_list[2];
    42.    column_list[0] = "WORD";
    43.    column_list[1] = "WORD_DESC";
    44.    
    45.    const char *where = "WORD = 'test'";
    46.    const char *order = "WORD ASC";

    47.    data_control_sql_select(provider_sql, column_list, 2, where, order, &req_id);

    48.    // Add row
    49.    bundle_add_str(b, "WORD", "'test_new'");
    50.    data_control_sql_update(provider_sql, b, where, &req_id);

    51.    // Delete row
    52.    const char *where_delete = "WORD = 'test'";
    53.    result = data_control_sql_delete(provider_sql, where_delete, &req_id);

    54.    // Free memory
    55.    bundle_free(b);
    56. }

    57. static bool
    58. app_create(void *data)
    59. {
    60.    // Take necessary actions before main event loop starts
    61.    // Initialize UI resources and application data
    62.    // If this function returns true, the main loop of application starts
    63.    // If this function returns false, the application is terminated
    64.    appdata_s *ad = data;

    65.    create_base_gui(ad);
    66.    initialize_datacontrol_consumer(ad);

    67.    return true;
    68. }
    复制代码






欢迎来到泰泽网:http://www.tizennet.com/ 泰泽论坛:http://bbs.tizennet.com/ 好没有内涵哦,快到设置中更改这个无聊的签名吧!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|泰泽应用|泰泽论坛|泰泽网|小黑屋|Archiver|手机版|泰泽邮箱|泰泽网 ( 蜀ICP备13024062号-1 )

GMT+8, 2024-4-20 13:07 , Processed in 0.063370 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表