我刚开始练习android和sqlite数据库。我遇到了一个非常奇怪的问题,我无法纠正。请帮助我。
下面我已经拿出了我的DataBaseHelper类。 (代码块之后的描述......)
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 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 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) {
}
}
现在,当我为这个类创建一个对象时,我收到以下错误
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();
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;
}
}
上面代码中第9行的错误...
//语法标记错误“;”在下面的行中
DataBaseHelper myDbHelper = new DataBaseHelper();
请多多帮助我解决我的问题。
答案 0 :(得分:2)
您没有将语句编写到方法中,而是直接在类中编写。 您将包装到方法中。你可以保留成员变量,具体取决于你是否有意义。
通过缩进代码可以很容易地发现这一点。例如:
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;
}
}
从布局本身看起来不正确。
顶部的两行(2和3)是正确的,但效率不高。您可以通过默认构造函数创建一个新的DataBaseHelper
,并在下一行中用一个不同的构造函数创建一个覆盖它。您可以将这些合并到
DataBaseHelper myDbHelper = new DataBaseHelper(this);
使用较少资源时效果完全相同。
答案 1 :(得分:0)
使用以下代码更正您的VarbClass代码。你在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;
}
}
答案 2 :(得分:0)
VarbClass的代码显然是错误的。 'try'块应该在方法或'静态'块内。试试这个:
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;
}
}
}