मैं DataHelper.java के साथ कुछ तालिका बनाउंगा, लेकिन केवल एक तालिका बनाई गई है (स्थान तालिका), यहां मेरा कोड

public class DataHelper extends SQLiteOpenHelper {

    private static final String LOGCAT = null;
    private static final String DATABASE_NAME = "layerfarm-mobile.db";
    private static final int DATABASE_VERSION = 1;

    public static final String locationSQL = "CREATE TABLE IF NOT EXISTS location(id integer primary key, name text, address text, rid integer);";
    public static final String eqq_qualitySQL = "CREATE TABLE IF NOT EXISTS  eqq_quality(id integer primary key, name text);";

    public DataHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d(LOGCAT,"Created");
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            //Create table
            db.execSQL(locationSQL);
            db.execSQL(eqq_qualitySQL);

        String InsertlocationSQL = "INSERT INTO location (id, name, address, rid) VALUES " +
                "(1,'Location A','Blitar',1)," +
                "(2,'Location B','Blitar',2)," +
                "(3,'Location C','Blitar',3)," +
                "(4,'Location D','Blitar',4);";
        db.execSQL(InsertlocationSQL);

        String InsertEggQualitySQL = "INSERT INTO egg_quality (id, name) VALUES " +
                "(1,'Quality A'), " +
                "(2,'Quality B'), " +
                "(3,'Quality C'), " +
                "(4,'Cracked');";
        db.execSQL(InsertEggQualitySQL);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop table if existed, all data will be gone!!!
        db.execSQL("DROP TABLE IF EXISTS location;"); 
    db.execSQL("DROP TABLE IF EXISTS eqq_quality;"); onCreate(db);
    }

}

स्थान तालिका के लिए तालिका बनाएं और सम्मिलित करें सफलतापूर्वक है, लेकिन अन्य तालिका विफल है, त्रुटि लॉग यहां दिखाएं:

 Caused by: android.database.sqlite.SQLiteException: no such table: 
 egg_quality (code 1): , while compiling: SELECT * FROM egg_quality

मैं इसे कैसे ठीक कर सकता हूं?

धन्यवाद

-3
Nanang Hidayatulloh 12 पद 2018, 05:13

1 उत्तर

सबसे बढ़िया उत्तर

आपकी समस्या यह है कि आपने अलग-अलग टेबल नामों को कोड किया है। तालिका eqq_table का उपयोग करके बनाई गई है, जबकि क्वेरी अंडे_टेबल तक पहुंचने का प्रयास करती है।

ऐसी गलतियों की संभावना को कम करने का एक तरीका ऐसे मूल्यों के लिए CONSTANT को परिभाषित करना है और फिर हमेशा संबंधित CONSTANT के माध्यम से आइटम (तालिका नाम, स्तंभ नाम, अनुक्रमणिका नाम आदि) को संदर्भित करना है।

जैसे मैं बदलने का सुझाव दूंगा: -

public class DataHelper extends SQLiteOpenHelper {

    private static final String LOGCAT = null;
    private static final String DATABASE_NAME = "layerfarm-mobile.db";
    private static final int DATABASE_VERSION = 1;

    public static final String locationSQL = "CREATE TABLE IF NOT EXISTS location(id integer primary key, name text, address text, rid integer);";
    public static final String eqq_qualitySQL = "CREATE TABLE IF NOT EXISTS  eqq_quality(id integer primary key, name text);";

    public DataHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d(LOGCAT,"Created");
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            //Create table
            db.execSQL(locationSQL);
            db.execSQL(eqq_qualitySQL);

        String InsertlocationSQL = "INSERT INTO location (id, name, address, rid) VALUES " +
                "(1,'Location A','Blitar',1)," +
                "(2,'Location B','Blitar',2)," +
                "(3,'Location C','Blitar',3)," +
                "(4,'Location D','Blitar',4);";
        db.execSQL(InsertlocationSQL);

        String InsertEggQualitySQL = "INSERT INTO egg_quality (id, name) VALUES " +
                "(1,'Quality A'), " +
                "(2,'Quality B'), " +
                "(3,'Quality C'), " +
                "(4,'Cracked');";
        db.execSQL(InsertEggQualitySQL);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop table if existed, all data will be gone!!!
        db.execSQL("DROP TABLE IF EXISTS location;"); 
    db.execSQL("DROP TABLE IF EXISTS eqq_quality;"); onCreate(db);
    }
}

प्रति :-

public class DataHelper extends SQLiteOpenHelper {

    private static final String LOGCAT = null;
    private static final String DATABASE_NAME = "layerfarm-mobile.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_LOCATION = "location";
    public static final String TABLE_EGGQUALITY = "eqq_quality";

    public static final String COL_LOCATION_ID = "id";
    public static final String COL_LOCATION_NAME = "name";
    public static final String COL_LOCATION_ADDRESS = "address";
    public static final String COL_LOCATION_RID = "rid";

    public static final String COl_EGGQUALITY_ID = "id";
    public static final String COL_EGGQUALITY_NAME = "name";

    private String locationSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_LOCATION + "(" +
            COL_LOCATION_ID + " INTEGER PRIMARY KEY, " +
            COL_LOCATION_NAME + " TEXT, " +
            COL_LOCATION_ADDRESS + " TEXT," +
            COL_LOCATION_RID + " INTEGER" +
            ")";
    private String egg_qualitySQL = "CREATE TABLE IF NOT EXISTS " + TABLE_EGGQUALITY + "(" +
            COl_EGGQUALITY_ID + " INTEGER PRIMARY KEY, " +
            COL_EGGQUALITY_NAME + " TEXT" +
            ")";

    SQLiteDatabase mDB;

    public DataHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mDB = this.getWritableDatabase();
        //Log.d(LOGCAT,"Created"); //????? Not correct as the database doesn't get created until an attempt is made to open it
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        mDB = db;

        /* USING TRY CATCH MAY CAUSE A GREAT DEAL OF CONFUSION
        try {
            //Create table
            db.execSQL(locationSQL);
            db.execSQL(eqq_qualitySQL);

            String InsertlocationSQL = "INSERT INTO location (id, name, address, rid) VALUES " +
                    "(1,'Location A','Blitar',1)," +
                    "(2,'Location B','Blitar',2)," +
                    "(3,'Location C','Blitar',3)," +
                    "(4,'Location D','Blitar',4);";
            db.execSQL(InsertlocationSQL);

            String InsertEggQualitySQL = "INSERT INTO egg_quality (id, name) VALUES " +
                    "(1,'Quality A'), " +
                    "(2,'Quality B'), " +
                    "(3,'Quality C'), " +
                    "(4,'Cracked');";
            db.execSQL(InsertEggQualitySQL);
        }catch (Exception e){
            e.printStackTrace();
        }
        */
        db.execSQL(locationSQL);
        db.execSQL(egg_qualitySQL);
        insertLocation("Location A","Blitar",1);
        insertLocation("Location B", "Blitar",2);
        insertLocation("Location C","Blitar ",3);
        insertLocation("Location D","Blitar",4);
        insertEggQuality("Quality A");
        insertEggQuality("Quality B");
        insertEggQuality("Quality C");
        insertEggQuality("Quality B");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop table if existed, all data will be gone!!!
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATION);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EGGQUALITY); 
        onCreate(db);
    }

    /**
     * NOTE as ID column is defined as INTEGER PRIMARY KEY, then SQlite will,
     * if no value is specified when inserting a row,
     * generate a unique value for the id column
     *  (initially 1, then likely 2, then likely 3 ......... )
     */
    public long insertLocation(String name, String address, long rid) {
        ContentValues cv = new ContentValues();
        cv.put(COL_LOCATION_NAME,name);
        cv.put(COL_LOCATION_ADDRESS,address);
        cv.put(COL_LOCATION_RID,rid);
        return mDB.insert(TABLE_LOCATION,null,cv);
    }

    public long insertEggQuality(String name) {
        ContentValues cv = new ContentValues();
        cv.put(COL_EGGQUALITY_NAME,name);
        return mDB.insert(TABLE_EGGQUALITY,null,cv);
    }

    public Cursor getAllLocations() {
        return getAll(TABLE_LOCATION);
    }

    public Cursor getAllEggQuality() {
        return getAll(TABLE_EGGQUALITY);
    }

    public Cursor getAll(String table) {
        return mDB.query(table,null,null,null,null,null,null);
    }
}
  • अन्य सुझाए गए सुधार शामिल किए गए हैं
  • ध्यान दें कि यदि ऑनक्रिएट विधि के रूप में तालिकाओं को जोड़ना या बदलना केवल तब चलता है जब डेटाबेस पहली बार बनाया जाता है, तो आपको या तो ऐप के डेटा को हटा देना चाहिए, ऐप को अनइंस्टॉल करना चाहिए या संस्करण संख्या को बढ़ाना चाहिए और फिर ऐप को फिर से चलाना चाहिए (यह देखते हुए कि सभी तीन विधियों का परिणाम होगा डेटा का नुकसान)।

उपरोक्त का उपयोग एक गतिविधि में इस प्रकार किया जा सकता है: -

public class MainActivity extends AppCompatActivity {

    DataHelper mDBHlpr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHlpr = new DataHelper(this);

        Cursor csr = mDBHlpr.getAllLocations();
        DatabaseUtils.dumpCursor(csr);
        csr = mDBHlpr.getAllEggQuality();
        DatabaseUtils.dumpCursor(csr);
    }
}

डंप कर्सर का परिणाम समान होगा: -

12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534aac14
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 0 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=1
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Location A
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    address=Blitar
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    rid=1
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 1 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=2
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Location B
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    address=Blitar
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    rid=2
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 2 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=3
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Location C
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    address=Blitar 
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    rid=3
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 3 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=4
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Location D
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    address=Blitar
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    rid=4
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: <<<<<
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534c5e0c
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 0 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=1
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Quality A
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 1 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=2
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Quality B
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 2 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=3
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Quality C
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: 3 {
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    id=4
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out:    name=Quality B
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: }
12-12 04:32:23.854 1248-1248/so53700541.so53700541updatewithexpression I/System.out: <<<<<
0
MikeT 12 पद 2018, 08:32