2014-09-22 63 views
0

我有一个小型数据库的android应用程序。 我可以使用da数据库与数据库管理器。android sqlite数据库 - 除了android_metadata什么也没有

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

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

    DatabaseUtils.dumpCursor(c); 

这样做的结果,但是, :

09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor [email protected] 
09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor [email protected] 
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): <<<<< 

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

我dbhelperclass是:

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 

} 

} 

我初始化助手这样的:

@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例外:

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

但是:提供我的DB是NN命名(它是)和我的应用程序被命名为myapp,这不应该是正确的路径吗? nn在资产中。

+1

你在哪里和如何创建表?顺便说一句,你可以下载SQLite文件,并使用PC工具浏览它,它更方便。 – m0skit0 2014-09-22 19:58:08

+0

看起来像表不在那里,所以你可能没有创建数据库,或者你试图打开你的现有数据库错误的路径!检查路径:D – 2014-09-22 20:30:59

+0

路径应该是正确的。我把数据库放入资产。我将编辑它以便我的助手类被看到 – newnewbie 2014-09-22 21:51:34

回答

1

getReadableDatabase()自动创建一个数据库,用您在onCreate()(这是emtpy)中创建的任何内容进行初始化。

如果要从资产文件夹复制数据库,最好使用已知可用的库,如SQLiteAssetHelper

+0

ok.which解释了为什么它一般不起作用,但不解释为什么我得到一个filenotfound异常。 – newnewbie 2014-09-23 07:01:54

+0

所以这意味着我必须从头开始或使用一个库,尽管几年前我的代码可以工作? – newnewbie 2014-09-23 07:48:26

+0

谢谢。解决了多个问题。 – newnewbie 2014-09-23 17:39:56