java - SQLite数据库读取错误 'no such file or directory '

标签 java android database sqlite

您好,我正在手机上使用 android studio 使用 SQlite

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

数据库 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);
            }
        }
    }

我想向同一个数据库添加另一个表 (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

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

谢谢......?

最佳答案

正如您所说 - 我想将另一个表添加到同一个数据库(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.

此处的数据库版本相同,这就是未创建第二个表的原因。

myDbHelper 类的 onCreate()onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 内创建任意数量的表 code> 对每个表执行删除表代码。

当您需要另一个新表时,只需如上所述创建表并在 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 内编写删除表的代码

您只需要记住创建新表或 sqlite 数据库内的任何结构更改只有在更改数据库的版本代码后才会反射(reflect)出来。

关于java - SQLite数据库读取错误 'no such file or directory ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189225/

相关文章:

mysql - 如何解决此 SQL 错误 : Every derived table must have its own alias?

java - Cacerts 与 Java 版本的兼容性

java - 使用ANT编译后无法运行Java代码

android - Xamarin PCLStorage 包 : What is the root folder this PCLStorage reference to

java - 在 Java 中创建异常安全执行包装器

Python 加载一组 .csv 文件到 MySQL

java - 循环中的局部变量声明

java - 为什么 j_spring_security_check 404?

android - 使用 HDMI wifi dongle 在 Android 中进行截屏

php - 在循环、 sleep 循环或单独请求中查询数据库