数据未插入Sqlitedatabase中

时间:2015-08-26 05:11:07

标签: android database android-sqlite

我想在数据库表中插入一些值。 logcat中没有错误,但仍未在表中插入值。我无法确定问题。

这是数据库类。 的 FoodieDatabase.java

public class FoodieDatabase extends SQLiteOpenHelper {

       public String DATABASE_CREATE = "create table "
               + TableData.TableInfo.TABLE_NAME + "("
               + TableData.TableInfo.COLUMN_ID + " INTEGER primary key ,"
               + TableData.TableInfo.COLUMN_hostel + " TEXT not null,"
               + TableData.TableInfo.COLUMN_weekday + " TEXT not null,"
               + TableData.TableInfo.COLUMN_category + " TEXT not null,"
               + TableData.TableInfo.COLUMN_food + " TEXT not null,"
               + TableData.TableInfo.COLUMN_rate + " TEXT not null" + ")";


        private static final int DATABASE_VERSION = 1;

        // Database creation sql statement
        public FoodieDatabase(Context context) {
            super(context, TableData.TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
            Log.w("FoodieDatabase","Database Created");
        }

        @Override
        public void onCreate(SQLiteDatabase database) {
                    database.execSQL(DATABASE_CREATE);
            Log.w(FoodieDatabase.class.getName(),
                    " Table " + TableData.TableInfo.TABLE_NAME + " Successfully created. "
            );
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

        public void insertvalues(FoodieDatabase fdb, Integer id, String hostel,
                                 String weekday, String category, String food , String rate ){
            SQLiteDatabase sdb = fdb.getWritableDatabase();

            ContentValues cv = new ContentValues();
            cv.put(TableData.TableInfo.COLUMN_ID,id);
            cv.put(TableData.TableInfo.COLUMN_hostel,hostel );
            cv.put(TableData.TableInfo.COLUMN_weekday,weekday );
            cv.put(TableData.TableInfo.COLUMN_category,category );
            cv.put(TableData.TableInfo.COLUMN_food,food );
            cv.put(TableData.TableInfo.COLUMN_rate,rate );

            sdb.insert(TableData.TableInfo.TABLE_NAME, null, cv);
        }

        public Cursor retrievevalues(FoodieDatabase fdb){

            SQLiteDatabase sq = fdb.getReadableDatabase();
            String[] columns = {TableData.TableInfo.COLUMN_ID,
                    TableData.TableInfo.COLUMN_hostel,
                    TableData.TableInfo.COLUMN_weekday,
                    TableData.TableInfo.COLUMN_category,
                    TableData.TableInfo.COLUMN_food,
                    TableData.TableInfo.COLUMN_rate};
            Cursor CR = sq.query(TableData.TableInfo.TABLE_NAME,columns,null,null,null,null,null);
            return CR;
        }
    }

InsertData.java

public class InsertData extends AppCompatActivity {
    com.rengwuxian.materialedittext.MaterialEditText sno;
    com.rengwuxian.materialedittext.MaterialEditText hostel;
    com.rengwuxian.materialedittext.MaterialEditText category;
    com.rengwuxian.materialedittext.MaterialEditText food;
    com.rengwuxian.materialedittext.MaterialEditText weekday;
    com.rengwuxian.materialedittext.MaterialEditText rate;


    Button insert;

    String Shostel;
    String Scategory;
    String Sfood;
    String Sweekday;
    String Srate;

    Integer Isno;

    Context context= this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_data);

        sno = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.sno);
        sno.setText("1");
        hostel = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.hostel);
        category = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.category);
        food = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.food);
        weekday = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.weekday);
        rate = (com.rengwuxian.materialedittext.MaterialEditText) findViewById(R.id.rate);

        insert = (Button) findViewById(R.id.insert);

        Isno = Integer.parseInt(sno.getText().toString());
        Shostel = hostel.getText().toString();
        Scategory = category.getText().toString();
        Sfood = food.getText().toString();
        Sweekday = weekday.getText().toString();
        Srate = rate.getText().toString();

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FoodieDatabase fdb = new FoodieDatabase(context);
                fdb.insertvalues(fdb,Isno,Shostel,Sweekday,Scategory,Sfood,Srate);
                Toast.makeText(InsertData.this,"Value No." + Isno + Shostel + Scategory + "inserted successfully.",Toast.LENGTH_LONG).show();

                Intent intent = new Intent(InsertData.this, Home.class);
                InsertData.this.startActivity(intent);
                finish();
            }
        });
    }

InsertData.java 文件中,我使用Toast来检查IsnoShostelScategory等值。 ShostelScategory值在toast中为null。我的意思是他们是空白的,没有任何显示。虽然Isno始终显示我1最初设置的editText <div class="rating"> {% for i in 1..5 %} {% set starClass = (user.rating >= i ? "star voted" : "star-empty") %} <i class="{{ starClass }}"></i> {% endfor %} </div> 。 请告诉我你是否需要更多细节。在此先感谢!

1 个答案:

答案 0 :(得分:0)

您在用户查看UI之前存储值。在获得单击后,您应该从EditTexts中输入值 -

    insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Isno = Integer.parseInt(sno.getText().toString());
            Shostel = hostel.getText().toString();
            Scategory = category.getText().toString();
            Sfood = food.getText().toString();
            Sweekday = weekday.getText().toString();
            Srate = rate.getText().toString();

            FoodieDatabase fdb = new FoodieDatabase(context);
            fdb.insertvalues(fdb,Isno,Shostel,Sweekday,Scategory,Sfood,Srate);
            Toast.makeText(InsertData.this,"Value No." + Isno + Shostel + Scategory + "inserted successfully.",Toast.LENGTH_LONG).show();

            Intent intent = new Intent(InsertData.this, Home.class);
            InsertData.this.startActivity(intent);
            finish();
        }
    });