找不到Sqlite表

时间:2013-01-18 09:35:08

标签: android sqlite

我想从sqlite数据库中选择记录,只需在我的应用程序上显示。但是当我运行我的程序时,我收到了调试错误: - [2013-01-18 14:39:28 - ddms] Can't bind to local 8649 for debugger and table not found error.我在这里发布我的代码请有人检查并告诉我解决方案。

DatabaseMain类:

    public class DatabaseMain extends SQLiteOpenHelper{


    private static String DB_PATH = "/data/data/com.ocs.employeedetails/databases/";

    private static String DB_NAME = "EMPLOYEE_DB.sqlite";
    private static int VERSION = 1;
    private final Context mycontext;
    private SQLiteDatabase sqliteDb;

    public DatabaseMain(Context context) {
        super(context,DB_NAME,null,VERSION);
        this.mycontext = context;

    }


    public void createDataBase() throws IOException{

        boolean databaseExist = checkDataBase();
         if(databaseExist){

        }else{

            this.getReadableDatabase();
            //this.getWritableDatabase(); 
            try{
            copyDataBase();
            }catch(IOException e){
                throw new Error("Error copying database");

            }
        }
    } 


    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){
             e.printStackTrace();
        }
        if(checkDB != null){
            checkDB.close();
          }

        return checkDB != null ? true : false;

    }


    private void copyDataBase() throws IOException{ 

        InputStream myInput = mycontext.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);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close(); 
    }

    public void openDataBase() throws SQLException{      
        String myPath = DB_PATH + DB_NAME;
        sqliteDb = SQLiteDatabase.openDatabase(myPath, null,   SQLiteDatabase.OPEN_READWRITE);  
    }
    public Cursor queryExec(){
        openDataBase();
        String qry = "Select * from employee_tab";
        Cursor cur = null;
        cur = sqliteDb.rawQuery(qry,null);
        close();
        return cur;

    }
    @Override
    public synchronized void close() {

            if(sqliteDb != null)
                sqliteDb.close();

            super.close();

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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


    }

My Activityclass我正在使用DatabaseMain类:

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.buttonOk){
            Toast.makeText(getBaseContext(), "Hi ",Toast.LENGTH_SHORT).show();
        try {
                       dm.createDataBase();
        } catch (IOException e) {
             e.printStackTrace();
        }
        dm.openDataBase();
        Cursor cr = dm.queryExec();
        if(cr.moveToFirst())
        {

            while(cr.isFirst())
            {
                id[0] = cr.getInt(0);
                name.setText(cr.getString(1)); 
                ph.setText(cr.getInt(2));
                doj.setText(cr.getString(2));
            }
        }

        }

1 个答案:

答案 0 :(得分:0)

您可以尝试更改调试器的端口:

Under Window -> Preferences -> Android -> DDMS:

    Set Base local debugger port to "8601"
    Check the box that says "Use ADBHOST" and the value should be "127.0.0.1"