1

I am working on an android app and I have a problem i can't solve. I hope you guys can help me.

I get this error:

android.database.sqlite.SQLiteException: table FBFriends has no column named id (code 1): , while compiling: INSERT INTO FBFriends(id,name) VALUES (?,?)

Like you all know, the error is that it can't find the table. But I make the table.

  public DatabaseManager(Context context) {
            //referentie naar cursus
            super(context, DATABASE_NAME, null, 1);
        }

       @Override
        public void onCreate(SQLiteDatabase db) {
           //SQL for creating a new table

           Log.d(TAG, "Tabel gemaakd");
            String CREATE_DATABASE_TABLE ="CREATE TABLE Friends (id VARCHAR(255) PRIMARY KEY not null , name VARCHAR(255) )";
            db.execSQL(CREATE_DATABASE_TABLE);
       }
   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older database if existed
        db.execSQL("DROP TABLE IF EXISTS Friends");
        Log.d(TAG, "Tabel verwijdert");
        // create new table
        this.onCreate(db);
    }

And this is the function for adding data to the database.

private static final String TABLE_FBFriends = "Friends";    
private static final String KEY_ID = "id";
private static final String KEY_Name = "name";


    private static final String[] COLUMNS = {KEY_ID,KEY_Name};
    public boolean addPersons(ArrayList<Person> listOfPersons)
    {
        Log.d(TAG, "add");
        //get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        Log.d(TAG, "toevoegen add");
        //create ContentValues to add key "column"/value
        for (Person p:listOfPersons) {
            Log.d(TAG, "FOREACH");
            ContentValues values = new ContentValues();
                values.put(KEY_ID, p.getId());
            Log.d(TAG, "first VALUE");
            values.put(KEY_Name, p.getName()); // get title
            Log.d(TAG, "second VALUE");
            Log.d(TAG, "add " + values.toString());
            try{
                // insert orThrow an exeption
                db.insertOrThrow(TABLE_FBFriends, null, values);
//key/value -> keys = column names/ values = column values
                }
                catch (SQLException e) {
                    Log.d(TAG,  e.toString());
                   return false;
                    }
        }
            db.close();
            return true;
        }

Thank you guys!

Regards, Stijn

StijnB
  • 79
  • 9
  • 2
    Your table is named Friends. But the error says FBFriends. Do you have an old table in your DB? – juergen d Apr 13 '16 at 15:00
  • could you add your complete logcat? – Miguel Benitez Apr 13 '16 at 15:05
  • Not that I know, I have changed it to this. (like suggested below) db.insertOrThrow("Friends", null, values); And I get this error now. android.database.sqlite.SQLiteException: no such table: Friends (code 1): , while compiling: INSERT INTO Friends(id,name) VALUES (?,?) – StijnB Apr 13 '16 at 15:12
  • 1
    Upgrade the database version in the constructor super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION); or using setting clean the data of your app. – Miguel Benitez Apr 13 '16 at 15:16
  • Mine logcad: add toevoegen add FOREACH first VALUE second VALUE add id=48648464654 name=John Test android.database.sqlite.SQLiteException: no such table: Friends (code 1): , while compiling: INSERT INTO Friends(id,name) VALUES (?,?) – StijnB Apr 13 '16 at 15:17
  • 1
    onCreate is only call the first time you create your database, and onUpgrade is call every time you change the database version. Try changing the database version or cleaning the data of your app – Miguel Benitez Apr 13 '16 at 15:20
  • This was the solution, thank you very mutch! I changed the version to "2" because de Constants. doens't work.(but that is the solution) If I add the Constants., I need to add an import and I get an error at that moment(DATABASE_NAME and DaTABASE_VERSION. (and I need to make SyncStateContract.Constants. of it, i don't get an other import) Will the Constants make also sure I don't get like 1000000 databases? (eveytime i open the app). – StijnB Apr 13 '16 at 15:30

1 Answers1

1

it's because you still have your old table and dont have the new table. first to see, dont change the name table directly in your onCreate database helper after you create one. because it's only create table on the first time execute. if you want to change your name table, just create a function to use alter table query and change the old name table with the new one.

itsa04g9
  • 348
  • 1
  • 10