언어 설정

Menu
Sites
Language
Code from Encrypted Database Tutorial doesn’t work.

Hello,

I am trying to use code from Encrypted Database Tutorial (Tizen Mobile Native App Programming > Tutorials > Open Source Tutorials-> Encrypted Database Tutorial):

char * resource = app_get_data_path();
int siz = strlen(resource)+10;
char * path = malloc(sizeof(char)*siz);
strncat(path,resource,siz);
strncat(path, "test.db", siz);
memset(path, 0, siz);
sqlite3_open(path, &db);

but it doesn’t work. Value for the path variable is empty string. Please help me what is wrong?

Edited by: Eric Satkimbaev on 08 4월, 2015
답변 바로가기

Responses

5 댓글
Mark as answer
Alex Ashirov

Hi,

It looks like an error. To fix it you just need to put

*path = '\0';

after

char * path = malloc(sizeof(char)*512);

 or use

strcpy(path,resource);

instead of

strncat(path,resource,siz);

Eric Satkimbaev

Hi,

Thank you! I also, removed memset(path, 0, siz) at all. Finally my code looks like:

char * resource = app_get_data_path();

int siz = strlen(resource)+10;

char * path = malloc(sizeof(char)*siz);

strcpy(path,resource);

strncat(path, "test.db", siz);

sqlite3_open(path, &db);

colin Rao

Hi,

As your code, the path should be the full path of the database, it need to be passed to sqlite3_open to let sqlite3 to create(if not exist) and open the database. Right?

But the function call "memset(path, 0, siz);" will set the path to empty before you pass it to sqlite3_open. I think this is the root cause, please try to remove "memset(path, 0, siz);" or put it before "strncat(path,resource,siz);". 

char * resource = app_get_data_path();
int siz = strlen(resource)+10;

char * path = malloc(sizeof(char)*siz);

strncat(path,resource,siz);
strncat(path, "test.db", siz);

sqlite3_open(path, &db);

or,

char * resource = app_get_data_path();
int siz = strlen(resource)+10;

char * path = malloc(sizeof(char)*siz);
memset(path, 0, siz);

strncat(path,resource,siz);
strncat(path, "test.db", siz);

sqlite3_open(path, &db);

 

Eric Satkimbaev

Hi,

Thank you! First variant doesn’t work, but the second one works fine. I preferred to use strcpy() instead of strncat () as shown above.

Alex Dem

Hi,
fyi: looks like eina_strlcat() works similar.Hence you should use copy method (strcpy,eina_strlcpy) at first step or add  null-terminator at first position of allocated memory.
Alexey.