抱歉我的英文。我说英语不太好。 作为Android编程的新手,我正在研究一个Android应用程序,它使用SQLITEDATABASE创建,修改和简单地删除测验。
运行程序后出错。 这是我的程序的解释
我按照以下步骤进行:
这是我想要创建的表的结构。 Table Example
我要求用户输入要为测验创建的新表的名称,然后我在EDITTEXT中获取屏幕用户输入的值,然后根据用户输入的名称创建一个表。 这是activity_add_quizz的图片。 activity_add_quizz
所以当点击按钮Save NAME时。 onclick按钮方法是saveNameListener。 守则在下面
这是错误:
Caused by: android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: CREATE TABLE null (_id integer primary key autoincrement, Question TEXT,Reponse1 TEXT,Reponse2 TEXT,Reponse3 TEXT,Reponse4 TEXT,Reponse5 TEXT,Reponse6 TEXT,Reponse7 TEXT,TrueReponse TEXT);
我已在EditText"表"中输入的文字没有创建为新的TABLE 有人可以帮助我PLZ。
这是我的代码:
public class BasedeDonee extends SQLiteOpenHelper {
public String Table_Name;
SQLiteDatabase db;
public static final int DATABASE_VERSION = 1;
int j = 0;
public String DATABASE_CREATE1 = "CREATE TABLE "+ Table_Name
+ " (_id integer primary key autoincrement, "
+ "Question TEXT,"
+ "Reponse1 TEXT,"
+ "Reponse2 TEXT,"
+ "Reponse3 TEXT,"
+ "Reponse4 TEXT,"
+ "Reponse5 TEXT,"
+ "Reponse6 TEXT,"
+ "Reponse7 TEXT,"
+ "TrueReponse TEXT);";
public BasedeDonee(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
this.db = database;
database.execSQL(DATABASE_CREATE1);
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2, TrueReponse) VALUES ('First Question','Reponse 1','Reponse 2','Reponse 1')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Second Question','Reponse 1','Reponse 2')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Third Question','Reponse 1', 'Answer 2')");
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
/* Pas pour le moment */
}
public Cursor getTableName() {
this.db = this.getWritableDatabase();
return db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' And name is not 'sqlite_sequence' And name is not'android_metadata'", null);
}
public Cursor getTableContent() {
this.db = this.getWritableDatabase();
return db.rawQuery("SELECT Question FROM " + Table_Name, null);
}
public Cursor getTableContents() {
this.db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + Table_Name, null);
}
public void insertData(String question, String[] table, String reponsevrai) {
ContentValues cv = new ContentValues();
cv.put("Question", question);
// for(int j=0; j<=table.length; j++){
cv.put("Reponse1", table[0]);
cv.put("Reponse2", table[1]);
cv.put("Reponse3", table[2]);
cv.put("Reponse4", table[3]);
cv.put("Reponse5", table[4]);
cv.put("Reponse6", table[5]);
cv.put("Reponse7", table[6]);
// }
cv.put("TrueReponse", reponsevrai);
db.insert(Table_Name, null, cv);
Log.d("AddQuizz", "Insertion of Data succed");
}
public void chargerLesQuizzs(List<String> lcs) {
Cursor c = this.getTableName();
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
lcs.add(c.getString(c.getColumnIndex("name")));
c.moveToNext();
}
}
c.close();
}
public void chargerLesQuestions(List<String> lcs) {
Cursor cursor = this.getTableContent();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String Question = cursor.getString(0);
lcs.add(Question);
cursor.moveToNext();
}
cursor.close();
}
public void chargerLesDonees(Question[] q) {
String[] quest = new String[7];
Cursor cursor = this.getTableContents();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
quest[0] = cursor.getString(cursor.getColumnIndex("Reponse1"));
quest[1] = cursor.getString(cursor.getColumnIndex("Reponse2"));
quest[2] = cursor.getString(cursor.getColumnIndex("Reponse3"));
quest[3] = cursor.getString(cursor.getColumnIndex("Reponse4"));
quest[4] = cursor.getString(cursor.getColumnIndex("Reponse5"));
quest[5] = cursor.getString(cursor.getColumnIndex("Reponse6"));
quest[6] = cursor.getString(cursor.getColumnIndex("Reponse7"));
q[j] = new Question(cursor.getString(cursor.getColumnIndex("Question")), quest, cursor.getString(cursor.getColumnIndex("TrueReponse")));
j++;
cursor.moveToNext();
}
cursor.close();
}}
以下是AddQuizz活动的一部分。
public class AddQuizz extends Activity {
String a;
String trueAnswer;
String []reponses={null,null,null,null,null,null,null};
int i=0;
EditText Questions, Reponses, Name;
Button SaveQuestion, Instruction, NextQuestion, SaveReponse;
RadioGroup radios;
RadioButton vrai, faux;
ListView ListSaisi;
List<String> listQuestion = new ArrayList<String>();
ArrayAdapter<String> adapter;
BasedeDonee db;
SQLiteDatabase SQ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_quizz);
Questions=(EditText)findViewById(R.id.editTextQuestion);
Reponses=(EditText)findViewById(R.id.editTextReponse);
Name=(EditText)findViewById(R.id.SaveName);
Questions.setVisibility(View.INVISIBLE);
Reponses.setVisibility(View.INVISIBLE);
SaveQuestion=(Button)findViewById(R.id.SaveQuestion);
SaveReponse=(Button)findViewById(R.id.SaveReponse);
Instruction=(Button)findViewById(R.id.buttonInstruction);
NextQuestion=(Button)findViewById(R.id.buttonQuestionNext);
SaveQuestion.setVisibility(View.INVISIBLE);
SaveReponse.setVisibility(View.INVISIBLE);
SaveQuestion.setVisibility(View.INVISIBLE);
NextQuestion.setVisibility(View.INVISIBLE);
vrai=(RadioButton)findViewById(R.id.VraiRadioButton);
faux=(RadioButton)findViewById(R.id.FauxRadioButton);
radios=(RadioGroup)findViewById(R.id.group);
radios.setVisibility(View.INVISIBLE);
ListSaisi=(ListView) findViewById(R.id.listViewReponses);
ListSaisi.setVisibility(View.INVISIBLE);
}
public void saveNameListener(View v){
db=new BasedeDonee(this);
db.Table_Name=Name.getText().toString();
db.chargerLesQuestions(listQuestion);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listQuestion);
ListSaisi.setAdapter(adapter);
Questions.setVisibility(View.VISIBLE);
Reponses.setVisibility(View.VISIBLE);
radios.setVisibility(View.VISIBLE);
SaveQuestion.setVisibility(View.VISIBLE);
SaveReponse.setVisibility(View.VISIBLE);
SaveQuestion.setVisibility(View.VISIBLE);
NextQuestion.setVisibility(View.VISIBLE);
ListSaisi.setVisibility(View.VISIBLE);
}}
答案 0 :(得分:0)
确保运行以下方法时没有任何错误。不知何故,似乎您的应用程序在创建给定表之前运行SELECT查询。请查看以下教程中的第9.2节中的示例:http://www.vogella.com/tutorials/AndroidSQLite/article.html
@Override
public void onCreate(SQLiteDatabase database) {
this.db = database;
database.execSQL(DATABASE_CREATE1);
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2, TrueReponse) VALUES ('First Question','Reponse 1','Reponse 2','Reponse 1')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Second Question','Reponse 1','Reponse 2')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Third Question','Reponse 1', 'Answer 2')");
}
更新
试试这段代码:
@Override
public void onCreate(SQLiteDatabase database) {
this.db = database;
String sql = String DATABASE_CREATE1 = "CREATE TABLE "+ Table_Name
+ " (_id integer primary key autoincrement, "
+ "Question TEXT,"
+ "Reponse1 TEXT,"
+ "Reponse2 TEXT,"
+ "Reponse3 TEXT,"
+ "Reponse4 TEXT,"
+ "Reponse5 TEXT,"
+ "Reponse6 TEXT,"
+ "Reponse7 TEXT,"
+ "TrueReponse TEXT);";
database.execSQL(sql);
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2, TrueReponse) VALUES ('First Question','Reponse 1','Reponse 2','Reponse 1')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Second Question','Reponse 1','Reponse 2')");
db.execSQL("INSERT INTO " + Table_Name + " (Question, Reponse1, Reponse2) VALUES ('Third Question','Reponse 1', 'Answer 2')");
}
如果仍然无效,则问题可能出在onCreate方法的调用顺序中。在设置Table_name之前调用它。检查一下以确保:When the SQLiteOpenHelper onCreate method is called?
答案 1 :(得分:0)
我相信你因为这个而得到错误
db.Table_Name=Name.getText().toString();
您可以像这样在BasedeDonee类中设置表名,然后重试
public String Table_Name="Questionaries";