在数据库中插入错误的值

时间:2014-07-17 06:23:52

标签: android sql database

我创建了两个从用户获取数据的活动,第一个活动的数据被传送到第二个活动,然后第一个活动和第二个活动的数据一起存储在数据库中。第二个活动的数据在db中是完美的,但第一个活动的数据是错误的。这是我的代码。

dropdown = (Spinner)findViewById(R.id.spinner1);

        String course=dropdown.getSelectedItem().toString();
        edit_name=(EditText)findViewById(R.id.name);
        edit_email=(EditText)findViewById(R.id.email);
        edit_pass=(EditText)findViewById(R.id.editText1);
        edit_conpass=(EditText)findViewById(R.id.editText2);
        String name1=edit_name.getText().toString();
        String email1=edit_email.getText().toString();
        String pas=edit_pass.getText().toString();
        String conpass=edit_conpass.getText().toString();
        if(pas.equals(conpass))
        {
            edit_name.setText(" ");
            edit_email.setText(" ");
            edit_pass.setText(" ");
            edit_conpass.setText(" ");
        Intent fin = new Intent(getApplicationContext(),
                Form.class);
        fin.putExtra(TAG_NAME, name1);
        fin.putExtra(TAG_EMAIL, email1);
        fin.putExtra(TAG_PASS, pas);
        fin.putExtra(TAG_COURSE, course);

        startActivity(fin);

我的第二项活动是。

 public class Form extends Activity {

protected static final int ACTIVITY_SELECT_IMAGE = 0;
private static final String TAG_NAME = null;
private static final String TAG_PASS = null;
private static final String TAG_EMAIL = null;
private static final String TAG_COURSE = null;
SQLiteDatabase db=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);
    getIntent();
     db= openOrCreateDatabase("Angel", MODE_PRIVATE, null);
       db.execSQL("create table if not exists login1(name varchar,email varchar,age integer,course varchar,phone integer,password varchar)");


    Button button1=(Button) findViewById(R.id.button1);
    Button button2=(Button) findViewById(R.id.button2);
    NumberPicker numpic=(NumberPicker) findViewById(R.id.numberPicker1);
    numpic.setMaxValue(99);
    numpic.setMinValue(0);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 
        }
    });


    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent fin=getIntent();
            EditText editText1=(EditText) findViewById(R.id.editText1);
            String username=fin.getStringExtra(TAG_NAME);
            String email=fin.getStringExtra(TAG_EMAIL);
            String password=fin.getStringExtra(TAG_PASS);

             String course=fin.getStringExtra(TAG_COURSE);
             String no=editText1.getText().toString();
                long no2=Long.parseLong(no);  
                NumberPicker numpic=(NumberPicker) findViewById(R.id.numberPicker1);
                int a=numpic.getValue();
            db.execSQL("insert into login1 values('"+username+"','"+email+"','"+a+"','"+course+"','"+no2+"','"+password+"')");
            db.close();
            Toast.makeText(Form.this, "values inserted successfully.", Toast.LENGTH_LONG).show();
            Intent in = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(in);
        }
    });
}
}

我现在没有存储图像。

2 个答案:

答案 0 :(得分:1)

您需要初始化Strings,如此...

private static final String TAG_NAME = "TAG";
private static final String TAG_PASS = "PASS";
private static final String TAG_EMAIL = "EMAIL";
private static final String TAG_COURSE = "COURSE";

这些是您的唯一标识符字符串,它不是空的。

更新

另一种方式是

 Intent fin = new Intent(getApplicationContext(),
            Form.class);
    fin.putExtra("TAG", name1);
    fin.putExtra("EMAIL", email1);
    fin.putExtra("PASS", pas);
    fin.putExtra("COURSE", course);

并且像

一样
   Intent fin=getIntent().getExtras();
   String username=fin.getStringExtra("TAG");
   String email=fin.getStringExtra("EMAIL");
   String password=fin.getStringExtra("PASS");
   String course=fin.getStringExtra("COURSE");

答案 1 :(得分:0)

试试这个...........

public class Form extends Activity 
{
protected static final int ACTIVITY_SELECT_IMAGE = 0;
SQLiteDatabase db=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);
    getIntent();
     db= openOrCreateDatabase("Angel", MODE_PRIVATE, null);
       db.execSQL("create table if not exists login1(name varchar,email varchar,age integer,course varchar,phone integer,password varchar)");


    Button button1=(Button) findViewById(R.id.button1);
    Button button2=(Button) findViewById(R.id.button2);
    NumberPicker numpic=(NumberPicker) findViewById(R.id.numberPicker1);
    numpic.setMaxValue(99);
    numpic.setMinValue(0);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 
        }
    });


    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent fin=getIntent();
            EditText editText1=(EditText) findViewById(R.id.editText1);
            String username=fin.getStringExtra("TAG_NAME");
            String email=fin.getStringExtra("TAG_EMAIL");
            String password=fin.getStringExtra("TAG_PASS");
             String course=fin.getStringExtra("TAG_COURSE");
             String no=editText1.getText().toString();
                long no2=Long.parseLong(no);  
                NumberPicker numpic=(NumberPicker) findViewById(R.id.numberPicker1);
                int a=numpic.getValue();
            db.execSQL("insert into login1 values('"+username+"','"+email+"','"+a+"','"+course+"','"+no2+"','"+password+"')");
            db.close();
            Toast.makeText(Form.this, "values inserted successfully.", Toast.LENGTH_LONG).show();
            Intent in = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(in);
        }
    });
}
}