当我将第4列放入数据库查询时,我的应用程序崩溃了

时间:2017-01-15 16:40:36

标签: android

我正在制作农业应用。以下是我遇到问题的表格代码。

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

在我的应用程序中,fieldname,Area,Measure_unit的值是从用户获得的,并且我为种植的作物提供了NA(代理值)。这是我的代码。我刚刚加入了这个功能。

 private void storetodatabase(String name,int area_vlaue,String type){
        String ss="NA";
        SQLiteOpenHelper AgroDatabase=new AgroDatabase(this);
        SQLiteDatabase database = AgroDatabase.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("FieldName", name);
        values.put("Area", area_vlaue);
        values.put("Measure_Unit",type);
        values.put("CropGrown",ss);
        database.insert("fieldinfo", null, values);
        database.close();

} 现在,当我进行包含CropGrown列的数据库查询时,应用程序开始崩溃。如果我排除这一切一切正常,但包括这导致我的应用程序崩溃。我正在使用列表活动。

     @Override

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_homescreen);
            ListView m_listview = (ListView) findViewById(R.id.list_view);

            SQLiteOpenHelper AgroDatabase = new AgroDatabase(this);
             db = AgroDatabase.getReadableDatabase();
            cursor=db.query("fieldinfo",new String[]{"_id",    "FieldName","Area","Measure_Unit","CropGrown"
,null,null,null,null,null);



            CursorAdapter listAdapter= new SimpleCursorAdapter(this,
                                                android.R.layout.simple_list_item_1,
                                                cursor,
                                                new String[]{"FieldName"},
                                                new int[]{android.R.id.text1},0);

            m_listview.setAdapter(listAdapter);
            m_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    Intent intent = new Intent(homescreenjava.this, Addition1.class);
                    final String name= cursor.getString(1);
                    final String Area=cursor.getString(2);
                    final String M_unit=cursor.getString(3);
                    final String Crop_grown=cursor.getString(4);
                Bundle extras = new Bundle();

                    extras.putString("name",name);
                    extras.putString("Area",Area);
                    extras.putString("Measurement Unit",M_unit);
                   extras.putString("Crop planted",Crop_grown);
                   intent.putExtras(extras);
                    startActivity(intent);


                }
            });


        } 

1 个答案:

答案 0 :(得分:1)

您的SQL语句中缺少逗号。

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