Android Database Helper [ Syntax Token Error]?

The code of VarbClass is obviously wrong. The 'try' block should be inside a method or a 'static' block. Try this.

Up vote 0 down vote favorite share g+ share fb share tw.

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" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io.

FileOutputStream; import java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io. IOException; import java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io. InputStream; import java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">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 Androids 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 doest 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 byte1024; 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" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io" rel="nofollow">java.io. IOException; import android.database. SQLException; public class VarbClass { //syntax token error ";" in the below line DataBaseHelper myDbHelper = new DataBaseHelper(); 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... //syntax token error ";" in the below line DataBaseHelper myDbHelper = new DataBaseHelper(); kindly help me resolve my issue thanks a lot.

Android sqlite link|improve this question asked Sep 14 '11 at 9:55Daniel Euchar548 50% accept rate.

The code of VarbClass is obviously wrong. The 'try' block should be inside a method or a 'static' block. Try this: public class VarbClass { DataBaseHelper myDbHelper; public VarbClass() { 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; } } }.

I get the error "the constructor DataBaseHelper is undefined" – Daniel Euchar Sep 14 '11 at 10:28 Sorry, my mistake. You get this error because you pass VarbClass instance to the constructor for DataBaseHelper. You need to pass a Context.

Add a context parameter to the constructor ('public VarbClass()' should become 'public VarbClass(Context context)') and pass it on to the DataBaseHelper, instead of 'this'. – Ash Sep 14 '11 at 10:37 Ash I am sorry but I am new to this can you please brief me on this? Thank You – Daniel Euchar Sep 14 '11 at 12:05 Each Android application has a Context.

Instances of this class represent environment of the application. Check this link out for more information. SQLiteOpenHelper (and DataBaseHelper, obviously) needs context in order to open the database.

Context can be obtained from any Activity (see getApplicationContext()). If VarbClass is used from an Activity, it should be instantiated something like this: new VarbClass(getApplicationContext()), and then VarbClass will be able to pass the context on to the DataBaseHelper. – Ash Sep 14 '11 at 15:34.

You didn't write your statements into a method, instead directly inside the class. You have to wrap them into a method. You can keep the member variable though, depending on if it makes sense to you.

This would be easily spottable by indenting your code. Example: public class VarbClass { DataBaseHelper myDbHelper = new DataBaseHelper(); 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; } } That does not look correct from the layout itself. The two lines at the top (2 and 3) are correct, but not efficient.

You create a new DataBaseHelper via the default constructor and overwrite this in the next line with one created from a different constructor. You can merge these to DataBaseHelper myDbHelper = new DataBaseHelper(this); Has exactly the same effect while using less resources.

Correct your VarbClass code with following code. You have done wrong in DataBaseHelper myDbHelper = new DataBaseHelper(null); package com.dialog. Test; import java.io.

IOException; import android.database. SQLException; public class VarbClass { //syntax token error ";" in the below line DataBaseHelper myDbHelper = 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; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions