在Android NDK NATIVE中连接到SQLite [英] Connect to SQLite in android NDK NATIVE

查看:121
本文介绍了在Android NDK NATIVE中连接到SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android上的 NATIVE 中连接SQLite.

I'm connecting SQLite in NATIVE on android.

我遇到了以下异常:无法打开数据库文件

我的NDK代码:

#include <android/log.h>
#include <stdint.h>
#include "DatabaseHelper.h"
#include "../sqlite/sqlite3.h"

const char* SQL = "CREATE TABLE IF NOT EXISTS foo(a,b,c); INSERT INTO FOO 
VALUES(1,2,3); INSERT INTO FOO SELECT * FROM FOO;";
const int ERROR_STATE = -1;
const int SUCCESSFULLY_STATE = 0;

int createDatabase() {
char TAG[20] = "NATIVE_SQL";
sqlite3 *db = 0; // хэндл объекта соединение к БД
char *err = 0;

// открываем соединение и тут вылетает ошибка! 
if(sqlite3_open("myDb.db", &db)) {
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "Error opened database. 
%s\n",sqlite3_errmsg(db));
    return ERROR_STATE;
}
...someone code

P.S.if if(sqlite3_open(",& db)){.. 数据库创建了临时文件,并在关闭时将其删除.如果open(",& db)有效!

P.S. if if(sqlite3_open("", &db)) {.. database created temporary file and remove it durning close. If open("",&db) is work!

如何使用"myDb.db"名称创建数据库连接?

How will I create the db connection with "myDb.db" name?

if(sqlite3_open_v2("myDb.db", &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL)) {
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "Error opened database2. %s\n",sqlite3_errmsg(db));
    return ERROR_STATE;
}

清单中的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

推荐答案

Android应用没有自己的主"目录.类似于Linux的基础运行时具有getcwd(),它将返回/-即文件系统的根.请注意,该应用程序不拥有/,并且无法写入此目录.

Android app does not have its own 'home' directory. The underlying Linux-like runtime has getcwd(), and it will return / - i.e. the root of the filesystem. Note that the app does not own / and it cannot write to this directory.

这意味着您必须将完整路径传递到要打开的任何文件,包括 myDb.db .您可能希望将数据库保留在应用程序的默认数据库位置中,例如由

This means that you must pass the full path to any file you want to open, including myDb.db. You probably want to keep the database in the default database location for the app, such as returned by Context.getDatabasePath(). The best easiest reliable way to get it in C++, is to have Java query it before calling native, and pass the path to C++.

这篇关于在Android NDK NATIVE中连接到SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆