mercredi 24 juin 2015

Why am I not able to fetch the code from the DataBase?

I'm unable to fetch the data although I'm able to add data. I have two questions here: What is the purpose of the primary key and how can I implement it? In my code, if I give the same values multiple times then it also gets accepted. //Check this statement

db.execSQL("create table mtable (_Id Integer  auto_increment, name Text Primary key , mail Text)");

Now,my 2nd major problem..

Why am I not able to fetch the data from the database ? My two java classes are as follows:

MainActivity.java

package com.apna.mydatabase;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity  implements OnClickListener {

    Button save,fetch,update,Delete;
    EditText edtname,edtmail,afetch;
    MySqlOpenHelper ab,db;
    SQLiteDatabase sql,sql1;


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

        edtname=(EditText) findViewById(R.id.editText1);
        edtmail=(EditText)findViewById(R.id.editText2);
        afetch=(EditText) findViewById(R.id.editText3);
    save=(Button) findViewById(R.id.button1);
    fetch=(Button) findViewById(R.id.button2);
    update=(Button) findViewById(R.id.button3);
Delete=(Button)findViewById(R.id.button4);
    save.setOnClickListener(this);
    fetch.setOnClickListener(this);
    ab=new MySqlOpenHelper(MainActivity.this);
    sql=ab.getWritableDatabase();

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.button1:
            ContentValues cv=new ContentValues();
            cv.put("name", edtname.getText().toString());
            cv.put("mail",edtmail.getText().toString());
            long result=sql.insert("mtable", "nullCo", cv);     
        if (result>0)
        {            
            Toast.makeText(MainActivity.this, "Saved at"+result, 5000).show();
            edtname.setText(" ");
            edtmail.setText(" ");
        }
        else 
        {
            Toast.makeText(MainActivity.this, "Not Saved"+result, 5000).show();
        }
        break;

        case R.id.button2:
            db= new  MySqlOpenHelper(MainActivity.this);
            sql1=db.getReadableDatabase();
            String a=afetch.getText().toString();

            Cursor c=sql1.rawQuery("Select * from mtable where _id ="+a,null );
            String str=null,str1=null;
            if(c.getCount()>0)
            {
            while(c.moveToNext())
            {
                str=c.getString(c.getColumnIndex("name"));
                str1=c.getString(c.getColumnIndex("mail")); 
            }

            edtname.setText(str);
            edtmail.setText(str1);          
            }
    else
            {
            Toast.makeText(MainActivity.this,"Invalid record",5000 ).show();
            }


}

    }
    }

MySqlOpenHelper.java

package com.apna.mydatabase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqlOpenHelper extends SQLiteOpenHelper {

    public MySqlOpenHelper(Context context)
    {
        super(context, "Mynew", null,1);
            }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table mtable (_Id Integer  auto_increment, name Text Primary key , mail Text)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

It would be my pleasure to get the required results.

Aucun commentaire:

Enregistrer un commentaire