Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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 从assets文件夹读取sqlite文件时未调用onCreate SqliteOpenHelper类_Android_Sqlite - Fatal编程技术网

Android 从assets文件夹读取sqlite文件时未调用onCreate SqliteOpenHelper类

Android 从assets文件夹读取sqlite文件时未调用onCreate SqliteOpenHelper类,android,sqlite,Android,Sqlite,我有sqlite文件。我已将该文件添加到资产文件夹中。我想从该文件创建数据库。但未调用SQLiteOPenhelper类的onCreate()方法。怎么办。请帮忙 public class SqliteAdapter_InsideDB extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/your package name/databases/"; private static String DB_N

我有sqlite文件。我已将该文件添加到资产文件夹中。我想从该文件创建数据库。但未调用SQLiteOPenhelper类的onCreate()方法。怎么办。请帮忙

public class SqliteAdapter_InsideDB extends SQLiteOpenHelper {



private static String DB_PATH = "/data/data/your package name/databases/";

private static String DB_NAME = "your.db";

private SQLiteDatabase database;

private final Context myContext;

/**
 * Constructor Takes and keeps a reference of the passed context in order to
 * access to the application assets and resources.
 * 
 * @param context
 */
public SqliteAdapter_InsideDB(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    try {
        createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (Exception e) {
        Log.e("DB-Exception", e.toString());
    }
} 

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
        openDataBase();

    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();
            openDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);

    } catch (SQLiteException e) {



    }

    return checkDB != null ? true : false;
}



private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    database = SQLiteDatabase.openDatabase(myPath, null, 
            SQLiteDatabase.OPEN_READWRITE);

}

// public SqliteAdapter open() throws SQLException {
// database = getWritableDatabase();
// return this;
// }

@Override
public synchronized void close() {

    if (database != null)
        database.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

// Add your public helper methods to access and get content from the
// database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd
// be easy
// to you to create adapters for your views.
public boolean isOpen() {
    if (database != null) {
        return database.isOpen();
    }
    return false;
}
}
您可以通过以下方式调用您的db类

 SqliteAdapter_InsideDB mySQLiteAdapter = new SqliteAdapter_InsideDB(className.this);

如果要访问数据库文件,请创建一个类:

DBConnect.java

package com.appgiudeextra.Database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.appguideextra.Items.MasterItem;

public class DBConnect extends SQLiteOpenHelper {
public int GetCursor;
// ****************** Declare all the global variable
// ****************************//
private Context myContext;
public String DB_PATH = "data/data/com.appguideextra/databases/"; // path
// of
// your
// datbase
public static String DB_NAME = "your.sqlite";// your database name
static String ASSETS_DB_FOLDER = "db";
private SQLiteDatabase db;

public DBConnect(Context context, String db_name) {
    super(context, db_name, null, 2);
    if (db != null && db.isOpen())
        close();

    this.myContext = context;
    DB_NAME = db_name;

    try {
        createDataBase();
        openDataBase();
    } catch (IOException e) {
        // System.out.println("Exception in creation of database : "+
        // e.getMessage());
        e.printStackTrace();
    }

}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();

    if (dbExist) {
        // System.out.println("Database Exist");
    } else {
        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private void copyDatabase() throws IOException {
    InputStream input = myContext.getAssets().open(DB_NAME);
    String outputFileName = DB_PATH + DB_NAME;
    OutputStream output = new FileOutputStream(outputFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }

    // Close the streams
    output.flush();
    output.close();
    input.close();
    // System.out.println(DB_NAME + "Database Copied !");
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDataBase() throws SQLException {
    // Open the database
    String myPath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

public boolean isOpen() {
    if (db != null)
        return db.isOpen();
    return false;
}

@Override
public synchronized void close() {
    if (db != null)
        db.close();
    super.close();
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        // System.out.println("My Pathe is:- " + myPath);
        // System.out.println("Open");
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        // System.out.println("checkDB value:" + checkDB);
        // System.out.println("My Pathe is:- " + myPath);
    } catch (Exception e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        // System.out.println("Closed");
        checkDB.close();
        // System.out.println("My db is:- " + checkDB.isOpen());
    }

    return checkDB != null ? true : false;
}

public Cursor execCursorQuery(String sql) {
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, null);
        GetCursor = cursor.getCount();
        Log.i("Inside execCursorQuery try", sql);
    } catch (Exception e) {
        Log.i("Inside execCursorQuery exception", e.getMessage());
    }
    return cursor;
}

public void execNonQuery(String sql) {
    try {
        db.execSQL(sql);
        // Log.d("SQL", sql);
    } catch (Exception e) {
        // Log.e("Err", e.getMessage());
    } finally {
        // closeDb();
    }
}


   }
现在,在您的活动中,为该类生成全局变量:

DBConnect db;
在启用onCreate()方法之后:


发布你的代码。为了让您得到帮助,询问代码的问题必须证明您对所解决的问题的理解是最低限度的。包括尝试过的解决方案、它们不起作用的原因以及预期结果。另请参阅:堆栈溢出问题检查列表谢谢您的回答。这解决了我的问题。但是如果我把createDataBase()放在;在onCreate方法中,则其不起作用。OnCreate没有在那里调用。为什么要感谢你的回答。这解决了我的问题。但是如果我把createDataBase()放在;在onCreate方法中,则其不起作用。OnCreate没有在那里调用。为什么现在你必须打开你的数据库。。。。
db = new DBConnect("context here", DBConnect.DB_NAME);
db.openDataBase();
db.close():