本文介绍了构造函数DataBaseHelper未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataBaseHelper类setupin我的申请。
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口值java.sql.SQLException;
进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteException;
进口android.database.sqlite.SQLiteOpenHelper;
公共类DataBaseHelper扩展SQLiteOpenHelper { //你的应用程序数据库的Android的默认系统路径。
私人静态字符串DB_PATH =/数据/数据/ YOUR_PACKAGE /数据库/; 私人静态字符串DB_NAME =myDBName; 私人SQLiteDatabase MYDATABASE; 私人最终上下文myContext; / **
*构造
*注意到并保持以访问该应用程序的资产和资源所传递的上下文的引用。
* @参数方面
* /
公共DataBaseHelper(上下文的背景下){ 超级(上下文,DB_NAME,空,1);
this.myContext =背景;
}/ **
*在系统上创建一个空数据库,并与自己的数据库重写它。
* * /
公共无效的CreateDatabase()抛出IOException 布尔dbExist = checkDataBase(); 如果(dbExist){
//什么也不做 - 已存在于数据库
}其他{ //通过调用此方法与空的数据库将被创建到默认的系统路径
//你的应用程序,所以我们要能够覆盖该数据库与我们的数据库。
this.getReadableDatabase(); 尝试{ copyDataBase(); }赶上(IOException异常五){ 抛出新的错误(错误复制数据库); }
} } / **
*检查是否已存在于数据库,以避免重新复制每次打开应用程序时的文件。
*如果存在返回:真的,假的,如果它不
* /
私人布尔checkDataBase(){ SQLiteDatabase CHECKDB = NULL; 尝试{
字符串mypath中= DB_PATH + DB_NAME;
CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY); }赶上(SQLiteException E){ //数据库开不存在。 } 如果(CHECKDB!= NULL){ checkDB.close(); } 返回CHECKDB!= NULL?真假;
} / **
*副本数据库从本地资产文件夹在刚才创建的空数据库
*系统文件夹,从那里可以访问和处理。
*这是通过转流的字节流进行。
* * /
私人无效copyDataBase()抛出IOException //打开本地数据库的输入流
InputStream的myInput = myContext.getAssets()开(DB_NAME)。 //路径刚刚创建的空分贝
字符串outFileName = DB_PATH + DB_NAME; //打开空分贝的输出流
的OutputStream myOutput =新的FileOutputStream(outFileName); //传递从inputfile中字节到OUTPUTFILE
字节[]缓冲区=新的字节[1024];
INT长;
而((长度= myInput.read(缓冲液))大于0){
myOutput.write(缓冲液,0,长度);
} //关闭流
myOutput.flush();
myOutput.close();
myInput.close(); } 公共无效的openDatabase()抛出的SQLException { //打开数据库
字符串mypath中= DB_PATH + DB_NAME;
MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY); } @覆盖
公共同步无效的close(){ 如果(MYDATABASE!= NULL)
myDataBase.close(); super.close(); } @覆盖
公共无效的onCreate(SQLiteDatabase DB){ } @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){ }}
但是,当我在主要活动创建这个类的一个实例只是为了找回我得到以下错误的数据。
(里面的主要活动)
进口java.io.IOException异常;
进口值java.sql.SQLException;
进口android.annotation.TargetApi;
进口android.app.Activity;
进口android.content.Context;
进口android.os.Build;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.View;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;
公共类daskj延伸活动{
...
....
....
...
...
DataBaseHelper myDbHelper =新DataBaseHelper(本);
尝试{
myDbHelper.createDataBase();
}赶上(IOException异常IOE){
抛出新的错误();
}
尝试{
myDbHelper.openDataBase();
}赶上(的SQLException SQLE){
扔SQLE;
} }
}
帮我出去!
[...再code我的力presents线包括。 ]
解决方案
您必须通过上下文对象的构造函数。
您只需使用: DataBaseHelper myDbHelper =新DataBaseHelper(本);
如果它是一个Activity类内部发出
I have a DataBaseHelper class setupin my application.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
//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 = "myDBName";
private SQLiteDatabase myDataBase;
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 DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* 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
}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();
} 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_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
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;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
But when I create an instance of this class in my main activity just to retrieve the data I get the following error.( Inside the main activity )
import java.io.IOException;
import java.sql.SQLException;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class daskj extends Activity {
...
....
....
...
...
DataBaseHelper myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error();
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
}
}
Help me out!
[ ... represents lines of code I dint include. ]
解决方案
You have to pass the Context object in your constructor.
You just use: DataBaseHelper myDbHelper = new DataBaseHelper(this);
if it's issued inside an Activity class
这篇关于构造函数DataBaseHelper未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!