无法在android studio中打开使用SQLite Manager创建的SQLite数据库

时间:2014-09-08 08:33:23

标签: android database sqlite

我使用SQLiteManager创建了一个名为dbase的数据库。它包含一个名为comments的表.Its字段是id(主键,整数自动增量)和注释(文本)。所以它的扩展名为.sqlite。我将数据库保存在我的app.ie ex1 \ app \ build \ intermediates \ assets的assets文件夹中。我想将表内容检索到textview.But,我无法打开数据库。任何人都可以帮助我吗?

扩展SQLiteOpenHelper的我的DBHElper类是:

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



 public class DBHElper extends SQLiteOpenHelper {


private static String DB_PATH ="/data/data/com.example.iyas.ex1/databases/";
private static String DB_NAME = "dbase.sqlite";
private SQLiteDatabase myDataBase;
private static final int DATABASE_VERSION = 1;
//TABLE NAME : comments;column id integer autoincrement,comment text

   public DBHElper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
                }

public boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{

        String myPath = DB_PATH + DB_NAME;
        checkDB =SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){
                    Log.d("checkDatabase", e.toString());
    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

public void openDataBase() throws SQLException {

    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);

}

@Override
public 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) {

                }


}

我的活动课程:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MyActivity extends Activity {

TextView tv;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    dbQuery();
}

public void dbQuery() {
    try {

        DBHElper helper = new DBHElper(this.getApplicationContext());
        boolean dbExist = helper.checkDataBase();

        if (dbExist) {
            helper.openDataBase();
            Cursor c = db.rawQuery("SELECT comment from comments", null);

            if (c != null) {
                c.moveToFirst();
                {
                    do {
                        String comment = c.getString(1);
                        tv = (TextView) findViewById(R.id.labels);
                        tv.setText(comment);
                    } while (c.moveToNext());
                }
            }

        } else {
            Log.e("IN onStart()", "NO DATABASE EXISTS");
        }

    } catch (SQLiteException se) {

        Log.e("myapp in my activity", se.toString());
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用SQLiteAssetsHelper打开您的数据库。您可以从here.

下载其jar

你可以像这样使用它: -

public class AssetsHelper extends SQLiteAssetHelper {

  private static final String DATABASE_NAME = "dbase.sqlite";
  private static final int DATABASE_VERSION = 1;

  public AssetsHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

  }
}