在DDMS中找不到SqliteDatabase

时间:2013-04-25 18:42:52

标签: android sql

我现在已经被困在这一段时间了iv我改变了我的代码很多次它让我痛苦。无论如何我已经在sql数据库浏览器中创建了一个数据库我一直在阅读如何将它放在我正在工作的应用程序中此刻。所以我将它放在我的资产文件夹中并制作了一个数据库帮助程序java class.But我只是可以将我的数据库文件导入eclipse DDMS我看看堆栈溢出的答案,但只是不知道问题在哪里。我将DBHelper代码再次更改为Database not copying from assets,但我的DDMS中仍然没有DB文件。

你看到的图像是我现在的。这是我做过的第一个计算机编码 我是android.Thank你的新手

http://s1.postimg.org/6u667r3bj/snip1.jpg

http://s13.postimg.org/l5vsas40n/snip2.jpg

   package com.mybasicapp;

   import java.io.File;
   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{
   private Context mycontext;

  private String DB_PATH = "/data/data/gr.peos/databases/";
 //private String DB_PATH = 
 mycontext.getApplicationContext().getPackageName()+"/databases/";
 private static String DB_NAME = "BLib.sqlite";//the extension may be .sqlite or .db
 public SQLiteDatabase myDataBase;
  /*private String DB_PATH = "/data/data/"
                        + mycontext.getApplicationContext().getPackageName()
                        + "/databases/";*/

  public DataBaseHelper(Context context) throws IOException  {
   super(context,DB_NAME,null,1);
   this.mycontext=context;
   boolean dbexist = checkdatabase();
   if(dbexist)
  {
    //System.out.println("Database exists");
    opendatabase(); 
  }
  else
  {
    System.out.println("Database doesn't exist");
   createdatabase();
  }

 }

  public void createdatabase() throws IOException{
   boolean dbexist = checkdatabase();
   if(dbexist)
   {
    //System.out.println(" Database exists.");
   }
   else{
       this.getReadableDatabase();
    try{
        copydatabase();
    }
    catch(IOException e){
        throw new Error("Error copying database");
      }
    }
  }
  private boolean checkdatabase() {
    //SQLiteDatabase checkdb = null;
   boolean checkdb = false;
    try{
    String myPath = DB_PATH + DB_NAME;
    File dbfile = new File(myPath);
    //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
    checkdb = dbfile.exists();
     }
     catch(SQLiteException e){
    System.out.println("Database doesn't exist");
    }

     return checkdb;
   }
  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("/data/data/gr.peos/databases  
  /BLib.sqlite");

    // transfer byte to inputfile to 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_READWRITE);

    }



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

  @Override
   public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub

    }

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

   }
   }

3 个答案:

答案 0 :(得分:0)

您不必创建数据库并在项目中手动移动它。如果它不存在,它将自动创建。因此,有两种方法可以覆盖以创建数据库,并在数据库版本更改时进行更改。只需致电

super(context, DB_NAME, null, 1);

在构造函数中,“1”是您的数据库版本。如果将其更改为“2”并且数据库已存在,则将调用“onUpgrade”。

public void onCreate(SQLiteDatabase db)
如果您的数据库不存在,将自动调用

。在这里你想要添加表格。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
如果设备上的数据库版本低于应用程序中给定的数据库版本,则会自动调用

。在此方法中,如果表格在版本之间发生了变化,则更改表格。

我建议创建第二个类来交互数据库(打开/关闭/插入)。看一下本教程:http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial_database

答案 1 :(得分:0)

如果我理解你的错误,你就不会在你的db文件夹中添加一个现成的SQLite文件。

  1. 打开您的模拟器并确保它已被选中。

  2. 在DDMS open / data / data / [your_package_name] / databases文件夹中选择它。

  3. 使用 - +按钮拉取和插入文件。

  4. enter image description here

答案 2 :(得分:0)

使用SQLiteOpenHelper是正确的方法。这对我很有效。但是,我要覆盖的唯一功能是

public void onCreate(SQLiteDatabase db) 
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) 

我想让它在我的应用程序内部存储区域的根目录中创建数据库的默认位置。

在您的情况下,如果我了解您发布的内容,则需要将资源文件夹中的数据库复制到内部存储。我的建议是4部分:

  1. 暂时让Android为您创建一个空数据库并找出它放在哪里。
  2. 修改copyDatabase代码以复制到该位置。
  3. 覆盖onCreate以调用copyDatabase。如果没有数据库,Android将只调用onCreate,因此您可以无条件地调用copyDatabase。
  4. 将来,当您进行架构更改时,请在onUpgrade中处理它们。
  5. 应该是全部。让我知道你如何取得进展。

    有一点需要注意:onCreate已经创建了一个空数据库,因此我不确定从资产中复制数据库是否有效,而不会以某种方式关闭并重新打开数据库。我正在使用的另一种方法是将DDL放在资产文件中,然后在onCreate中处理它。像这样的东西(除了我使用的是字符串数组而不是资产文件):

    String[] ddl = 
    {
      "CREATE TABLE [T1] ([id] INTEGER NOT NULL, [col1] CHAR);" +
      "CREATE TABLE [T2] ([id] INTEGER NOT NULL, [col2] CHAR)";
    };
    
    db.beginTransaction();
    try
    {
      for (int i = 0, limit = ddl.length;  i < limit;  i++)
        db.execSQL (ddl [i]);
    } finally {
      db.endTransaction();
    }