Sqlite-没有这样的表错误

时间:2013-05-16 12:05:50

标签: android sqlite

我使用 Sqlite数据库浏览器创建了数据库并将其放在我的资源文件夹中,但是当我使用调用()方法时它给了我错误:  没有这样的表DailyWorks

这是 on create 代码

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    CopyDatabaseintheEmulatorMemory();  // Copy database in memory of emulator
     call();

}

private void call() 
{
    DBAdapter db = new DBAdapter(this);

     //---get all contacts---
    db.open();

    String sql="select * from DailyWorks";

    Cursor c = db.getAllContacts(sql);

    db.close();     
}

DBAdapter类是:

public class DBAdapter {

    static final String DATABASE_NAME = "MyDB";
    static final int DATABASE_VERSION = 2;


    final Context context;

    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static  class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
        }

     }

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close() 
    {
        DBHelper.close();
    }
    //---retrieves all the contacts---
    public Cursor getAllContacts(String sql)
    {
        return db.rawQuery(sql, null);
    }
}

1 个答案:

答案 0 :(得分:0)

在从assets文件夹访问数据库之前,必须将数据库复制到设备的包内存中。因为您的应用程序只能从您的包中访问数据库。 示例代码:

String DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";   
String DB_NAME = "database";
 InputStream myInput = context.getAssets().open(DB_NAME);
      String outFileName = DB_PATH + DB_NAME;
      OutputStream myOutput = new FileOutputStream(outFileName);
      byte[] buffer = new byte[1024];
      int length;
      while ((length = myInput.read(buffer)) > 0) {
       myOutput.write(buffer, 0, length);
      }

如需完整的代码示例,请重新here

我希望这会对你有所帮助。