Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

java - SQLite DataBase Read Error 'no such file or directory '

Hello I was working with SQlite on my phone with android studio

I have a simple database like this :

DATABASE 1 :

public class myDbAdapter {
    myDbHelper myhelper;

    public myDbAdapter(Context context) {
        myhelper = new myDbHelper(context);
    }

    public long insertData(String name, String ip, String port, String rele) {
        SQLiteDatabase dbb = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME, name);
        /* ... same for more items ...*/
        long id = dbb.insert(TABLE_NAME, null, contentValues);
        return id;
    }

    public String getData() {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        String[] columns = {myDbHelper.UID, myDbHelper.NAME, myDbHelper.IP, myDbHelper.PORT, myDbHelper.RELE, myDbHelper.Hash};
        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        StringBuffer buffer = new StringBuffer();

        int i = 0;
        while (cursor.moveToNext()) {
            i++;
            int cid = cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
            String name = cursor.getString(cursor.getColumnIndex(myDbHelper.NAME));
            String ipE = cursor.getString(cursor.getColumnIndex(myDbHelper.IPE));
            /* ... same for more items ...*/
            buffer.append("*" + cid + "-" + name + "-" + ipE + "-" + port + "-" + rele + "
");
        }
        List1.colu = i;
        return buffer.toString();
    }

    public int delete(String uid) {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        String delgrp = "DELETE FROM " + TABLE_NAME + " WHERE  _id='" + uid + "'";
        db.execSQL(delgrp);
        return 1;
    }


    static class myDbHelper extends SQLiteOpenHelper {
        public static final String DATABASE_NAME = "myDatabase";    // Database Name
        public static final String TABLE_NAME = "Data";   // Table Name
        private static final int DATABASE_Version = 1;    // Database Version
        private static final String UID = "_id";     // Column I (Primary Key)
        /* ... same for more items ...*/
        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
                " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + IPE + " VARCHAR(255) ," + TEST1 + " VARCHAR(255) ," + TEST2 + " VARCHAR(255) ," + Hash + " VARCHAR(225));";
        private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
        private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context = context;
        }

        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(CREATE_TABLE);
            } catch (Exception e) {
                //  Message.message(context,""+e);
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                // Message.message(context,"OnUpgrade");
                db.execSQL(DROP_TABLE);
                onCreate(db);
            } catch (Exception e) {
                // Message.message(context,""+e);
            }
        }
    }

I Wanted to add another TABLE to same database (MyDataBase)

So i created another java class named MyDbAdapter2

Same codes as above just changed class names and Table name

  helper = new myDbAdapter(this);
  helper2 = new myDbAdapter2(this);

DATABASE 2 :

public class myDbAdapter2 {
        myDbHelper myhelper;

        public myDbAdapter2(Context context) {
            myhelper = new myDbHelper(context);
        }

        public long insertData(String name, String ip) {
            /*...*/
        }

        public String getData() {
            SQLiteDatabase db = myhelper.getWritableDatabase();
            String[] columns = {myDbHelper.UID, myDbHelper.ITEM, myDbHelper.SUBITEM};
            Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
            StringBuffer buffer = new StringBuffer();

            int i = 0;
            while (cursor.moveToNext()) {
                i++;
                int cid = cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
                String name = cursor.getString(cursor.getColumnIndex(myDbHelper.ITEM));
                String ipe = cursor.getString(cursor.getColumnIndex(myDbHelper.SUBITEM));
                buffer.append("*" + cid + "-" + name + "-" + ipe + "
");
            }
            //  List1.colu=i;
            return buffer.toString();
        }


        static class myDbHelper extends SQLiteOpenHelper {
            public static final String DATABASE_NAME = "myDatabase";    // Database Name
            public static final String TABLE_NAME = "Data2";   // Table Name
            private static final int DATABASE_Version = 1;    // Database Version
            private static final String UID = "_id";     // Column I (Primary Key)
            /*...*/  //Column II
            // ... // Column III
            private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
                    " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ITEM + " VARCHAR(255) ," + SUBITEM + " VARCHAR(225));";
            private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
            private Context context;

            public myDbHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_Version);
                this.context = context;
            }

            public void onCreate(SQLiteDatabase db) {

                try {
                    db.execSQL(CREATE_TABLE);
                } catch (Exception e) {
                    //  Message.message(context,""+e);
                }
            }

           @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            // Message.message(context,"OnUpgrade");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        }catch (Exception e) {
            // Message.message(context,""+e);
        }
    }
        }
    }

When i try to access the other (Data2) database it cause a error !

android.database.sqlite.SQLiteException: no such table: Data2 (Sqlite code 1): , while compiling: SELECT _id, Item, SubItem FROM Data2, (OS error - 2:No such file or directory)

I Saw this on Log :

09-13 09:31:05.788 18454-18454/com.example.discopc.yubismart I/HwSecImmHelper: mSecurityInputMethodService is null
09-13 09:31:06.468 18454-18604/com.example.discopc.yubismart E/SQLiteLog: (1) 
09-13 09:31:06.571 18454-18604/com.example.discopc.yubismart I/Process: Sending signal. PID: 18454 SIG: 9

Whats the problem ? First database works fine but second one not ,

Thanks.... ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you said - I Wanted to add another TABLE to same database (MyDataBase)

You should not use two different class for creating two different table in sqlite database. While executing one single of your adapter classes its creating one table and if your db version is same / different in adapter classes then one of two table would not be created / would be deleted.

Here db version is same that's why second table is not creating.

Create as many as your required tables inside onCreate() of your myDbHelper class and inside onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) execute drop table code for each table.

When you need another new table just create table as above and write code for drop table inside onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion).

You just need to remember for creating new tables or any structural change inside your sqlite database would be reflected only after changing the VERSION CODE of database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...