Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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/3/android/189.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
Java Android:填充sqlite数据库时出错_Java_Android_Sqlite - Fatal编程技术网

Java Android:填充sqlite数据库时出错

Java Android:填充sqlite数据库时出错,java,android,sqlite,Java,Android,Sqlite,我已经使用sqlite浏览器创建了一个sqlite数据库。然后我将其复制到我的资产文件夹中。 包my.easymedi.db import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.ContentValues; import and

我已经使用sqlite浏览器创建了一个sqlite数据库。然后我将其复制到我的资产文件夹中。 包my.easymedi.db

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DBHelper extends SQLiteOpenHelper {

/*
 * Database attributes - the Android's default system path for your
 * application database
 */
private static final String pkg = "my.easymedi.controller";
private static String DB_PATH = "/data/data/" + pkg + "/databases/";
private static String DB_NAME = "EasyMediInfo.jpeg";
private static final int DB_VERSION = 1;
private final Context myContext;
private SQLiteDatabase myDatabase;

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;

    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        if (dbfile.exists()) {
            Toast.makeText(context, "database exists", Toast.LENGTH_LONG)
                    .show();
        } else {
            Toast.makeText(context, "cant find database", Toast.LENGTH_LONG)
                    .show();
        }
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
}

/*
 * Creates an empty database on the system and rewrite it with your own
 * database
 */
public void createDataBase() {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {
        /*
         * By calling this method an empty database will be created into the
         * default system path of your application
         * So e can be able to overwrite that database with our database
         */
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT)
                    .show();
            Log.d("CREATE_DB", e.getMessage());
        }
    }
}

/*
 * Copy your database from assets folder to the just created empty database
 * in the system folder from where it can be access and handle. This id done
 * by transferring byte stream
 */
private void copyDataBase() throws IOException {
    InputStream databaseInput = null;
    /* Path to copy the database */
    String outFileName = DB_PATH + DB_NAME;
    /* open the empty database as an output stream */
    OutputStream databaseOutput = new FileOutputStream(outFileName);
    /* open the local database as the input stream */
    databaseInput = myContext.getAssets().open(DB_NAME);

    /* Transfer byte from byte from input file to output file */
    byte[] buffer = new byte[1024];
    int length = databaseInput.read(buffer);
    while (length > 0) {
        databaseOutput.write(buffer, 0, length);
        databaseOutput.flush();
    }
    databaseOutput.flush();
    databaseInput.close();
    databaseOutput.close();
}

/*
 * Check if the database is already exist to avoid re-copying the file each
 * time you open the application This returns true if if db is exist, 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_READONLY);
    } catch (SQLiteException e) {
        Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT)
                .show();
        Log.d("Check_DB", e.getMessage());
    }
    if (checkDB != null) {
        String str = "checked";
        System.out.println("====" + str + "====");
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

/* Open the database */
public boolean openDatabase() {
    String myPath = DB_PATH + DB_NAME;
    Toast.makeText(myContext, myPath, Toast.LENGTH_SHORT).show();
    myDatabase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    if (myDatabase != null) {
        System.out.println("====database opened====");
    } else {
        System.out.println("====error opening database====");
    }
    return myDatabase != null ? true : false;
}

public void closeDatabase() {
    if(myDatabase != null){
        myDatabase.close();
    }
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

/**
 * @param table - name of the table
 * @param values - values to insert
 */
public void insertIntoDatabase(String table, ContentValues values) {
    myDatabase.insert(table, null, values);
}
} 在我的主活动类中,我使用以下代码段初始化数据库

DBHelper helper = new DBHelper(this);
helper.createDataBase();
helper.openDatabase();
helper.close();
但我犯了以下错误。

如果有人能解释此应用程序的错误,我将不胜感激


Thanx提前。

请修复您的logCat帖子。错误是不言自明的,它无法打开您的数据库。uknown错误,请确保您的数据库存在并且指向正确的位置。我的数据库位于assets文件夹中,给定的路径正确。您是否尝试将其命名为“EasyMediInfo.db”?是,但我遇到了相同的错误:(