无法打开数据库的Andr​​oid [英] Can't open database-Android

查看:109
本文介绍了无法打开数据库的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的Andr​​oid应用程序与SQL。 我跟着下面的指南 - <一href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/">http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

I'm developing a simple Android application with SQL. I followed the following guides - http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

我试图打开数据库时出现错误。 这是我的DataBaseHelp.Java类 -

I get an error when trying to open the database. Here is my DataBaseHelp.Java Class-

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, null, 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, 0, 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, 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) {
  }
}

这是我testDB.Java类 -

And this is my testDB.Java class-

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数据库浏览器的数据库,创建了一个名为啤酒表,加入2行,把它放在/资产产生的文件夹在我的项目。

I created the database using SQLite Database Browser, created a table named "beer", added 2 rows and put it in the /assests folder in my project.

请注意,我是新来的SQL,并使用它了我的第一次。我搜索了一整天的答案,无法找到一个。

Please note that i'm new to SQL, and its my first time using it. I searched all day for an answer and couldn't find one.

谢谢!

推荐答案

在SQLite数据库浏览器中打开你的数据库,并添加一个新的表称为android_metadata

Open your database in sqlite database browser and add a new table called "android_metadata",

您可以执行以下SQL语句来做到这一点:

you can execute the following SQL statement to do it:

CREATE TABLEandroid_metadata(区域设置TEXT DEFAULTEN_US')

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

现在将单行的文字EN_US中的android_metadata表:

Now insert a single row with the text 'en_US' in the "android_metadata" table:

INSERT INTOandroid_metadataVALUES('EN_US')

INSERT INTO "android_metadata" VALUES ('en_US')

那么,有必要重命名表的主id字段为_id,因此Android将知道id字段绑定你的表。

Then, it is necessary to rename the primary id field of your tables to "_id" so Android will know where to bind the id field of your tables.

现在新的数据库文件复制到你的项目资产的文件夹,它会工作.....

now copy the new database file into your projects assets folder, it will work.....

这篇关于无法打开数据库的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆