无法在Android应用程序中的sqlite中存储数据

时间:2013-12-07 16:10:47

标签: java sqlite

我无法在我的SQLite数据库中存储数据。在单击“提交”按钮时,应用程序未正确地向下移动。我已经完成了解决方案@ stackoverflow但没有得到我的解决方案。请帮忙。 获取条目的java类:

public class emergcon extends Activity implements OnClickListener{
    boolean diditwork;
    TextView tnam,tmail,tphone;
    EditText enam,email,ephone;
    Button sbtn,shom;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);

        tnam=(TextView)findViewById(R.id.txtName);
        tmail=(TextView)findViewById(R.id.txtEmail);
        tphone=(TextView)findViewById(R.id.txtPhone);

        enam=(EditText)findViewById(R.id.edName);
        email=(EditText)findViewById(R.id.edMail);
        ephone=(EditText)findViewById(R.id.edPhone);

        sbtn=(Button)findViewById(R.id.dbtn);
        shom=(Button)findViewById(R.id.show);
        sbtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch(arg0.getId())
        {
        case R.id.dbtn:

            try
            {
                String name=enam.getText().toString();
                String mail=email.getText().toString();
                String phone=ephone.getText().toString();


                datab d=new datab(emergcon.this);
                d.open();
                d.createEntry(name,mail,phone);
                d.close();

            }catch (Exception e) {
                // TODO: handle exception

                diditwork=false;
            }finally{
                if(diditwork)   
                {
                    Toast t=Toast.makeText(emergcon.this, "Added Successfully!", Toast.LENGTH_LONG);
            t.show();
                }
                else
                {
                    Toast t1=Toast.makeText(emergcon.this, "ERROR! TRY AGAIN", Toast.LENGTH_LONG);
            t1.show();

                }
            }
                break;
        case R.id.show:
            Intent i= new Intent("com.anj.techlady.openview");
            startActivity(i);
            break;
        }
    }

进行数据库操作的类:

public class datab 
{

    public static final String KEY_NAME="_name";
    public static final String KEY_MAIL="_email";
    public static final String KEY_PHONE="_phone";
    public static final String KEY_ID="_id";

    public static final String dbNAME="Emergency";
    public static final String dbTable="Contacts";
    public static final int dbVersion=1;

    private Contacts ourHelper;
    private static Context ourContext;
    private SQLiteDatabase ourDb;


    private static class Contacts extends SQLiteOpenHelper {


        public Contacts(Context context) {
            super(context, dbNAME, null, dbVersion);
            // TODO Auto-generated constructor stub
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE "+dbTable+" ("+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT NOT NULL, "
        +KEY_MAIL+" TEXT NOT NULL, "+KEY_PHONE+" INTEGER NOT NULL);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS "+dbTable);
        onCreate(db);
        }

    }
public datab()
{

}
    public datab(Context c)
    {
        ourContext=c;
    }
    public datab open() throws SQLException
    {
        ourHelper=new Contacts(ourContext);
        ourDb=ourHelper.getWritableDatabase();
    return this;
    }

    public void close()
    {
        ourHelper.close();
    }
    public long createEntry(String name, String mail, String phone) {
        // TODO Auto-generated method stub
        ContentValues cv=new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_MAIL, mail);
        cv.put(KEY_PHONE,phone);
        return ourDb.insert(dbTable, null,cv);

    }

0 个答案:

没有答案