SQLite DataBase未在android AVD中加载

时间:2013-02-20 11:42:59

标签: android sqlite intellij-idea

我运行了这段代码,但是当我想使用 db 时, AVD 无效 我已将 .db 文件复制到资产中!但不知道为什么不行! 它说:不幸的是程序停止了 任何人都可以帮忙吗?

       public class MyActivity extends Activity {
        Button run;
        EditText ed;
        TextView v;
        SQLiteDatabase db;

        @Over

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

        run = (Button) findViewById(R.id.search);
        ed = (EditText) findViewById(R.id.word);
        v = (TextView) findViewById(R.id.res);

        try {
            String path="/data/data/"+getPackageName()+"/databases/dic.db";
            v.append("output file created ! \n");
            File dbfile= new File(path);
            if(!dbfile.exists())
            {   try
                {
                    v.append("file not exist !  \n");
                    InputStream in=getAssets().open("dic.db");
                    v.append("input file opend \n");
                    OutputStream out = new FileOutputStream(dbfile);
                    byte[] buffer = new byte[1024];
                    int length;

                    while ((length = in.read(buffer)) > 0)
                        out.write(buffer, 0, length);

                    out.flush();
                    out.close();
                    in.close();
                    v.append("db copied!");
                }
                catch (IOException e)
                {
                    v.append("ERR:"+e.getMessage());
                }
            }
        }
        catch (SQLException e)
        {
            v.append("ERR:"+e.getMessage());
        }

        run.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                v.append(ed.getText()+":");
                try
                {
                    db=openOrCreateDatabase("dic.db",MODE_PRIVATE,null);
                    v.append("db opend \n");

                    Cursor c;
                    c = db.rawQuery("SELECT * FROM words WHERE en='"+ed.getText()+"';",null);
                    v.append(c.getString(1));

                }
                catch (SQLException e)
                {
                    v.append("ERR:"+e.getMessage());
                }
            }
        } );

    }
}
欢呼。

0 个答案:

没有答案