0

I have got three tables in my database and only one of them has values inserted at time of creation. SO the code of my database is as follows.

    public class AgroDatabase extends SQLiteOpenHelper {
             private  static  String DB_NAME="AgroData";
        private  static  String TABLE_VARIABLES="variables";
        private  static  String TABLE_USERINFO="userinfo";
        private static String TABLE_FIELDINFO="fieldinfo";

        private static int DB_VERSION=1;

        AgroDatabase(Context context)
        {super(context,DB_NAME,null,DB_VERSION);//null is for cursors//sqlite helper classes constructor is being called

        }

        @Override
        public void onCreate(SQLiteDatabase db){//Sqlitedatabase class gives us access to database
        db.execSQL("CREATE TABLE "+TABLE_VARIABLES+"("
                +"_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "Crop_Name TEXT,"
                + "Temp_Max REAL,"
                + "Temp_Min REAL,"
                + "Humidity_Max REAL,"
                + "Humidity_Min REAL,"
                + "PH_Max REAL,"
                + "PH_Min REAL,"
                + "Rain REAL,"

                +"Time_To_Harvest INTEGER,"

                + "Sunshine_Days INTEGER);");



            db.execSQL("CREATE TABLE "+TABLE_USERINFO+"("
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "User_Name TEXT,"
                    + "Location TEXT,"
                    + "CurrentNoFields INTEGER);");
            db.execSQL("CREATE TABLE "+TABLE_FIELDINFO+"("
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "FieldName TEXT,"
                    + "Area TEXT,"
                    + "Measure_Unit TEXT,"
                    + "CropGrown  TEXT,"
                    +"Growth_Date   INTEGER,"
                    +"Growth_Month   INTEGER,"
                    +"Growth_Year   INTEGER);");

            insert_Data(db,"Rice",35,20,80,60,(float)6.5,(float)5.0,1400,150,6);
            insert_Data(db,"Wheat",12,30,60,50,(float)3.7,7,310,120,6);
            insert_Data(db,"Soybean",27,15,60,50,6,(float)6.8,60,100,6);
            insert_Data(db,"Strawberries",27,15,60,50,7,(float)3.7,310,120,6);
            insert_Data(db,"Peas",(float)24.1,(float)15.5,40,60,6,(float)7.5,0,70,6);
            insert_Data(db,"Potatoes",30,10,80,50,6,(float)4.5,0,110,6);
            insert_Data(db,"Pumpkin",32,21,60,40,7,(float)5.5,600,100,6);
            insert_Data(db,"Onions",35,21,60,40,7,6,0,100,6);
            insert_Data(db,"Corn",24,15,60,40,7,(float)5.5,0,75,6);
            insert_Data(db,"LimaBeans",24,18,60,40,7,6,0,80,6);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){


        }


    private static void insert_Data(SQLiteDatabase db,String name,float tempmax,float tempmin,float humiditymax,
                                   float humidity_min,float phmax,float phmin,float rainmax,
                                    int T2H,int Sunshine){



        ContentValues crop_data= new ContentValues();
        crop_data.put("Crop_Name",name);
        crop_data.put("Temp_Max",tempmax);
        crop_data.put("Temp_Min",tempmin);
        crop_data.put("Humidity_Max",humiditymax);
        crop_data.put("Humidity_Min",humidity_min);
        crop_data.put("PH_Max",phmax);
        crop_data.put("PH_Min",phmin);
        crop_data.put("Rain",rainmax);

        crop_data.put("Time_To_Harvest",T2H);
        crop_data.put("Sunshine)Days",Sunshine);

                   db.insert("variables",null,crop_data);

    }






}

But data is not being filled into this table . I can access other tables where data is filled in other classes but i cant fill data here.

Sagar Acharya
  • 1,763
  • 2
  • 19
  • 38
  • Try Using Realm,instead of SQLite, if u can somehow arrange ur existing data in Plain Java Objects,it's more simple and a lot cleaner. – Avinash Roy Jan 20 '17 at 09:59
  • You need to post the whole code. There is not enough information to diagnose your problem. i.e. the SQLiteOpenHelper OnCreate etc. – abstractx1 Jan 20 '17 at 09:59
  • export the database file from your rooted device ...app/data/data/database/ and check tables are having data or not. I think issue is with your query. Cursor cursor = db.query("variables", new String[]{"_id", "Crop_Name", "Temp_Max", "Temp_Min", "Humidity_Max", "Humidity_Min", "PH_Max", "PH_Min", "Time_To_Harvest", "Sunshine_Days"}, null, null, null, null, null ); which doesn't mean anything. – Ready Android Jan 20 '17 at 10:00
  • Doesn't that mean take all the data from the table? – Sagar Acharya Jan 20 '17 at 10:04
  • See you had written code to create table db.execSQL("CREATE TABLE.... you had written code to insert data in created table db.insert("variables".... but where is the query to fetch the inserted data. I mean where is the query Select * from ...... – Ready Android Jan 20 '17 at 10:17
  • table creation is okay i guess. i checked if i had mistyped some colum but they seemed okay too – Sagar Acharya Jan 20 '17 at 10:20
  • Check with this code in findBestCrop method. String countQuery = "SELECT * FROM " + TABLE_NAME; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int cnt = cursor.getCount(); cursor.close(); – Ready Android Jan 20 '17 at 10:22
  • when i tried similar code to my other tables it works fluently. But the problem is only in this table. It may be because i am calling this table in a class that doesnot have a activity or data has not been insterted at all. i will try to query my other tables and try to find out the problem. – Sagar Acharya Jan 20 '17 at 10:32
  • I have confirmed that no data is being inserted into my table. Anyone know why? – Sagar Acharya Jan 20 '17 at 11:23
  • The problem was that i was using REAL as my database element, once i made it integer everything was okay. Dont know why though. – Sagar Acharya Jan 20 '17 at 15:30

0 Answers0