Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的SQLite静态openDatabase数据库方法错误_Android_Database_Sqlite_Opendatabase - Fatal编程技术网

Android中的SQLite静态openDatabase数据库方法错误

Android中的SQLite静态openDatabase数据库方法错误,android,database,sqlite,opendatabase,Android,Database,Sqlite,Opendatabase,我创建了一个静态方法来访问我的一个活动中的数据库,但在打开数据库时不断出错 主要活动 public static String getStudentData() { SQLiteDatabase sampleDB = null; try { //NOT WORKING sampleDB = SQLiteDatabase.openDatabase("studentDB",null, SQLiteDatabase.CREATE_IF_

我创建了一个静态方法来访问我的一个活动中的数据库,但在打开数据库时不断出错

主要活动

 public static String getStudentData() {

    SQLiteDatabase sampleDB =  null;

    try {
            //NOT WORKING
        sampleDB =  SQLiteDatabase.openDatabase("studentDB",null, SQLiteDatabase.CREATE_IF_NECESSARY); 
            //Also not working
        //sampleDB =  SQLiteDatabase.openOrCreateDatabase("studentDB", null);

        Cursor c = sampleDB.rawQuery("SELECT * FROM student where id='1'", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    //...                       
                }while (c.moveToNext());
            } 
        }
        c.close();
    } catch (Exception se ) {
    } finally {
            sampleDB.close();
    }
}
其他活动

String student = MainActivity.getStudentData();

我一直在获取
sqlite3\u open\u v2(“studentDB”,和handle,6,NULL)失败
。找不到问题所在。。。我也尝试过使用
模式\u WORLD\u WRITEABLE
。当前正在使用
模式\u PRIVATE
创建数据库。请帮帮我

openDatabase的第一个参数应该是db的完整路径,因此如果:

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";

private static String DB_NAME = "studentDB";
那么你应该打电话:

sampleDB =  SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
使用一个方法并将结果传递给SQLiteDatabase。openDatabase:

File dbFile= myActivity.getDatabasePath("studentDB");
sampleDB = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY);

哇,你说得对!我没想到我需要输入静态方法的完整路径。通常,我只做
sampleDB=SQLiteDatabase.openOrCreateDatabase(“studentDB”,null)。第一次尝试静态方法。谢谢难道我们不需要把扩展名“studentDB.db”或“studentDB.sqlite”放进去吗?