Ich versuche einfach, eine Quiz-App zu erstellen, für die ich die Daten in der Datenbank speichern werde. Der folgende Code funktioniert nicht, und die App stürztAndroid Sqlite Datenbank funktioniert nicht richtig
logpublic class QuestionFeedMainPage extends AppCompatActivity {
Button b1;
EditText e1,e2,e3,e4,e5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_feed_main_page);
b1 = (Button)findViewById(R.id.submitQF);
e1 = (EditText)findViewById(R.id.q);
e2 = (EditText)findViewById(R.id.o1);
e3 = (EditText)findViewById(R.id.o2);
e4 = (EditText)findViewById(R.id.o3);
e5 = (EditText)findViewById(R.id.o4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String q = e1.getText().toString();
String o1 = e2.getText().toString();
String o2 = e3.getText().toString();
String o3 = e4.getText().toString();
String o4 = e5.getText().toString();
if(q.equals("") || o1.equals("") || o2.equals("") || o3.equals("") || o4.equals("")){
Toast.makeText(QuestionFeedMainPage.this, "Please fill all the fields", Toast.LENGTH_SHORT).show();
}
else {
SQLiteDatabase sql = openOrCreateDatabase("multip",MODE_PRIVATE,null);
sql.execSQL("create table if not exists questions (sno INTEGER PRIMARY KEY AUTOINCREMENT,question varchar,optionone varchar,optiontwo varchar,optionthree varchar,optionfour varchar)");
String s4 = "select * from questions where question='"+q+"'";
Cursor cursor = sql.rawQuery(s4,null);
if(cursor.getCount()>0){
Toast.makeText(QuestionFeedMainPage.this, "This question already exist", Toast.LENGTH_SHORT).show();
}
else{
sql.execSQL("insert into questions values ('"+q+"','"+o1+"','"+o2+"','"+o3+"','"+o4+"')");
Toast.makeText(QuestionFeedMainPage.this, "Question Added", Toast.LENGTH_SHORT).show();
e1.setText("");
e2.setText("");
e3.setText("");
e4.setText("");
e5.setText("");
}
}
}
});
}
}
Fehler jedes Mal, wenn ich die Daten füttern:
D/ActivityThreadInjector: clearCachedDrawables.
D/OpenGLRenderer: endAllStagingAnimators on 0x55596f0a20 (RippleDrawable) with handle 0x55594260c0
V/BoostFramework: BoostFramework() : mPerf = [email protected]
E/SQLiteLog: (1) table questions has 6 columns but 5 values were supplied
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fridaygmail.saurabh.multip, PID: 25409
android.database.sqlite.SQLiteException: table questions has 6 columns but 5 values were supplied (code 1): , while compiling: insert into questions values ('x','x','x','x','x')
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.fridaygmail.saurabh.multip.QuestionFeedMainPage$1.onClick(QuestionFeedMainPage.java:48)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21177)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
I/Process: Sending signal. PID: 25409 SIG: 9
Application terminated.
Können Sie die Fehlerprotokolle enthalten? Ziemlich schwer zu debuggen ohne sie – Isaac
Wahrscheinlich wird nicht Ihr Problem lösen, aber Sie sollten auch einen Blick auf diese Antwort auf die Verhinderung von SQL-Injektion http://stackoverflow.com/a/41496437/1543839 – Isaac
'Einfügen in Fragen Werte (' WERTE * * sollte ** vorangestellt werden die Felder Liste –