SQLiteOpenHelper的构造问题 [英] SQLiteOpenHelper constructor problem
本文介绍了SQLiteOpenHelper的构造问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在SQLiteBrowser创建SQLite数据库。我想引用它的Android应用程序,所以我把数据库文件中的资产文件夹用于n以下code到数据库复制到应用程序的内存。使用Eclipse的Android应用程序开发米
但SQLiteOpenHelper的构造是给错误。 构造DB_Import(DB_test1)未定义
包com.example.DB_test1;
公共类DB_test1延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
DB_Import my_Import;
// SQLiteDatabase MYDB = NULL;
my_Import =新DB_Import(本);
尝试 {
my_Import.createDatabase();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
下面是另一个类文件。
包com.example.DB_test1;
公共类DB_Import扩展SQLiteOpenHelper {
私人最终语境myContext;
字符串DB_PATH =数据/数据/ com.example.DB_test /数据库/;
字符串DB_NAME =Dict_temp;
私人SQLiteDatabase MyDatabase的;
公共DB_Import(上下文的背景下){
超级(上下文中,数据/数据/ com.example.DB_test /数据库/,空,1);
this.myContext =背景;
}
公共无效的CreateDatabase()抛出IOException异常{
// TODO自动生成方法存根
布尔dbExist = checkDatabase(); ////////////////检查数据库中存在
如果(dbExist){
//的openDatabase();
}其他 {
this.getReadableDatabase();
尝试 {
copyDatabase(); ///////////////复制数据库,从资产到Android
}赶上(IOException异常E){
// TODO自动生成的catch块
抛出新的错误(错误复制数据库);
}
}
}
解决方案
试试这个code:
公共类DataBaseHelper扩展SQLiteOpenHelper {
私人语境mycontext;
私人字符串DB_PATH =/data/data/gr.peos/databases/;
//私有String DB_PATH = mycontext.getApplicationContext()getPackageName()+/数据库/。
私有静态字符串DB_NAME =BLib.sqlite; //扩展名可能是.sqlite或.db的
公共SQLiteDatabase MyDatabase的;
/ *私人字符串DB_PATH =/数据/数据/
+ mycontext.getApplicationContext()。getPackageName()
+/数据库/; * /
公共DataBaseHelper(上下文的背景下)抛出IOException异常{
超级(上下文,DB_NAME,空,1);
this.mycontext =背景;
布尔dbexist = checkdatabase();
如果(dbexist)
{
//System.out.println("Database存在);
的openDatabase();
}
其他
{
的System.out.println(数据库不存在);
的CreateDatabase();
}
}
公共无效的CreateDatabase()抛出IOException异常{
布尔dbexist = checkdatabase();
如果(dbexist)
{
//System.out.println(数据库的存在。);
}
其他{
this.getReadableDatabase();
尝试{
copydatabase();
}
赶上(IOException异常E){
抛出新的错误(错误复制数据库);
}
}
}
私人布尔checkdatabase(){
// SQLiteDatabase CHECKDB = NULL;
布尔CHECKDB = FALSE;
尝试{
字符串mypath中= DB_PATH + DB_NAME;
文件DBFILE =新的文件(mypath中);
// CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
CHECKDB = dbfile.exists();
}
赶上(SQLiteException E){
的System.out.println(数据库不存在);
}
返回CHECKDB;
}
私人无效copydatabase()抛出IOException异常{
//打开本地数据库作为输入流
InputStream的myinput = mycontext.getAssets()开(DB_NAME)。
//路径刚刚创建的空分贝
字符串outfilename = DB_PATH + DB_NAME;
//打开空分贝的输出流
的OutputStream myoutput =新的FileOutputStream(/数据/数据/ gr.peos /数据库/ BLib.sqlite);
//传输字节INPUTFILE到OUTPUTFILE
byte []的缓冲区=新的字节[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_READWRITE);
}
市民同步无效的close(){
如果(MyDatabase的!= NULL){
myDataBase.close();
}
super.close();
}
I Created SQLite Database in SQLiteBrowser. I want to refer it in android app so I put DB file in assets folder n used the following code to copy DB to App memory. M using eclipse for android app development.
But SQLiteOpenHelper constructor is giving error. The constructor DB_Import(DB_test1) is undefined
package com.example.DB_test1;
public class DB_test1 extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DB_Import my_Import;
//SQLiteDatabase myDb = null;
my_Import=new DB_Import(this);
try {
my_Import.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Following is another class file.
package com.example.DB_test1;
public class DB_Import extends SQLiteOpenHelper{
private final Context myContext;
String DB_PATH = "data/data/com.example.DB_test/databases/";
String DB_NAME = "Dict_temp";
private SQLiteDatabase myDatabase;
public DB_Import(Context context) {
super(context, "data/data/com.example.DB_test/databases/", null, 1);
this.myContext = context;
}
public void createDatabase()throws IOException{
// TODO Auto-generated method stub
boolean dbExist = checkDatabase(); ////////////////Check if database Exist
if (dbExist) {
//openDatabase();
}else {
this.getReadableDatabase();
try {
copyDatabase(); ///////////////Copies the database from assets to android
} catch (IOException e) {
// TODO Auto-generated catch block
throw new Error("Error copying database");
}
}
}
解决方案
Try this code:
public class DataBaseHelper extends SQLiteOpenHelper{
private Context mycontext;
private String DB_PATH = "/data/data/gr.peos/databases/";
//private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"/databases/";
private static String DB_NAME = "BLib.sqlite";//the extension may be .sqlite or .db
public SQLiteDatabase myDataBase;
/*private String DB_PATH = "/data/data/"
+ mycontext.getApplicationContext().getPackageName()
+ "/databases/";*/
public DataBaseHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println("Database exists");
opendatabase();
}
else
{
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException{
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copydatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
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("/data/data/gr.peos/databases/BLib.sqlite");
// transfer byte to inputfile to 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_READWRITE);
}
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
这篇关于SQLiteOpenHelper的构造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文