2

I need to do database operations from C++ layer using ndk in Android.

but while opening database using sqlite3_open, I am getting "unable to open database file" error.

I am getting database path in Java using:

String sqliteDir = getApplicationContext().getDatabasePath("MyDb").getPath();

C++ code:

int rc = sqlite3_open(dbPath, _db);
if(rc != SQLITE3_OK) LOGD("Can't open database: %s with path %s\n", KSqlite3::sqlite3_errmsg(_db), dbPath);
else LOGD(" Opened database successfully %s \n", sqlite3_errmsg(_db));

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := myMod
LOCAL_SRC_FILES := myMod.cpp sqlite3.c
LOCAL_LDLIBS := -llog 
#Includes various C++ libraries
include $(BUILD_SHARED_LIBRARY)

Error Message: Can't open database: unable to open database file with path /data/data/com.packageName/databases/MyDb

5
  • Does the path really lack the initial /? Commented Feb 11, 2014 at 18:39
  • Where'd RSSFeed come from? Commented Feb 11, 2014 at 21:14
  • Edited with proper error message Commented Feb 12, 2014 at 1:45
  • Check if com.packageName/databases/ exists. I don't think getDatabasePath() creates it if it's not there. Commented Feb 12, 2014 at 14:25
  • Yes that was the only problem. Thanks!!! Commented Feb 13, 2014 at 8:22

1 Answer 1

5

As suggested by Seva Alekseyev, problem was I wrongly assumed that sqlite3_open will create whole path if it doesn't exists but it doesn't. So I need to create /databases manually. Instead of getting databases directory like this:

String sqliteDir = getApplicationContext().getDatabasePath("MyDb").getPath();

I got it till databases:

String sqliteDir = "/data/data/" + getApplicationContext().getPackageName() + "/databases";

Now in cpp code before calling sqlite3_open, check if it is there.

struct stat sb;
int32_t res = stat(path, &sb);
if (0 == res && (sb.st_mode & S_IFDIR)){
    LOGD("Database already exists in path:%s", path);
}else{
    LOGD("Creating database path:%s", path);
    int status = mkdir(path, S_IRWXU | S_IRWXG | S_IWOTH | S_IXOTH);
    if(status != 0){
        LOGD("Error occurred while creating database path : %s", path);
        return;
    }
}
string dbPath = string(path) + "//MyDb";

and then proceed with normal code:

int rc = sqlite3_open(dbPath, _db);
if(rc != SQLITE3_OK) LOGD("Can't open database: %s with path %s\n", KSqlite3::sqlite3_errmsg(_db), dbPath);
else LOGD(" Opened database successfully %s \n", sqlite3_errmsg(_db));
Sign up to request clarification or add additional context in comments.

1 Comment

Hello. I have similar problem. I need to open DB with c in android app. I'm developing .so library. How can I get packagename of the app using my library in natvie c layer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.