hi all,
i just started practicing android and sqlite database. i came across a very strange problem which i am unable to rectify.kindly help me out. Below i have out my DataBaseHelper class. (Description follows after the code block...)

package com.dialog.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
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/com.dialog.test/databases/";
 
    private static String DB_NAME = "butName";
 
    private SQLiteDatabase myDataBase; 
 
    private final Context myContext;
    
    public DataBaseHelper(Context context) {	 
    	super(context, DB_NAME, null, 1);
        this.myContext = context;
    }
    
    //Create Database
    public void createDataBase() throws IOException{
    	boolean dbExist = checkDataBase();
     	if(dbExist){
    		//do nothing - database already exist
    	}else{
        	this.getReadableDatabase();
         	try {
     			copyDataBase();
     		} catch (IOException e) {
         		throw new Error("Error copying database");
         	}
    	}
    }
    
    //check Database
    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;
    }
    
    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) {
 
	}
}

now when i create an object for this class i get the following error

package com.dialog.test;

import java.io.IOException;

import android.database.SQLException;

public class VarbClass {
        //syntax token error ";" in the below line
	DataBaseHelper myDbHelper = new DataBaseHelper(null);
	myDbHelper = new DataBaseHelper(this);
    try {
    	myDbHelper.createDataBase();
	} catch (IOException ioe) {
		throw new Error("Unable to create database");
	}
	try {
		myDbHelper.openDataBase();
	}catch(SQLException sqle){
		throw sqle;
	}
}

error on line 9 in the code above...
kindly help me resolve my issue thanks a lot.

Hmm... Not sure...

in line 22

private final Context myContext;

You put "final" to your myContext variable but then you attempt to assign a value to it in the constructor???

@Taywin: You're allowed to initialize a final instance variable like that in the constructor one time. You cannot make further assignments to it though.

Oh thank you for clarification :) Then I need to loop further in the code :)

OK, back to the last code portion at line 9 & 10

//syntax token error ";" in the below line
DataBaseHelper myDbHelper = new DataBaseHelper(null);
myDbHelper = new DataBaseHelper(this);

Why do you create "new" once, and then try the second time? The second one, however, use "this" which is the "VarbClass" class. I thought your constructor require "Context" class? Does "VerbClass" has any relation with "Context" class at all?

There is no DataBaseHelper constructor, therefore error.

PS: As Taywin pointed out assignment part of line 9 new DataBaseHelper(null) should be replaced by new DataBaseHelper(CONTEXT); .
PS2: You can have look at this sample I put on github few weeks back. It is from Pro Android 3 book

commented: Thanks! It helped me a lot :) +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.