【问题标题】:SQLite DataBase Read Error 'no such file or directory 'SQLite 数据库读取错误'没有这样的文件或目录'
【发布时间】:2017-09-13 05:05:54
【问题描述】:

您好,我在手机上使用SQliteandroid studio 合作

我有一个这样的简单数据库:

数据库 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 + "\n");
        }
        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);
            }
        }
    }

我想在同一个数据库中添加另一个 TABLE (MyDataBase)

所以我创建了另一个名为 MyDbAdapter2 的 java 类

与上面相同的代码只是更改了类名和表名

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

数据库 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 + "\n");
            }
            //  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);
        }
    }
        }
    }

当我尝试访问另一个 (Data2) 数据库时,它会导致错误!

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)

我在日志上看到了这个:

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

有什么问题?第一个数据库工作正常,但第二个不行,

谢谢....?

【问题讨论】:

  • 你能贴上myDbAdapter2的升级代码吗?
  • 您正在创建相同的数据库/数据库名称与相同的位置相同,尝试更改 myDbAdapter2 中的数据库名称,为什么要创建 2 个数据库,您只需要在同一个数据库中创建 2 个表,无需创建另一个数据库。
  • 两个表不需要两个数据库,也不需要两个适配器类来访问两个表
  • 我只想创建 1 个数据库,将名称更改为 MyDataBase2 可以,但为什么?
  • @Disco4uf 试试这个,不太确定结果。就像 myDbAdapter 中的 getData() 创建一个类似 createTable() 的方法,尝试创建第二个表,我认为它会起作用?

标签: java android database sqlite


【解决方案1】:

正如你所说 - 我想将另一个 TABLE 添加到同一个数据库 (MyDataBase)

您不应该使用两个不同的类在 sqlite 数据库中创建两个不同的表。在执行一个适配器类时,它会创建一个表,如果适配器类中的数据库版本相同/不同,则不会创建/删除两个表之一。

这里的数据库版本相同,这就是第二个表没有创建的原因。

myDbHelper 类的onCreate() 中创建所需的表,在onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 中为每个表执行删除表代码。

当您需要另一个新表时,只需像上面一样创建表并在onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 中编写删除表的代码。

您只需要记住创建新表或 sqlite 数据库中的任何结构更改只有在更改数据库的 VERSION CODE 后才会反映。

【讨论】:

    【解决方案2】:

    您已经创建了一个名为 MyDataBase 的数据库,其中包含表 Data。 您再次尝试使用导致问题的表 Data2 创建相同的 MyDataBase。

    “我只想创建 1 个数据库,将名称更改为 MyDataBase2 可以,但为什么?”

    当您将名称更改为 MyDataBase2 时,它会起作用,因为这是一个全新的数据库,您可以在其中创建 Data2 表。

    因此,如果您想在您的第一个数据库版本中创建 Data2,您必须在其中创建表 Data2,但不是一个全新的数据库。如果您想了解更多信息,请查找here

    我相信这回答了您的问题。

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 1970-01-01
      • 2018-12-31
      • 2021-09-10
      • 2021-10-31
      • 1970-01-01
      • 2018-08-12
      • 2023-04-07
      • 2013-08-08
      相关资源
      最近更新 更多