0

I'm trying to create a table named by the user, but everytime my app crashes... I'm new in android coding so, if you have any suggestion you're welcome.

This is the part of maine activity:

floatingMaine.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                final Dialog dialog=new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.dialog_maine);
                dialog.setTitle("Nome del formulario?");
                final TextView text = (TextView) dialog.findViewById(R.id.editTextDialog);
                ImageButton imageButton=(ImageButton)dialog.findViewById(R.id.dialogButton);
                imageButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //NON FUNZIONA L'INSERISCI TAVOLA
                        mydb.insertFormulario(text.toString());
                        Toast.makeText(MainActivity.this, "Formulario salvato", Toast.LENGTH_LONG).show();
                        ArrayList array_list=mydb.getAllFormulari();
                        ArrayAdapter arrayAdapter=new ArrayAdapter(MainActivity.this,R.layout.basic_adapter,R.id.textViewAdapter,array_list);
                        obj.setAdapter(arrayAdapter);
                        arrayAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });

This is my database, the problem is with insertFormulario:

public class DBHelper extends SQLiteOpenHelper{

    public static final String DATABASE_NAME = "FormulaeDB";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean insertFormulario (String formulario){
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("CREATE TABLE " + formulario /*+ "(Formule text)"*/);
        return true;//doesn't work
    }

    public boolean deleteFormulario (String formulario){
        SQLiteDatabase db =this.getReadableDatabase();
        db.execSQL("DROP TABLE IF EXIST"+formulario);
        return true;
    }

    public ArrayList<String> getAllFormulari(){

        ArrayList<String> array_list = new ArrayList<String>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name!='android_metadata' order by name", null);

        if (c.moveToFirst()) {
            while ( !c.isAfterLast() ) {
                array_list.add( c.getString( c.getColumnIndex("name")) );
                c.moveToNext();
            }
        }

        return array_list;
    }

}

Here is my error code:

06-13 16:14:36.858 2658-2658/com.example.eugenioanselmino.formulae E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.eugenioanselmino.formulae, PID: 2658
                                                                                     android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: CREATE TABLE android.support.v7.widget.AppCompatEditText{b673f86 VFED..CL. .F...... 29,16-560,134 #7f0d008b app:id/editTextDialog}
                                                                                         at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                         at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                         at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                         at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                         at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                         at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                         at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
                                                                                         at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
                                                                                         at com.example.eugenioanselmino.formulae.DBHelper.insertFormulario(DBHelper.java:30)
                                                                                         at com.example.eugenioanselmino.formulae.MainActivity$1$1.onClick(MainActivity.java:52)
                                                                                         at android.view.View.performClick(View.java:5637)
                                                                                         at android.view.View$PerformClick.run(View.java:22429)
                                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2
  • You're right... I totally forgot it.. anyway it's my first time, pe patiet... Thank you anyway:-) Commented Jun 14, 2017 at 17:27
  • i am glad that i could help , keep it up , see u around Commented Jun 14, 2017 at 17:28

1 Answer 1

1

You should use this code:

mydb.insertFormulario(text.getText().toString());

instead of this:

mydb.insertFormulario(text.toString());
Sign up to request clarification or add additional context in comments.

1 Comment

I corrected the mistake you found, but it still not working... Any other suggestion? Thank you anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.