我正在尝试创建一个由用户命名的表,但每次我的应用程序崩溃时... 我是Android编码的新手,所以,如果你有任何建议,欢迎你。
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();
}
});
这是我的数据库,问题在于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;
}
}
这是我的错误代码:
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)
答案 0 :(得分:1)
您应该使用以下代码:
mydb.insertFormulario(text.getText().toString());
而不是:
mydb.insertFormulario(text.toString());