在SQLite中添加信息

时间:2011-01-15 08:44:22

标签: java android sql sqlite

在向SQLite添加信息时,我的Android应用程序出现问题。我对Java / SQLite相对较新,虽然我已经学习了很多关于SQLite的教程并且能够获得运行的示例代码但是我无法在运行自己的应用程序时创建要创建的表和要导入的数据。我已将我的代码包含在两个Java文件问题(主程序)和QuestionData(辅助类代表数据库)中。

Questions.java:

public class Questions extends Activity {
private QuestionData questions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiztest);
questions = new QuestionData(this);
try {
Cursor cursor = getQuestions();
showQuestions(cursor);
} finally {
questions.close();
}
}

private Cursor getQuestions() {
//Select Query 
String loadQuestions = "SELECT * FROM questionlist";
SQLiteDatabase db = questions.getReadableDatabase();
Cursor cursor = db.rawQuery(loadQuestions, null);
startManagingCursor(cursor);
return cursor;
}

private void showQuestions(Cursor cursor) {
// Collect String Values from Query and Display them this part of the code is wokring fine when there is data present.

QuestionData.java

public class QuestionData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "TriviaQuiz.db" ;
private static final int DATABASE_VERSION = 2;

public QuestionData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);

}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE questionlist (_id INTEGER PRIMARY KEY AUTOINCREMENT, QID TEXT, QQuestion TEXT, QAnswer TEXT, QOption1 TEXT, QOption2 TEXT, QOption3 TEXT, QCategoryTagLvl1 TEXT, QCategoryTagLvl2 TEXT, QOptionalTag1 TEXT, QOptionalTag2 TEXT, QOptionalTag3 TEXT, QOptionalTag4 TEXT, QOptionalTag5 TEXT, QTimePeriod TEXT, QDifficultyRating TEXT, QGenderBias TEXT, QAgeBias TEXT, QRegion TEXT, QWikiLink TEXT, QValidationLink1 TEXT, QValidationLink2 TEXT, QHint TEXT, QLastValidation TEXT, QNotes TEXT, QMultimediaType TEXT, QMultimediaLink TEXT, QLastAsked TEXT);");

db.execSQL("INSERT INTO questionlist (_id, QID, QQuestion, QAnswer, QOption1, QOption2, QOption3, QCategoryTagLvl1, QCategoryTagLvl2, QOptionalTag1, QOptionalTag2, QOptionalTag3, QOptionalTag4, QOptionalTag5, QTimePeriod, QDifficultyRating, QGenderBias, QAgeBias, QRegion, QWikiLink, QValidationLink1, QValidationLink2, QHint, QLastValidation, QNotes, QMultimediaType, QMultimediaLink, QLastAsked)"+
"VALUES (null,'Q00001','Example','Ans1','Q1','Q2','Q3','Q4','','','','','','','','','','','','','','','','','','','','')");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

任何建议都会很棒。我试过调试,这表明数据库不存在。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我删除了常量设置,其中包含大写,其他地方没有引用。我知道它会变得简单,但我找不到问题。再次感谢!