0

m trying to retrive data from the databse but logcat shows msg "no such table " everything looks good but i don't understand why this problem happens.any one plese help me m new in android.

this belongs from main activity:

public class AllContact extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allcontactlayout_main);

      TextView  text=(TextView)findViewById(R.id.textView);
       TextView text2=(TextView)findViewById(R.id.textView2);

        ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());

       Cursor get= onbOfContactDatabase.showData();
    }
}

//when i call showData() logcat show msg "no such table"

this belongs from databse:

    public class ContactDatabase extends SQLiteOpenHelper {
        SQLiteDatabase db;
        public static final String DATABASE_NAME="totalContact.db";
        public static final  String TABLE_NAME="contact";
        public static final  String NAME="name";
        public static final  String PHONE="phone";

        public ContactDatabase(Context context) {
            super(context, DATABASE_NAME, null, 1);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL("craete table contact" +
                        "(id integer primary key autoincrement, name text, phone text)");
            }catch(android.database.SQLException e){
                    System.out.println("table create nhi ho rha");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS contact");
            onCreate(db);
        }

   public void insertContact(String nam,String mob){

            db=this.getWritableDatabase();
            ContentValues contentValues=new ContentValues();

            contentValues.put(NAME,nam);
            contentValues.put(PHONE,mob);

            db.insert(TABLE_NAME, null, contentValues);
            db.close();
        }

      public Cursor showData(){

            db=this.getWritableDatabase();
            Cursor res =  db.rawQuery("SELECT  * FROM contact", null);
            return res;

        }
    }
  • 2
    Your SQL syntax is incorrect. Change `craete table contact` to `create table contact` – Clive Seebregts Jun 09 '16 at 08:26
  • @CliveSeebregts thanks for this help but ne problem is occure log cat shows ' 06-09 13:40:49.299 30223-30550/? E/bf: Null reference found ,06-09 13:40:49.313 30223-30550/? E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 4 columns – Pushpam Kumar Jun 09 '16 at 08:34
  • You should never swallow an exception. – CL. Jun 09 '16 at 08:39
  • Add space: db.execSQL("create table contact " + "(id integer primary key autoincrement, name text, phone text)"); – Saeed Sharman Jun 09 '16 at 09:05

0 Answers0