微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法打开数据库-Android

我正在用sql开发一个简单的Android应用程序.
我按照以下指南 –
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

尝试打开数据库时出错.
这是我的DataBaseHelp.java类

public class DataBaseHelper extends sqliteOpenHelper{

private static String DB_PATH = "/data/data/and.testDB/databases/";

private static String DB_NAME = "MyData";

private sqliteDatabase myDataBase; 

private final Context myContext;
public DataBaseHelper(Context context) {

 super(context,DB_NAME,null,1);
    this.myContext = context;
} 
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");

     }
 }

}

private boolean checkDataBase(){

 sqliteDatabase checkDB = null;

 try{
  String myPath = DB_PATH + DB_NAME;
  checkDB = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READONLY);

 }catch(sqliteException e){

  //database does't exist yet.

 }

 if(checkDB != null){

  checkDB.close();

 }

 return checkDB != null ? true : false;
}

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,length);
 }

 //Close the streams
 myOutput.flush();
 myOutput.close();
 myInput.close();

}

public void openDataBase() {

 //Open the database
    String myPath = DB_PATH + DB_NAME;
 myDataBase = sqliteDatabase.openDatabase(myPath,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) {
  }
}

这是我的testDB.java类

public class testDB extends Activity {
sqliteDatabase myDataBase;
public String[] gur = new String[4];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    try {

        myDbHelper.createDataBase();

} catch (IOException ioe) {

    throw new Error("Unable to create database");

}

try {

    myDbHelper.openDataBase();
// Here is the problem-Can't open the database/
}catch(sqlException sqle){

    throw sqle;

}
    myDataBase = myDbHelper.getWritableDatabase();

    Cursor c = myDataBase.rawQuery("SELECT * FROM beer",null);


if (c != null ) {
    if  (c.movetoFirst()) {
          for(int x=0;x< 1;x++)
          {

              gur[x] = (c.getString(c.getColumnIndex("name")));
              c.movetoNext();
          }
}
    Toast.makeText(this,gur[0],Toast.LENGTH_LONG).show();
}


        }
    }

我使用sqlite数据库浏览器创建了数据库,创建了一个名为“beer”的表,添加了2行并将其放在我项目的/ assests文件夹中.

请注意,我是sql的新手,也是我第一次使用它.我整天都在寻找答案而找不到答案.

谢谢!

最佳答案
sqlite数据库浏览器中打开数据库添加一个名为“android_Metadata”的新表,

您可以执行以下sql语句来执行此操作:

CREATE TABLE“android_Metadata”(“locale”TEXT DEFAULT’en_US’)

现在在“android_Metadata”表中插入一行文本“en_US”:

INSERT INTO“android_Metadata”VALUES(‘en_US’)

然后,有必要将表的主id字段重命名为“_id”,以便Android知道绑定表的id字段的位置.

现在将新数据库文件复制到您的项目资产文件夹中,它将工作…..

原文地址:https://www.jb51.cc/android/430359.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐