0

When I try to insert data into my app I got error which say

no such table: student

I am creating table student but still I got this error.

Following is my code:

Main activity

public class MainActivity extends AppCompatActivity {
EditText editText_ID, editText_name, editText_city;
Button button_insert, button_all;
MyDBHandler mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText_ID = (EditText) findViewById(R.id.editText_ID);
    editText_name = (EditText) findViewById(R.id.editText_name);
    editText_city = (EditText) findViewById(R.id.editText_city);

    button_insert = (Button) findViewById(R.id.button_insert);
    button_all = (Button) findViewById(R.id.button_all);

    mydb = new MyDBHandler(this);
    //insert();
   // getAllData();

    button_insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean inserted = mydb.addStudent(new Student(editText_ID.getText().toString(), editText_name.getText().toString(), editText_city.getText().toString()));


            if (inserted = true)
                Toast.makeText(MainActivity.this, "Data is inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Data is Not inserted", Toast.LENGTH_LONG).show();

            mydb.close();
        }
    });

}

}

Dbhandler

public class MyDBHandler extends SQLiteOpenHelper {
public static final String DB_NAME="Student";
public static final String KEY_ID="key_id";
public static final String KEY_NAME="key_name";
public static final String KEY_CITY="key_city";
public static final String TBL_NAME="student";

public static final int DB_VERSION=4;
private final Context context;


public MyDBHandler(Context context) {
    super(context, DB_NAME, null,DB_VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

 // String CREATE_TABLE="CREATE TABLE info(id INTEGER PRIMARY KEY,name TEXT,city TEXT)";
     db.execSQL("CREATE TABLE " + TBL_NAME + "(id INTEGER PRIMARY KEY,name TEXT,city TEXT)" );
//String CREATE_STUDENT_TABLE="CREATE TABLE " + TBL_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_CITY + " TEXT,"+")";

}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public boolean addStudent( Student student) {

    SQLiteDatabase db=getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID,student.getId());
    contentValues.put(KEY_NAME,student.getName());
    contentValues.put(KEY_CITY,student.getCity());

     int result= (int) db.insert(TBL_NAME,null,contentValues);

    if (result == -1)
    {
        return false;
    }
    else

        return true;
}

void searchStudent(int id){
    SQLiteDatabase db=getReadableDatabase();

    String search_query="SELECT * FROM TBL_NAME WHERE key_id="+id;
    try {
        Cursor cursor=db.rawQuery(search_query, null);
        if (cursor.getColumnCount()>0)
        {
            cursor.moveToFirst();

            Log.d("mytag",""+cursor.getString(0)+": "+cursor.getString(1)+" :"+cursor.getString(2));

        }
        else
        {
            Toast.makeText(context, "No records", Toast.LENGTH_SHORT).show();
        }


    }catch (Exception e){

        Log.d("mytag","MyDBHandler:SearchContact:  :"+e.getMessage() );
    }



}
}

Logcat

08-26 18:22:50.956 7780-7780/com.example.owner.sqlite E/SQLiteLog: (1) no such table: student
    08-26 18:22:50.958 7780-7780/com.example.owner.sqlite E/SQLiteDatabase: Error inserting key_name=kunal key_id=20 key_city=pune
android.database.sqlite.SQLiteException: no such table: student (code 1): , while compiling: INSERT INTO student(key_name,key_id,key_city) VALUES (?,?,?)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
    at com.example.owner.sqlite.MyDBHandler.addStudent(MyDBHandler.java:55)
    at com.example.owner.sqlite.MainActivity$1.onClick(MainActivity.java:35)
    at android.view.View.performClick(View.java:4785)
    at android.view.View$PerformClick.run(View.java:19884)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

I am struggling with this error form hours but I don't understand how to deal with this.

Please suggest me how to deal with this.

MBH
  • 16,271
  • 19
  • 99
  • 149
Kunal Nikam
  • 79
  • 1
  • 11

1 Answers1

0

If it tells there's no table, then there's no table :)

But since you got multiple SQL stataments in your onCreate() commented out I assume that ran the old code at least one previously. If so, the onCreate() is no longer called as it is only called when there's no database - but there is - old one. What you shall do to check that, is to simply remove old data of the app. Just go to app manager on the device and clear all its data - that will remove database as well and then your onCreate() will be called on next launch.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141