Sqlite operation in Android 4.2: the specified path must be displayed during openDatabase ().

Source: Internet
Author: User

A small app is written, and data needs to be stored in sqlite. Follow the instructions in this document to create a database file as follows:

            db_aimu = SQLiteDatabase.openOrCreateDatabase("test.db", null);

Logcat error during running:

02-22 09:16:28.214: E/SQLiteLog(26781): (14) cannot open file at line 30176 of [00bb9c9ce4]02-22 09:16:28.222: E/SQLiteLog(26781): (14) os_unix.c:30176: (2) open(//test.db) - 02-22 09:16:28.331: E/SQLiteDatabase(26781): Failed to open database 'test.db'.02-22 09:16:28.331: E/SQLiteDatabase(26781): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at com.test.test.<init>(test.java:21)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at com.test.test.getInstance(test.java:34)02-22 09:16:28.331: E/SQLiteDatabase(26781):     at com.test.MainActivity$2.run(MainActivity.java:71)

It seems that opening the data file failed.

 

No reason was found when I checked the official documents online. Finally, someone mentioned:

I faced this problem, it was because in 4.2, there is multi users support, and if you are testing with non-admin user, you cannot acess/data path ..

In my case I'm using the path:

Context. getFilesDir (). getAbsolutePath (). replace ("files", "databases") + File. separator

 

Http://androiddev.orkitra.com /? P = 13793

That is to say, the multi-user mechanism is introduced in androi4.2. Normal users cannot access the/data directory under the root directory, and thus cannot create database files. My android simulator is indeed the latest version 4.2, so this problem occurs.

 

The problem is solved by knowing the cause. Use context. getFilesDir (). getAbsolutePath () in the above method to obtain the absolute path.

 

 

 

 

Related Article

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.