0

I am actually creating a android application which creates dynamic forms, in which the column headers for each table is dynamically set by the user itself, my request is that my application should enable user to enter column header with single quotes like- name's,no's etc.I already tried this code and it helps in inserting single quote String to database

public void CreateDynamicTables(String Table_Name, List<String> arr) {
        SQLiteDatabase dbs;

        dbs = this.getWritableDatabase();
        dbs.execSQL("DROP TABLE IF EXISTS " + Table_Name);

        String query = "CREATE TABLE " + Table_Name + "(" + CID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, ";

        for (int i = 0; i < arr.size(); i++) {

            if(i==arr.size()-1){
                query+="`"+arr.get(i)+"`"+ " TEXT);";
            }else{
                query+="`"+arr.get(i)+"`"+ " TEXT,";
            }
        }

        System.out.println(" Final Query  :: " + query);

        dbs.execSQL(query);
        //ColumnNames(Table_Name);
        System.out.println(" successfully created table from FILE  :: " + query);
        dbs = this.getWritableDatabase();
        dbs.close();
    }

Although i am having

Three different column header

I am still having trouble while altering the table am getting the below exception

android.database.sqlite.SQLiteException: duplicate column name: 'single's' (code 1): , while compiling: ALTER TABLE smp07 ADD COLUMN `'single's'` TEXT

columnNames for ALTERTABLE :['single's', 'triple', 'double']

code for altering

 if(cols.size()<arr.size()){
            db=this.getWritableDatabase();
            if(db.isOpen()){System.out.println("Open Database Confirmed...!!");}
            System.out.println(" in add column in table");
            for (int i = 0; i < arr.size(); i++) {
                if(!cols.contains(arr.get(i))){
                    System.out.println("COlumn names are:"+arr.get(i).toString());
                    System.out.println(" ADD column Alter table");
                    db.execSQL("ALTER TABLE "+ Table_name +" ADD COLUMN "+"`"+arr.get(i)+"`"+" TEXT");
                }
            }
        }

        db.close();}

PS: It happens in case where different column headers are inserted

If there a another safe way to insert single quote statement in sqllite plse help

e4c5
  • 52,766
  • 11
  • 101
  • 134

1 Answers1

0

In SQL, identifiers (such as column/table names) can be quoted with double quotes. In this case, any double quotes in the actual name must be escaped by doubling them.

For compatibility with other DBs, SQLite also allows quoting identifiers with single quotes, backticks, or square brackets.


Anyway, the error message "duplicate column name" has nothing to do with quoting; it indicates that you have tried to add the same column a second time.

CL.
  • 173,858
  • 17
  • 217
  • 259