我只是想创建一个测验应用程序,我将数据存储在数据库中。以下代码无效,每次我提供数据时应用程序崩溃
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("");
}
}
}
});
}
}
错误日志:
D/ActivityThreadInjector: clearCachedDrawables.
D/OpenGLRenderer: endAllStagingAnimators on 0x55596f0a20 (RippleDrawable) with handle 0x55594260c0
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@352760c
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 :(得分:2)
您希望将5个值填充到包含6列的表中。您不想插入主键sno。
这样的事情应该有效:
sql.execSQL("insert into questions (question,optionone,optiontwo,optionthree,optionfour) values ('"+q+"','"+o1+"','"+o2+"','"+o3+"','"+o4+"')");
答案 1 :(得分:0)
错误信息几乎解释了发生了什么:
android.database.sqlite.SQLiteException:表格问题有6列,但提供了5个值(代码1):,同时编译:插入问题值(&#39; x&#39;,&#39; x&# 39;,&#39; X&#39;&#39; X&#39;&#39; X&#39)
尝试以下几点:
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();