每当我按下按钮将数据插入表格时我的应用程序崩溃。
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;
}
}
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);
}
}
}
答案 0 :(得分:3)
只需添加空格。
String CreateGameSql="Create Table "+Table_Name+" ( "+
KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT , "+
KEY_ENGLISH+" TEXT , "
+KEY_HEBREW+" TEXT )";
答案 1 :(得分:0)
我看到的一件事是你在这里没有空白区域:
"Create Table"+Table_Name
必须是:
String CreateGameSql="Create Table "+Table_Name+"("+
KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
KEY_ENGLISH+"TEXT, "
+KEY_HEBREW+"TEXT)";
除Create table
之后和KEY_ID
之后,您的创建字符串是正确的。在SQLite中,每个值都必须分开。