Search code examples
androidsqliteandroid-database

Android database does not create columns in the table


I have a database with two tables that I want to work with. I have no problem with the first table, but when I have a database with two tables that I want, I have no problem when working with the first table, but when I insert the data in the second table, I get the "table has no clumn named" error.

This is my databaseHelper :

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public static final String TABLE_NAME = "bosors";
    public static final String _ID = "_id";
    public static final String index = "nindex";
 
  
  
    public static final String DATE = "date";
    
    
    
    static final String DB_NAME = "brs.DB";
    
    static final int DB_VERSION = 1;
    
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + index + " TEXT NOT NULL);";
    
     
    
    private static final String CREATE_TABLE2 = "create table " + "t67675656072510693" + "(" + _ID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " TEXT NOT NULL);";
    
    
    @Override
    public void onCreate(SQLiteDatabase p1)
    {p1.execSQL(CREATE_TABLE);
    
    p1.execSQL(CREATE_TABLE2);
    
    // TODO: Implement this method
    }

    @Override
    public void onUpgrade(SQLiteDatabase p1, int p2, int p3)
    {p1.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        p1.execSQL("DROP TABLE IF EXISTS " + "t67675656072510693");

    onCreate(p1);
        // TODO: Implement this method
    }


  
    
    
    
    
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        
}

}

This is my insert:

public void insert(String data) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(DatabaseHelper.index, data);
        database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
    }

From here I send the data to the insert method:

if(token[i].equals(nmdName.toString())){
    dbm.insertH("t"+nmdIndex,date);}

And when sending data to the column, I get this error:

**** (1) table t67675656072510693 has no column named date in "INSERT INTO t67675656072510693(date) VALUES (?)"****

I tried to introduce a separate DatabaseHelper and DatabaseManager , but I encountered the same problem again.


Solution

    • Verify if the create sql is executed in the onCreate method without any logged errors. It might fail if the table has already been created. In such cases, you need to either delete the table or execute an update table SQL statement.

    • You can use Android Studio or another tool to open the database and confirm the actual creation of the table.

    • Uninstall the application or clear the app data.