-1

while creating table and inserting values into database getting as nosuch table name. no such table exits.please find the logcat image attached. so how to insert values into the table inthe sqlite database.

![enter image description here][1]

  public class DataAdapter {

           DataBase database;
           SQLiteDatabase db;      

    public DataAdapter(Context context) {
        // TODO Auto-generated constructor stub
        database =new DataBase(context);    
    }

    public long insertData(String name ,String address){    

        SQLiteDatabase db= database.getWritableDatabase();  
        ContentValues values = new ContentValues();
        values.put(DataBase.NAME, name);
        values.put(DataBase.ADDRESS, address);
        return db.insert(DataBase.TABLENAME, null,values);
        }

    public static class DataBase extends SQLiteOpenHelper{

        public static final String DATABASE="Database.db";
        public static final String TABLENAME="tablename";   
        public static final int VERSION = 7;
        public static final String NAME="name"; 
        public static final String ADDRESS="address";
        public static final String ID="_id";

         public static final String CREATE_TABLE = "create table " + TABLENAME + "("+ 
     ID + " INTEGER PRIMARY KEY ," + NAME + " VARCHAR(255) ," + ADDRESS + "  VARCHAR(255) );";
        private Context context;

        public DataBase(Context context) {
            super(context, DATABASE, null, VERSION);        
            this.context =context;
            // TODO Auto-generated constructor stub     
            Toast.makeText(context, "onDataBase",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_TABLE);

//      try {
//          Toast.makeText(context, "oncreate",Toast.LENGTH_SHORT).show();
//      
//      } catch (SQLException e) {
//          // TODO: handle exception
//      }       


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            try {
                Toast.makeText(context, "oncUpgrade",Toast.LENGTH_SHORT).show();
                db.execSQL("DROP TABLE IF EXITS "+ TABLENAME);
                onCreate(db);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }   



    }

  [1]: https://i.stack.imgur.com/yWOtZ.jpg
arunk
  • 185
  • 2
  • 4
  • 14

1 Answers1

4

You have an SQL syntax error in onCreate() and you're ignoring the exception. Since onCreate() returns normally, the parent SQLiteOpenHelper thinks everything is all right.

Specifically, you need whitespace between ID and INTEGER PRIMARY KEY. And log your exceptions when you decide to catch them yourself. In onCreate() case, it's better to just let exceptions bubble up.

After updating the onCreate(), remove the old database file (uninstall, clear data, whatever) or bump up the database version in code so that the database gets recreated.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • public static final String CREATE_TABLE = "create table " + TABLENAME + "("+ ID + " INTEGER PRIMARY KEY ," + NAME + " VARCHAR(255) ," + ADDRESS + " VARCHAR(255) );"; private Context context; public DataBase(Context context) { super(context, DATABASE, null, VERSION); this.context =context; // TODO Auto-generated constructor stub Toast.makeText(context, "onDataBase",Toast.LENGTH_SHORT).show(); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(CREATE_TABLE); } – arunk Jan 21 '14 at 12:06
  • database file is located at data/data/com.mypackage/databases – arunk Jan 21 '14 at 12:25
  • how to check the values in the file. – arunk Jan 21 '14 at 12:25
  • You can pull the database file and examine it on your computer with your favorite sqlite client. http://stackoverflow.com/questions/2530548/browse-data-in-android-sqlite-database – laalto Jan 21 '14 at 12:33