未在android中插入数据库的值

时间:2012-07-07 15:08:46

标签: java android

下面给出的是我的代码。

我有两个课程:Dbhelper.javaRssActivity.java

基本上我已经创建了一个students表,我的代码正在创建表,但是没有将值插入到表中。

有两个文本框:一个用于输入名称,另一个用于输入roll no

我希望roll no位于00113202709到12013203709范围内时应插入值

我希望这些值应来自2个文本框editText1editText2,并插入我的数据库表中。

感谢您的帮助。

Dbhelper.java:

public class Dbhelper extends SQLiteOpenHelper {

//private static final String TAG = Dbhelper.class.getSimpleName();

public static final String db_name = "member.db";

public static final int db_version = 1;

public final String TABLE = "students";

public static final String C_ID = BaseColumns._ID;

public static final String C_Name = "SName";

public static final String C_ENo = "Enroll No";

private Context context;


public Dbhelper(Context context) {

    super(context, db_name, null, db_version);
    this.context=context;

}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = String.format(
            "create table %s (%s TEXT primary key, %s TEXT, %s TEXT )",
            TABLE, C_ID, C_Name, C_ENo);

db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("drop table if exists " + TABLE);
    this.onCreate(db);

}
}

RssActivity.java:

public class RssActivity extends Activity implements OnClickListener {


TextView Title;
EditText Name, ENo;
Button but;
public String n1 = "001";
public String n2 = "120";
public String s1, s2, s3;
Dbhelper obj;
SQLiteDatabase db;
ContentValues val;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Title = (TextView) findViewById(R.id.textView1);
    Name = (EditText) findViewById(R.id.editText2);
    ENo = (EditText) findViewById(R.id.editText1);
    but = (Button) findViewById(R.id.button1);
    but.setOnClickListener(this);
    obj=new Dbhelper(RssActivity.this);
    db=obj.getWritableDatabase();
}

public void onClick(View v) {
    String name = Name.getText().toString();
    String enr = ENo.getText().toString();

    try {

        if (name.equals("")) {
            Name.setError(getText(R.string.Empty));
            Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

        } else if ((ENo.getText().toString().length() == 0)) {
            ENo.setError(getText(R.string.Empty));
            Toast.makeText(this, R.string.Empty, Toast.LENGTH_LONG).show();

        } else if ((ENo.getText().toString().length() < 11))

        {
            ENo.setError(getText(R.string.Invalid));
            Toast.makeText(this, R.string.Invalid, Toast.LENGTH_LONG)
                    .show();

        }

        else

        {
            s1 = "000";
            s2 = "00000000";
            throw new Exception();
        }

    }

    catch (Exception e) {
        e.printStackTrace();

        s1 = enr.substring(0, 3);
        s2 = enr.substring(3, 11);
        s3 = "13202709";
        int r1 = s1.compareTo(n1);
        int r2 = s1.compareTo(n2);

        if (s2.equals(s3)) {
            if (r1 == 0 || r2 == 0) {

                Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                        .show();
                val=new ContentValues();
                val.clear();
                val.put(obj.C_ID, s1);
                val.put(obj.C_Name, name);
                val.put(obj.C_ENo,enr);
                db.insert(obj.TABLE, null, val);
                db.close();




            } else if (r1 < 0 || r2 > 0) {
                Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG)
                        .show();
            } else if (r1 > 0 || r2 < 0) {
                Toast.makeText(this, R.string.Success, Toast.LENGTH_LONG)
                        .show();
            }
        }

        else

            Toast.makeText(this, R.string.Fail, Toast.LENGTH_LONG).show();

    }
    db.close();
    obj.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.item1:
        startActivity(new Intent(this, Welcomescreen.class));
        this.finish();
        break;

    case R.id.item2:
        this.finish();
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, Welcomescreen.class);
    // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    this.finish();
}

}

0 个答案:

没有答案