Android5.0 running SQLite database on attempt to write a readonly db solution

Source: Internet
Author: User

Scene

JNI compiled SQLite source as a database, in the test Phone: Model (REDMI Note 2) Android version (5.0.2 lrx22g) system use, when attempting to write the database, return error message: Attempt to write a readonly Database


Solve

Find in the sqlite.c file

ino_t ino; /* Inode Number */

Revision changed to

unsigned long long ino; /* Inode Number */



Error description

Store inodes in unsigned long long


In + bit ABIs, ino_t is a A-bit type, while the St_ino field


In struct stat is the wide in both and the processes bit.


This means, struct stat can expose inode numbers that is


Truncated when stored in an ino_t.


The SDcard Fuse Daemon (/system/bin/sdcard) uses raw pointer


Values as inode numbers, so on devices bit, we ' re very likely


To observe inodes, that need > the bits to represent.


The filehasmoved function in SQLite compares the stored


Inode value with a new one from stat, and when the stored


Value is truncated, this check would falsely indicate that


The file has been moved. When the filehasmoved function


Triggers, other functions start returning errors indicating


That the database was in read-only mode.


Note:bionic differs from glibc on that struct stat ' s St_ino


is *always* to wide, and not the same width as ino_t.



Reference

http://www.jianshu.com/p/30139ef31230


Resolution process:

1 suspicion is the issue of read and write permissions, but other files are read-write, obviously not established, and have been provided with read and Write permissions:

<uses-permission android:name= "Android.permission.INTERNET"/>


2 suspected is to use MTP mode to share the body storage to the computer, causing both sides to edit simultaneously, by turning off MTP media sharing, not established

REDMI Note 2 Phone do not turn off the MTP media device, or you need to restore the factory settings to be able to re-share to the computer.


3 Copy the test.db and test.db-journal files to the Windows system, and use the SQLite console to make modifications without any

Problem. Currently using the same SQLite source code compiled by the console program. During the insertion of the data, the journal archive file is automatically deleted and is not prompted for read-only. Separately copy test.db, direct operation, there is no problem.


4 try to shut down the Java layer to read and write access to the database, just allow the NDK layer to operate the database directly, prevent multi-threaded access to the database, or the same results, of course, do not know whether the interface between Sqlite3_open and SQLITE3_OPEN_V2 will produce different results, The first method of accessing the database is currently used.


5 try to get the version of SQLite in the Android system, call getversion back 1, without substantial help.


6 is the other Java layer getting a connection to the database, without shutting down causing the problem to occur? Sqliteopenhelper internal Cache Only one database connection, in the multi-threaded use process, do not call close frequently, but should save a unique access instance, but for multi-process access, also brings problems. Blog.sina.com.cn/s/blog_5de73d0b0102w0g0.html



7 Although the NDK cannot modify the data, it can read the data, while the Java layer can easily complete the database modification operations.


8 attempts to invoke BeginTransaction and Endtransaction transaction locking for the operation of the database have no effect


9 suspect is the Android directory read problem, NDK layer SQLite read database files, found that there is a problem, but there is no way to find the journal archive file, it is considered to be read-only, is also reasonable

Summarize:

After all the above research and analysis, get the following speculation: NDK layer of SQLite instance after the system boot, will check whether there is journal file. If the journal file is automatically deleted through the program. However, if the program cannot be deleted automatically, the current suspicion is that the Journal,sqlite database cannot be deleted because of the asymmetry between versions, so the database becomes read-only.


Compromise Solution:

The NDK layer is only responsible for read-only data, if there are any changes, you need to call the Java interface to modify data, although there is sqlite source code, but the NDK layer is not debugging.


Android5.0 running SQLite database on attempt to write a readonly db solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.