Nirco Nirco - 1 month ago 18
SQL Question

Android database insert crash

Whenever I press the button to insert data to the table my app crashes.

The Database handler



public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="TranslateGame.db";
private static final String Table_Name="TranslateGame";
private static final String KEY_ID = "id";
private static final String KEY_ENGLISH="EnglishList";
private static final String KEY_HEBREW="HebrewList";

public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String CreateGameSql="Create Table"+Table_Name+"("+
KEY_ID+"INTEGER PRIMARY KEY AUTOINCREMENT, "+
KEY_ENGLISH+"TEXT, "
+KEY_HEBREW+"TEXT)";
db.execSQL(CreateGameSql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
onCreate(db);
}

public void AddWord(TranslateTable translate) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ENGLISH,translate.GetEnglish());
values.put(KEY_HEBREW,translate.GetHebrew());
db.insert(Table_Name, null, values);
db.close();
}

public String[] GetTranslation(String Word)
{
String[] Arr={KEY_ID,KEY_ENGLISH,KEY_HEBREW};
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(DATABASE_NAME,Arr,null,null,null,null,null);
if (cursor!=null)
{
cursor.moveToFirst();
}
return Arr;
}

public List<TranslateTableGetDataBaseInfo()
{
List<TranslateTabledbInfo=new ArrayList<>();
String SelectQuery="SELECT * FROM"+Table_Name;
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.rawQuery(SelectQuery,null);
if(cursor.moveToFirst()){
do {
TranslateTable NewTable=new TranslateTable();
NewTable.SetID(Integer.parseInt(cursor.getString(0)));
NewTable.SetEnglish(cursor.getString(1));
NewTable.SetHebrew(cursor.getString(2));
dbInfo.add(NewTable);
}while (cursor.moveToNext());
}
return dbInfo;
}
}


Insert Data Activity



public class MainPage extends AppCompatActivity {
MyDBHandler Newdb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
Newdb=new MyDBHandler(this);
}

public void AddWord(View view) {
EditText OriginalWord=(EditText)findViewById(R.id.Original);
EditText TranslationWord=(EditText)findViewById(R.id.Translation);
if(OriginalWord.getText().toString().isEmpty()||TranslationWord.getText().toString().isEmpty())
{
Toast.makeText(this, "Please fill the empty texts", Toast.LENGTH_SHORT).show();
}
else
{
TranslateTable NewTable=new TranslateTable(OriginalWord.getText().toString(),
TranslationWord.getText().toString());
Newdb.AddWord(NewTable);
}
}
}

Answer

One thing I see is that you donĀ“t have a white space here:

"Create Table"+Table_Name

It must be:

String CreateGameSql="Create Table "+Table_Name+"("+
                KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                KEY_ENGLISH+"TEXT, "
                +KEY_HEBREW+"TEXT)";

Your create string is correct except after Create table and after KEY_ID. In SQLite, every value must be separated.