SQLite数据库读取错误“没有这样的文件或目录" [英] SQLite DataBase Read Error 'no such file or directory '

查看:47
本文介绍了SQLite数据库读取错误“没有这样的文件或目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用 android studio

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

I have a simple database like this :

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

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

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

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);

数据库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 )数据库时,会导致错误!

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)

我在Log上看到了这个

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 ,

谢谢....?

推荐答案

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

您不应使用两个不同的类在sqlite数据库中创建两个不同的表.在执行一个适配器类中的一个时,它会创建一个表,并且如果您的数据库版本在适配器类中相同/不同,那么将不会创建/将删除两个表中的一个.

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)内部创建尽可能多的所需表.为每个表执行删除表代码.

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.

当您需要另一个新表时,只需如上所述创建表并在 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)内为drop 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).

您只需要记住要创建新表,否则sqlite数据库内部的任何结构更改只有在更改数据库的版本代码后才会反映出来.

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.

这篇关于SQLite数据库读取错误“没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆