语言

Menu
Sites
Language
How to copy sqlite data/.db file in application at runtime?

Hello,

In my application i am using database created using sqlite browser and which is in res folder and res folder is read only. So,I want to copy that .db in my application at runtime in shared/data folder.
So,will anyone help me?

响应

3 回复
Armaan-Ul- Islam

I hope I will.

 

You do not need anything exhaustive to copy sqlite database in runtime. It's just C code for copying file, That's It!!!!

I've written a code sample and tested copying '.db' file from 'res' folder to 'shared/data' folder successfully.

 

FILE *src,*dst;

char srcURL[100];
char dstURL[100];
int ch;

sprintf(srcURL,"%s%s",app_get_resource_path(),"ghi.db");
sprintf(dstURL,"%s%s",app_get_shared_data_path(),"ijk.db");

dlog_print(DLOG_DEBUG,"self","%s",srcURL);
dlog_print(DLOG_DEBUG,"self","%s",dstURL);

src=fopen(srcURL,"r");
dst=fopen(dstURL,"w");

while( ( ch = getc(src) ) != EOF )
    fputc(ch, dst);


fclose(src);
fclose(dst);

 

bhoomika rathod

Actually i have a table in 1st db file and i want to copy that table in second db. So after this code how to acces data in application??

Armaan-Ul- Islam

Seems "How to copy sqlite data/.db file in application at runtime?" query is responsed. Now "Copying a specific table from one database to another database" is quite a different SQL query topic. I would suggest lets leave this thread answered and Have a new beginning in a new post. 'Mark as answer' if the requirement stated in title is taken cared. That would be more convenient for other developers having difficulty on the same scenario.