Android的SQLite数据库 - 什么都没有,除了android_metadata [英] android sqlite database - nothing there except android_metadata

查看:172
本文介绍了Android的SQLite数据库 - 什么都没有,除了android_metadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型数据库的Andr​​oid应用。
我可以使用DA数据库与数据库管理器。

I have an android application with a small database. I can use da database with a database manager.

我尝试了查询,并得到了没有这样的表,所以为了找出哪些表有,我试过,与帮助从这里:

I tried a query and got "no such table", so in order to find out which tables there are, I tried, with help from here:

    Cursor c = helper.getReadableDatabase().rawQuery("SELECT name FROM sqlite_master WHERE type=?",  new String[] {"table"});

    DatabaseUtils.dumpCursor(c);

这样做的结果,但是,是:

The result of this, however , is:

 09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor   android.database.sqlite.SQLiteCursor@b2d5af30
 09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor   android.database.sqlite.SQLiteCursor@b2d5af30
09-22 15:30:59.710: I/System.out(973): 0 {
09-22 15:30:59.720: I/System.out(973):    name=android_metadata
09-22 15:30:59.720: I/System.out(973): }
09-22 15:30:59.720: I/System.out(973): <<<<<

我怎样才能找出发生在我的数据库的其余部分。

How can I find out what happened to the rest of my database

我dbhelperclass是:

My dbhelperclass was:

 package com.example.myapp;


public class Helper extends SQLiteOpenHelper

{
private static String path = "/data/data/com.example.myapp/databases/";
private static String db = "nn";
private static String dbpath = path + db;
private SQLiteDatabase myDB;
private  Context con;

 public Helper(Context context) {

     super(context, db, null, 1);
     this.con = context;
     }  

 public Context getContext(){
     return this.con;
 }

 public void createDataBase() throws IOException{


     if(!checkDataBase()){
     this.getReadableDatabase();

     try {

     copyDataBase();

     } catch (IOException e) {

     System.out.println("no Database");

     }
     }

     }


private boolean checkDataBase() {
     SQLiteDatabase checkDB = null;

     try{

     checkDB = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READONLY);

     }catch(SQLiteException e){



     }

     if(checkDB != null){

     checkDB.close();
     return true;

     } else {return false;}


}


private void copyDataBase() throws IOException {

    InputStream myInput = con.getAssets().open(db);  
    OutputStream myOutput = new FileOutputStream(dbpath);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();


}


 public void openDataBase() throws SQLException{


    myDB = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READONLY);

    }

 @Override
 public synchronized void close() {
 if(myDB != null)
 myDB.close();    
 super.close();

 }   


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

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

我初始化助手这样的:

I initialized the helper thus:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    helper = new Helper(this);

    try {
        helper.createDataBase();
    } catch (IOException ex) {
        System.out.println("no start");
    }
    try {
        helper.openDataBase();
    } catch (SQLException sqlex) {
        System.out.println("does not open");
    }
     }

好吧原来我得到一个filenotfound异常:

Ok it turns out I get a filenotfound exception:

 09-22 18:39:04.103: I/System.out(1119): java.io.FileNotFoundException: /data/data/com.example.myapp/databases/nn

不过:只要我的数据库被命名为NN(它是)和我的应用程序名为MyApp,不应该这样是正确的道路?
nn是的资产。

However: provided my db is named nn (which it is) and my app is named myapp, should not this be the correct path? nn is in assets.

推荐答案

getReadableDatabase()自动创建一个数据库,不管你在创建初始化的onCreate()(这是emtpy)。

getReadableDatabase() automatically creates a database, initialized with whatever you create in onCreate() (which is emtpy).

如果您想从数据库中的资源文件夹复制,更好地利用一个已知的工作,如的 SQLiteAssetHelper

If you want to copy the database from the assets folder, better use a library that is known to work, such as SQLiteAssetHelper.

这篇关于Android的SQLite数据库 - 什么都没有,除了android_metadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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