2017-01-16 3 views
-4

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

log
public 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. 
+0

Können Sie die Fehlerprotokolle enthalten? Ziemlich schwer zu debuggen ohne sie – Isaac

+1

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

+0

'Einfügen in Fragen Werte (' WERTE * * sollte ** vorangestellt werden die Felder Liste –

Antwort

0

Die Fehlermeldung ziemlich erklärt, was los ist:

android.database.sqlite.SQLiteException: Tabelle Fragen hat 6 Spalten, aber 5 Werte wurden zur Verfügung gestellt (Code 1):, während der Kompilierung: Einfügen in Fragen Werte ('x', 'x', 'x', 'x', 'x ')

versuchen, etwas in dieser Richtung:

String sql = "INSERT INTO questions (question, optionone, optiontwo, optionthree, optionfour) VALUES (?, ?, ?, ?, ?)"; 
SQLiteStatement statement = db.compileStatement(sql); 

String q = e1.getText().toString(); 
String q1 = e2.getText().toString(); 
String q2 = e3.getText().toString(); 
String q3 = e4.getText().toString(); 
String q4 = e5.getText().toString(); 

statement.bindString(1, q); // These match to the five question marks in the sql string 
statement.bindString(2, q1); 
statement.bindString(3, q2); 
statement.bindString(4, q3); 
statement.bindString(5, q4); 

long rowId = statement.executeInsert();  
+0

Aber ich muss es automatisch zu erhöhen, für das, was ist die Notwendigkeit zu geben? –

+0

Bearbeitet, um die Antwort weniger generische machen – Isaac

+0

Danke getan .. –

1

Sie wollen mit 6 Spalten 5 Werte in eine Tabelle füllen. Sie möchten den Primärschlüssel sno nicht einfügen.

So etwas sollte funktionieren:

sql.execSQL("insert into questions (question,optionone,optiontwo,optionthree,optionfour) values ('"+q+"','"+o1+"','"+o2+"','"+o3+"','"+o4+"')"); 
+0

Danke fertig .... –

Verwandte Themen