gpt4 book ai didi

android - 如何使用 SqliteDatabse 生成表

转载 作者:行者123 更新时间:2023-11-29 21:53:55 25 4
gpt4 key购买 nike

抱歉我的英语不好。我的英语说得不太好。作为 Android 编程新手,我正在开发一个 Android 应用程序,该应用程序使用 SQLITEDATABASE 创建、修改和简单地删除测验。

运行程序后出现错误。这是我的程序的解释

我按如下方式进行:

这是我要创建的表的结构。 Table Example

我要求用户输入要为测验创建的新表的名称,然后获取屏幕上用户在 EDITTEXT 中输入的值,并根据用户输入的名称创建一个表。这是activity_add_quizz 的图片。 activity_add_quizz

因此,当单击“保存名称”按钮时。 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“表”中输入的文本未创建为新表有人可以帮我吗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 Activity 的一部分。

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);
}}

最佳答案

确保以下方法运行时没有任何错误。不知何故,您的应用程序似乎在创建给定表之前运行 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?

关于android - 如何使用 SqliteDatabse 生成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com