如何正确打开和查询数据库?

时间:2013-04-08 14:09:33

标签: java android sqlite

我有我的nextQuestion方法,我每次在游戏中使用导入的sqlite预填充数据库时都需要新问题。我想我做错了因为每次我问新的问题我的数据库再次被创建。但我试图将db内容放在活动范围内,但我得到错误mDbHelper.createDatabase(); line(令牌createDatabase上的语法错误,此令牌后预期的indetifier)。这是我的nextQuestion方法,我在onCreate方法中第一次调用,然后在每个用户回答之后调用。

public void nextQuestion() {

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{

            mDbHelper.open();  //baza otvorena

            Cursor c = mDbHelper.getTestData(generateWhereClause());
            mAnsweredQuestions.add(c.getLong(0));

            List<Answer> labels = new ArrayList<Answer>();

            labels.add(new Answer(c.getString(2), true));
            labels.add(new Answer(c.getString(3), false));
            labels.add(new Answer(c.getString(4), false));
            labels.add(new Answer(c.getString(5), false));

            Collections.shuffle(labels);

            tacanOdg = c.getString(2);


        question.setText(c.getString(1));

        bOdgovor1.setText(labels.get(0).option);
        bOdgovor1.setTag(labels.get(0));
        bOdgovor1.setOnClickListener(clickListener);

        bOdgovor2.setText(labels.get(1).option);
        bOdgovor2.setTag(labels.get(1));
        bOdgovor2.setOnClickListener(clickListener);

        bOdgovor3.setText(labels.get(2).option);
        bOdgovor3.setTag(labels.get(2));
        bOdgovor3.setOnClickListener(clickListener);

        bOdgovor4.setText(labels.get(3).option);
        bOdgovor4.setTag(labels.get(3));
        bOdgovor4.setOnClickListener(clickListener);
            }

如何正确地做到这一点?

如果需要,这是我的助手。

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "/data/data/rs.androidaplikacijekvizopstekulture/databases/"; 
private static String DB_NAME ="pitanja.sqlite";// Database name
private static SQLiteDatabase mDataBase; 
private final Context mContext;
private static final String KEY_ID = "_ID";
private static final String KEY_PITANJE = "PITANJE";
private static final String KEY_ODGOVOR = "ODGOVOR";
private static final String KEY_OPCIJA1 = "OPCIJA1";
private static final String KEY_OPCIJA2 = "OPCIJA2";
private static final String KEY_OPCIJA3 = "OPCIJA3";
private static final String TABLE_NAME = "tblPitanja";

public DataBaseHelper(Context mojContext) 
{
    super(mojContext, DB_NAME, null, 1);// 1? its Database Version
    DB_PATH = mojContext.getApplicationInfo().dataDir + "/databases/";
    this.mContext = mojContext;
}

public void createDataBase() throws IOException
{
    //If database not exists copy it from the assets


        this.getReadableDatabase();
        this.close();
        try 
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
    /*Check that the database exists here: /data/data/your package/databases/Da Name
    private boolean checkDataBase()
    {
        File dbFile = new File(DB_PATH + DB_NAME);
        //Log.v("dbFile", dbFile + "   "+ dbFile.exists());
        return dbFile.exists();
    }
    */

    //Copy the database from assets
    private void copyDataBase() throws IOException
    {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        //Log.v("mPath", mPath);
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return mDataBase != null;
    }

    public void close() 
    {
        if(mDataBase != null)
            mDataBase.close();
        super.close();
    }


    public void onCreate(SQLiteDatabase arg0) {
        }


    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        Log.w("DataBaseHelper", "Upgrading database!!!!!");
          onCreate(arg0);

    }

}

和TestAdapter:

public class TestAdapter 
{
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() 
    {
        mDbHelper.close();
    }

     public Cursor getTestData(String whereClause)
     {;
         try
         {
             String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";

             Cursor mCur = mDb.rawQuery(sql, null);
             if (mCur!=null)
             {
                mCur.moveToNext();
             }
             return mCur;
         }
         catch (SQLException mSQLException) 
         {
             Log.e(TAG, "getTestData >>"+ mSQLException.toString());
             throw mSQLException;
         }
     }
}

好的,我编辑了我的数据库帮助程序类并添加了此代码以检查db是否存在:

**
 * 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
 */
public 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;
}

然后在我的游戏活动中,我检查这个方法是返回true还是false,如果它是false,我创建db:

TestAdapter mDbHelper = new TestAdapter(this);
        DataBaseHelper myDbHelper = new DataBaseHelper(this);

        if(!myDbHelper.checkDataBase()){
        mDbHelper.createDatabase();
        }

        try{

            mDbHelper.open();
}

3 个答案:

答案 0 :(得分:0)

你不应该一直调用createDatabase(),而是将数据库创建放在帮助者onCreate()中。完成后你还要close()你的数据库(mDbHelper.close())。

编辑:您似乎想根据存储在资产中的sqlite文件初始化您的数据库?如果是这样,请使用此助手:https://github.com/jgilfelt/android-sqlite-asset-helper

,而不是重新发明轮子

答案 1 :(得分:0)

尝试设置类似链接中提供的SQLiteHelper类。

http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_database

然后在教程中是一个CommentsDataSource类,它等同于你的TestAdapter类。

答案 2 :(得分:0)

以下是您可以使用的DataBaseHelper的好例子:

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) {

}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.

}

然后打开数据库:

 ...

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;

}

...