SQLite向表中添加行

时间:2014-05-17 06:04:55

标签: android sqlite

我试图将成员添加到成员资格表中。我的SQLite数据库已经可以成功地将行添加到其他表,例如我的book表和inventory表。出于某种原因,它不会在表格中添加新成员,这让我感到困惑,因为我似乎只是复制了与我成功添加到其他表格的插入语句相同的结构。这可能会出现什么问题?

MembershipAdapter

public void insertEntry(String FIRSTNAME, String LASTNAME, String BIRTHDAY,
        String MEMBER_ID, String EMAIL, int CHECKOUT_COUNT, int KARMA_PTS,
        String NOTES)
{
   ContentValues newValues = new ContentValues();
    // Assign values for each row.
   if(FIRSTNAME.equals(null))
       newValues.put("FIRST_NAME", "");
   else
        newValues.put("FIRST_NAME", FIRSTNAME);
   if(LASTNAME.equals(null))
       newValues.put("LAST_NAME", "");
   else
       newValues.put("LAST_NAME", LASTNAME);
   if(BIRTHDAY.equals(null))
        newValues.put("BIRTHDAY", "");
   else
       newValues.put("BIRTHDAY", BIRTHDAY);
   if(MEMBER_ID.equals(null))
        newValues.put("MEMBER_ID", "");
   else
       newValues.put("MEMBER_ID", MEMBER_ID);
   if(EMAIL.equals(null))
        newValues.put("EMAIL", "");
   else
       newValues.put("EMAIL", EMAIL);
   if(CHECKOUT_COUNT==0)
        newValues.put("CHECKOUT_COUNT", 0);
   else
       newValues.put("CHECKOUT_COUNT", CHECKOUT_COUNT);
   if(KARMA_PTS==0)
        newValues.put("KARMA_PTS", 0);
   else
       newValues.put("KARMA_PTS", KARMA_PTS);
   if(NOTES.equals(null))
        newValues.put("NOTES", "");
   else
       newValues.put("NOTES", NOTES);

    // Insert the row into your table
    db.insert("MEMBERSHIP", null, newValues);
    Toast.makeText(context, "Member Is Successfully Saved", Toast.LENGTH_LONG).show();
}

public int countMembers()
    {
        int count = 0;
        String query = "select * from MEMBERSHIP";
        Cursor cursor = db.rawQuery(query, null);
        if(cursor.getCount()<1) // title Not Exist
        {
            cursor.close();
            return 0;
        }
        while(cursor.moveToNext())
        {
            count++;
        }
        cursor.close();
        return count;
    }

MemberAddScreen

MembershipAdapter=new MembershipAdapter(this);
    MembershipAdapter=MembershipAdapter.open();

    Intent intent = getIntent();
    final String userName = intent.getExtras().getString("username");

    buttonMemberAdd = (Button)findViewById(R.id.buttonMemberAdd);
        buttonMemberAdd.setOnClickListener(new View.OnClickListener() {
          public void onClick(View arg0) {
              String firstName = inputFirstName.getText().toString();
              String lastName = inputLastName.getText().toString();
              String birthday = inputBirthday.getText().toString();
              String memberId = inputMemberId.getText().toString();
              String email = inputEmail.getText().toString();
              String notes = inputNotes.getText().toString();

              MembershipAdapter.insertEntry(firstName, lastName, birthday, memberId, email, 0, 0, notes);
              System.out.println("Count Members: "+MembershipAdapter.countMembers());
              System.out.println(MembershipAdapter.getAll()[MembershipAdapter.countMembers()-1][0]);
              Toast.makeText(MemberAddScreen.this, "Member Successfully Added", Toast.LENGTH_LONG).show();

                Intent i=new Intent(MemberAddScreen.this,ManageMemberScreen.class);
                i.putExtra("username",userName);
                startActivity(i);
          }
      });

单击buttonMemberAdd后,我的日志显示表中的行数仍为零。

05-17 01:56:55.660: I/System.out(2995): Count Members: 0

0 个答案:

没有答案