0

我已经运行了这段代码,但是当我想使用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());
                }
            }
        } );

    }
}

干杯。

4

0 回答 0