在我的Android应用程序中使用两个不同的类的sqlite db中的错误

时间:2021-02-14 20:05:08
package com.example.myfirstapp;


public class DB_Moshtari {



    /******************** if debug is set true then it will show all Logcat message ************/
    public static final boolean DEBUG = true;

    /******************** Logcat TAG ************/
    public static final String LOG_TAG = "DBMoshtari";

    /******************** Table Fields ************/
    public static final String KEY_ID = "_id";

    public static final String KEY_Moshtari_NAME = "Moshtari_name";

    public static final String KEY_Moshtari_ADDRES = "Moshtari_addres";

    public static final String KEY_Moshtari_tel = "Moshtari_tel";

    /******************** Database Name ************/
    public static final String DATABASE_NAME = "DB_sqllite";

    /******************** Database Version (Increase one if want to also upgrade your database) ************/
    public static final int DATABASE_VERSION = 1;// started at 1

    /** Table names */
    public static final String Moshtari_TABLE = "tbl_Moshtari";

    /******************** Set all table with comma seperated like Moshtari_TABLE,ABC_TABLE ************/
    private static final String[] ALL_TABLES = { Moshtari_TABLE };

    /** Create table syntax */
    private static final String Moshtari_CREATE = "create table tbl_Moshtari(_id integer primary key autoincrement," +
            " Moshtari_name text not null, Moshtari_addres text not null,Moshtari_tel text not null);";

    /******************** Used to open database in syncronized way ************/
    private static DataBaseHelper DBHelper = null;

    protected DB_Moshtari() {
        // TODO Auto-generated constructor stub

    }
    /******************* Initialize database *************/
    public static void init(Context context) {
        if (DBHelper == null) {
            if (DEBUG)
                Log.i("DBMoshtari", context.toString());
            DBHelper = new DataBaseHelper(context);
        }
    }

  /********************** Main Database creation INNER class ********************/
    private static class DataBaseHelper extends SQLiteOpenHelper {
        public DataBaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            if (DEBUG)
                Log.i(LOG_TAG, "new create");
            try {
                db.execSQL(Moshtari_CREATE);


            } catch (Exception exception) {
                if (DEBUG)
                    Log.i(LOG_TAG, "Exception onCreate() exception");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (DEBUG)
                Log.w(LOG_TAG, "Upgrading database from version" + oldVersion
                        + "to" + newVersion + "...");

            for (String table : ALL_TABLES) {
                db.execSQL("DROP TABLE IF EXISTS " + table);
            }
            onCreate(db);
        }

    } // Inner class closed


    /********************** Open database for insert,update,delete in syncronized manner ********************/
    private static synchronized SQLiteDatabase open() throws SQLException {
        return DBHelper.getWritableDatabase();
    }


    /************************ General functions**************************/


    /*********************** Escape string for single quotes (Insert,Update)************/
    private static String sqlEscapeString(String aString) {
        String aReturn = "";

        if (null != aString) {
            //aReturn = aString.replace("'", "''");
            aReturn = DatabaseUtils.sqlEscapeString(aString);
            // Remove the enclosing single quotes ...
            aReturn = aReturn.substring(1, aReturn.length() - 1);
        }

        return aReturn;
    }
    /*********************** UnEscape string for single quotes (show data)************/
    private static String sqlUnEscapeString(String aString) {   
        String aReturn = "";

        if (null != aString) {
            aReturn = aString.replace("''", "'");
        }

        return aReturn;
    }


    /********************************************************************/


     /**
     * All Operations (Create, Read, Update, Delete) 
     * @param <MoshtariData>
     */
    // Adding new contact

    public static void addMoshtariData(Moshtari mData) {
        final SQLiteDatabase db = open();

        String name = sqlEscapeString(mData.getName());
        String addres = sqlEscapeString(mData.getAddress());
        String tel = sqlEscapeString(mData.getTel());

        ContentValues cVal = new ContentValues();
        cVal.put(KEY_Moshtari_NAME, name);
        cVal.put(KEY_Moshtari_ADDRES, addres);
        cVal.put(KEY_Moshtari_tel, tel );

        db.insert(Moshtari_TABLE, null, cVal);
        db.close(); // Closing database connection
    }








    // Getting single contact
    public static Moshtari getMoshtariData(int id) {
        final SQLiteDatabase db = open();

        Cursor cursor = db.query(Moshtari_TABLE, new String[] { KEY_ID,
                KEY_Moshtari_NAME, KEY_Moshtari_ADDRES,KEY_Moshtari_tel }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Moshtari data = new Moshtari(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2), cursor.getString(3));
        // return contact
        return data;
    }

    // Getting All Contacts
    public static List<Moshtari> getAllMoshtariData() {
        List<Moshtari> contactList = new ArrayList<Moshtari>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + Moshtari_TABLE;

        final SQLiteDatabase db = open();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Moshtari data = new Moshtari();
                data.setId(Integer.parseInt(cursor.getString(0)));
                data.setName(cursor.getString(1));
                data.setAddress(cursor.getString(2));
                data.setTel(cursor.getString(3));

                // Adding contact to list
                contactList.add(data);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Updating single contact
    public static int updateMoshtariData(Moshtari data) {
        final SQLiteDatabase db = open();

        ContentValues values = new ContentValues();
        values.put(KEY_Moshtari_NAME, data.getName());
        values.put(KEY_Moshtari_ADDRES, data.getAddress());
        values.put(KEY_Moshtari_tel, data.getTel());

        // updating row
        return db.update(Moshtari_TABLE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(data.getId()) });
    }

    // Deleting single contact
    public static void deleteMoshtariData(Moshtari data) {
        final SQLiteDatabase db = open();
        db.delete(Moshtari_TABLE, KEY_ID + " = ?",
                new String[] { String.valueOf(data.getId()) });
        db.close();
    }

    // Getting contacts Count
    public static int getMoshtariDataCount() {
        String countQuery = "SELECT  * FROM " + Moshtari_TABLE;
        final SQLiteDatabase db = open();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}

package com.example.myfirstapp;



public class DB_Taminkonande {






    /******************** if debug is set true then it will show all Logcat message ************/
    public static final boolean DEBUG = true;

    /******************** Logcat TAG ************/
    public static final String LOG_TAG = "DBTaminkonande";

    /******************** Table Fields ************/
    public static final String KEY_ID = "_id";

    public static final String KEY_Taminkonande_NAME = "Taminkonande_name";

    public static final String KEY_Taminkonande_ADDRES = "Taminkonande_addres";

    public static final String KEY_Taminkonande_tel = "Taminkonande_tel";

    /******************** Database Name ************/
    public static final String DATABASE_NAME = "DB_sqllite";

    /******************** Database Version (Increase one if want to also upgrade your database) ************/
    public static final int DATABASE_VERSION = 1;// started at 1

    /** Table names */
    public static final String Taminkonande_TABLE = "tbl_Taminkonande";

    /******************** Set all table with comma seperated like Taminkonande_TABLE,ABC_TABLE ************/
    private static final String[] ALL_TABLES = { Taminkonande_TABLE };

    /** Create table syntax */
    private static final String Taminkonande_CREATE = "create table tbl_Taminkonande(_id integer primary key autoincrement," +
            " Taminkonande_name text not null, Taminkonande_addres text not null,Taminkonande_tel text not null);";

    /******************** Used to open database in syncronized way ************/
    private static DataBaseHelper DBHelper = null;

    protected DB_Taminkonande() {
        // TODO Auto-generated constructor stub

    }
    /******************* Initialize database *************/
    public static void init(Context context) {
        if (DBHelper == null) {
            if (DEBUG)
                Log.i("DBTaminkonande", context.toString());
            DBHelper = new DataBaseHelper(context);
        }
    }

  /********************** Main Database creation INNER class ********************/
    private static class DataBaseHelper extends SQLiteOpenHelper {
        public DataBaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            if (DEBUG)
                Log.i(LOG_TAG, "new create");
            try {
                db.execSQL(Taminkonande_CREATE);


            } catch (Exception exception) {
                if (DEBUG)
                    Log.i(LOG_TAG, "Exception onCreate() exception");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (DEBUG)
                Log.w(LOG_TAG, "Upgrading database from version" + oldVersion
                        + "to" + newVersion + "...");

            for (String table : ALL_TABLES) {
                db.execSQL("DROP TABLE IF EXISTS " + table);
            }
            onCreate(db);
        }

    } // Inner class closed


    /********************** Open database for insert,update,delete in syncronized manner ********************/
    private static synchronized SQLiteDatabase open() throws SQLException {
        return DBHelper.getWritableDatabase();
    }


    /************************ General functions**************************/


    /*********************** Escape string for single quotes (Insert,Update)************/
    private static String sqlEscapeString(String aString) {
        String aReturn = "";

        if (null != aString) {
            //aReturn = aString.replace("'", "''");
            aReturn = DatabaseUtils.sqlEscapeString(aString);
            // Remove the enclosing single quotes ...
            aReturn = aReturn.substring(1, aReturn.length() - 1);
        }

        return aReturn;
    }
    /*********************** UnEscape string for single quotes (show data)************/
    private static String sqlUnEscapeString(String aString) {   
        String aReturn = "";

        if (null != aString) {
            aReturn = aString.replace("''", "'");
        }

        return aReturn;
    }


    /********************************************************************/


     /**
     * All Operations (Create, Read, Update, Delete) 
     * @param <TaminkonandeData>
     */
    // Adding new contact

    public static void addTaminkonandeData(Taminkonande mData) {
        final SQLiteDatabase db = open();

        String name = sqlEscapeString(mData.getName());
        String addres = sqlEscapeString(mData.getAddress());
        String tel = sqlEscapeString(mData.getTel());

        ContentValues cVal = new ContentValues();
        cVal.put(KEY_Taminkonande_NAME, name);
        cVal.put(KEY_Taminkonande_ADDRES, addres);
        cVal.put(KEY_Taminkonande_tel, tel );

        db.insert(Taminkonande_TABLE, null, cVal);
        db.close(); // Closing database connection
    }








    // Getting single contact
    public static Taminkonande getTaminkonandeData(int id) {
        final SQLiteDatabase db = open();

        Cursor cursor = db.query(Taminkonande_TABLE, new String[] { KEY_ID,
                KEY_Taminkonande_NAME, KEY_Taminkonande_ADDRES,KEY_Taminkonande_tel }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Taminkonande data = new Taminkonande(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2), cursor.getString(3));
        // return contact
        return data;
    }

    // Getting All Contacts
    public static List<Taminkonande> getAllTaminkonandeData() {
        List<Taminkonande> contactList = new ArrayList<Taminkonande>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + Taminkonande_TABLE;

        final SQLiteDatabase db = open();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Taminkonande data = new Taminkonande();
                data.setId(Integer.parseInt(cursor.getString(0)));
                data.setName(cursor.getString(1));
                data.setAddress(cursor.getString(2));
                data.setTel(cursor.getString(3));

                // Adding contact to list
                contactList.add(data);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Updating single contact
    public static int updateTaminkonandeData(Taminkonande data) {
        final SQLiteDatabase db = open();

        ContentValues values = new ContentValues();
        values.put(KEY_Taminkonande_NAME, data.getName());
        values.put(KEY_Taminkonande_ADDRES, data.getAddress());
        values.put(KEY_Taminkonande_tel, data.getTel());

        // updating row
        return db.update(Taminkonande_TABLE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(data.getId()) });
    }

    // Deleting single contact
    public static void deleteTaminkonandeData(Taminkonande data) {
        final SQLiteDatabase db = open();
        db.delete(Taminkonande_TABLE, KEY_ID + " = ?",
                new String[] { String.valueOf(data.getId()) });
        db.close();
    }

    // Getting contacts Count
    public static int getTaminkonandeDataCount() {
        String countQuery = "SELECT  * FROM " + Taminkonande_TABLE;
        final SQLiteDatabase db = open();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}

i have this two classes , when i install my app and use one of this classes , the other class will not work, for example i add Moshtari in my DB_Moshtari , then go for another means DB_Taminkonande to add a row in it , it says "no such tbl_Taminkonande"

我有这两个类,当我安装我的应用程序并使用其中一个类时,另一个类将无法工作,例如我在我的DB_Moshtari中添加Moshtari,然后转到另一个意味着DB_Taminkonande在其中添加一行,它说“没有这样的tbl_Taminkonande“

1 个解决方案

#1


0  

For the first time when you install app and use DB_Moshtari, Android OS creates database and creates table Moshtari_CREATE only. Next time, when you try to run DB_Taminkonande created (same database name is specified) onCreate() method is not called. Hence, table no such tbl_Taminkonande exception.

当您第一次安装应用程序并使用DB_Moshtari时,Android OS会创建数据库并仅创建表Moshtari_CREATE。下次,当您尝试运行DB_Taminkonande创建(指定相同的数据库名称)时,不会调用onCreate()方法。因此,表没有这样的tbl_Taminkonande异常。

The approach is not good, please change the approach.

方法不好,请改变方法。

#1


0  

For the first time when you install app and use DB_Moshtari, Android OS creates database and creates table Moshtari_CREATE only. Next time, when you try to run DB_Taminkonande created (same database name is specified) onCreate() method is not called. Hence, table no such tbl_Taminkonande exception.

当您第一次安装应用程序并使用DB_Moshtari时,Android OS会创建数据库并仅创建表Moshtari_CREATE。下次,当您尝试运行DB_Taminkonande创建(指定相同的数据库名称)时,不会调用onCreate()方法。因此,表没有这样的tbl_Taminkonande异常。

The approach is not good, please change the approach.

方法不好,请改变方法。