语言

Menu
Sites
Language
How to write data in .db file copied in shared/data folder from res folder runtime?

Here is my code.

I want to update column data in copied quote2.db file. 

 /*copy database*/
FILE *src,*dst;

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

sprintf(srcURL,"%s%s",app_get_resource_path(),"quote.db");
sprintf(dstURL,"%s%s",app_get_shared_data_path(),"quote2.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);

I opened quote2.db here.

char *errmsg;
char *resource_path = app_get_shared_data_path();
dlog_print(DLOG_DEBUG, LOG_TAG, "shared data path is : %s",resource_path);

int size = strlen(resource_path) + 10;

char path2[255];
strcpy(path2,resource_path);
strncat(path2,"quote2.db",size);

dlog_print(DLOG_DEBUG, LOG_TAG, "shared db path is : %s", path2);
int ret = sqlite3_open_v2(path2, &db_handle,SQLITE_OPEN_READWRITE, NULL);

if (ret == SQLITE_OK) {
    dlog_print(DLOG_DEBUG, LOG_TAG, "shared db open success %d",ret);
}
else {
dlog_print(DLOG_DEBUG, LOG_TAG, "shared db open failed %s",&errmsg);
			
}

Here i have performed update query but it is not working and not giving any error.

int
image_set_cb(void *data,int argc,char **argv, char **azColName)
{
    	appdata_s *ad=data;

	dlog_print(DLOG_DEBUG,LOG_TAG,"success");

		const char *path = app_get_resource_path();
		char img[PATH_MAX];
		snprintf(img, sizeof(img), "%sicons/favourite.png", path);
		elm_image_file_set(ad->fav_img, img, NULL);

		return 0;
}

int
fav_col_change_cb(void *data,int argc,char **argv, char **azColName)
{
	appdata_s *ad=data;
	
	if (argv[3]==NULL) {

				int ret;
				char sql[255], *errmsg;
			
				sprintf(sql,"UPDATE quotes SET Favourites=1 WHERE QuoteID=%d;",o_array[array_index]);

				ret = sqlite3_exec(db_handle, sql,image_set_cb, ad, &errmsg);

				if (ret == SQLITE_OK) {
					dlog_print(DLOG_DEBUG, LOG_TAG,"fav_col_change_cb READ SUCCESSFULLY: %s",sql);

				} else {
					dlog_print(DLOG_DEBUG, LOG_TAG, "fav_col_change_cb READ FAILED: %s",errmsg);
				}

		}

	return 0;
}

void
add_to_fav_cb(void *data, Evas_Object *obj, void *event_info)
{
	appdata_s *ad=data;

	{
		int ret;
		char sql[255], *errmsg;
		cnt++;
		sprintf(sql, "SELECT * FROM quotes WHERE QuoteID=%d;",o_array[array_index]);

		ret = sqlite3_exec(db_handle, sql,fav_col_change_cb, ad, &errmsg);

		if (ret == SQLITE_OK) {
			dlog_print(DLOG_DEBUG, LOG_TAG,"add_to_fav_cb SUCCESS: %d", ret);
		} else {
			dlog_print(DLOG_DEBUG, LOG_TAG, "add_to_fav_cb FAILED: %s",errmsg);
		}

	}

}

 

编辑者为: bhoomika rathod 11 10月, 2017

响应

3 回复
Safwan

Copying SQLite database from 'res' or 'asset' folder to 'data' folder is very common scenario. Android also requires this step:

https://stackoverflow.com/questions/10738623/copy-database-from-assets-folder-in-unrooted-device

 

When you are opening the database using:

sqlite3_open_v2(path2, &db_handle,SQLITE_OPEN_READWRITE, NULL);

Is it returning SQLITE_OK?

What's on your log "shared db open success" or "shared db open failed" ?

 

If Database is successfully opened then there shouldn't be any Issue. If SQLITE_OK is returned then the problem could be with the SQL Queries. I would suggest you to test some simple queries like 'CREATE','INSERT' first. This Guide is a good resource in my opinion:

Complete tutorial for sqlite database CRUD operation and data access from Tizen native application

bhoomika rathod

it gives "shared db open success" but update is not working. And i have been checked Query, it works in sqlite manager.

Safwan

Seems, The database is opened successfully. Then, there shouldn't be any complicacy. Is SQLITE_OK returned from sqlite3_exec() functions you used?

 

Please try to CREATE a new table, INSERT one or two rows and then SELECT * FROM that table.