hi, 
I'm using sqlite in android system recently, here is how my programe running: 

1: there is an original sqlite db in apk 
2: when apk installed, I copy the db file from apk to a writable space, simply 
SD Card in android system 
3: download some files from internet and then replace them to sqlite if needed 
4: all things above is running in main thread

the software running well in most cases, but sometimes step(3) failed in some 
mobile phone, the return code is "SQLITE_READONLY" 
I'm very confuse about it and search google but finds nothing 

My codes is something like this: 

==========================================================================
[1]OPEN 

    int open_flags = SQLITE_OPEN_READWRITE| SQLITE_OPEN_FULLMUTEX| 
SQLITE_OPEN_SHAREDCACHE; 
        if (PathTool::IsFileExsit(db_file_name.c_str())) 
        { 
    sqlite3_open_v2(db_file_name.c_str(), &sqlite_db, open_flags, ...); 
    ... 
    } 
    .... 

[2]WRITE 

    std::string sql_info; 
        sqlite3_stmt *statement = 0; 
        bool is_suc = false; 
        sql_info = "replace into file_info( hash0, ...) values(?,...)"; 
        int ret = sqlite3_prepare_v2(sqlite_db, sql_info.c_str(), -1, 
&statement, NULL); 
        if (ret == SQLITE_OK) 
        { 
    sqlite3_bind_int(statement, 1, file_info.hash_info.hash0); 
    ... 

    ret = sqlite3_step(statement); 
    if(ret == SQLITE_DONE || ret == SQLITE_OK) 
    { 
         is_suc = true; 
    } 
    else 
    { 
        CCLog("sqlite step error, ret code: %d", ret); 
    } 
        } 
        else 
        { 
    CCLog("sqlite prepare error, ret code: %d", ret); 
        } 
 ==========================================================================
the code is failed in function sqlite3_step and return code is 
"SQLITE_READONLY" 


we all know that, files in apk is readonly 
firstly, I doubt that file property is inherited from its original file and it 
will leads the error 
so I try to make sure the file copy from apk is writable, codes(java): 

public static boolean setFilePrilivege(String path) 
  { 
    File file = new File(path); 
    boolean ret = false; 
    if (file.exists()) 
    { 
    try 
    { 
        ret = file.setWritable(true); 
        ret &= file.setReadable(true); 
        ret &= file.setExecutable(true); 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace(); 
    } 

    return ret; 
    } 
    return false; 
  } 

before write, I also detect whether the file is writable in operating system, 
codes(java): 
    public static boolean isFileWritable(String path) 
    { 
    File file = new File(path); 
    if (file.exists()) 
    { 
        return file.canWrite(); 
    } 
    return false; 
    } 

finally, I find that even I set the file writable and operating  system tells 
me that file is writable too, the results is still failed,  with return code: 
"SQLITE_READONLY" 



Please help me, thanks 



Best Regards 
ckwei

Reply via email to