在SQLite数据库中创建多个表

时间:2016-06-23 12:09:56

标签: android android-sqlite

我正在尝试在SQLite数据库中创建四个表。当我尝试创建这些表时,应用程序崩溃了。如果我注释掉除了一个表之外的所有表,代码运行正常,应用程序不会崩溃。这是我的MyHelper类,它扩展了SQLiteOpenHelper

public MyHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL("Create table "+TABLE_NAME+"("+COL1+" text)");

    db.execSQL("Create table "+QUES_TABLE_NAME+"("+QUES_COL1+" integer primary key autoincrement, "
            +QUES_COL2+" text, "+QUES_COL3+" text, "+QUES_COL4+" text, "+QUES_COL5+" text, "+QUES_COL6+" text, "
            +QUES_COL7+" text)");

    db.execSQL("Create table "+STUD_TABLE_NAME+"("+STUD_COL1+" integer primary key autoincrement, "
            +STUD_COL2+" text, "+STUD_COL3+" text, "+STUD_COL4+" integer, "+STUD_COL5+" text, "+STUD_COL6+" integer)");

    db.execSQL("Create table "+RES_TABLE_NAME+"("+RES_COL1+" integer primary key autoincrement, "
            +RES_COL2+" text, "+RES_COL3+" integer)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("Drop table if exists "+QUES_TABLE_NAME);
    db.execSQL("Drop table if exists "+STUD_TABLE_NAME);
    db.execSQL("Drop table if exists "+RES_TABLE_NAME);
    db.execSQL("Drop table if exists "+TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String sqeuence)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues con = new ContentValues();
    con.put(COL1, sqeuence);
    long result = db.insert(TABLE_NAME, null, con);
    if (result == -1)
        return false;
    else
        return true;
}

这是我的main_activity课程。我正在尝试调用MyHelper类:

public class MainActivity extends AppCompatActivity
{
Button btn_addQuesAns, btn_addStdnt, btn_strtExam, btn_showResult, btn_showCrctAns;
MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    btn_addQuesAns = (Button) findViewById(R.id.button_addQuestionAnswers);
    btn_addStdnt = (Button) findViewById(R.id.button_addStudents);
    btn_strtExam = (Button) findViewById(R.id.button_startExam);
    btn_showResult = (Button) findViewById(R.id.button_showResult);
    btn_showCrctAns = (Button) findViewById(R.id.button_showCorrectAnswers);
    myHelper = new MyHelper(this);
    btn_addQuesAns.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            boolean isInserted = myHelper.insertData("Add Question and Answers");
            if (isInserted)
            {
                Toast.makeText(HomeActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), AddQuestionAndAnswersActivity.class);
                startActivity(i);
                finish();
            }
            else
                Toast.makeText(HomeActivity.this, "Data not Inserted", Toast.LENGTH_SHORT).show();
        }
    });

/*----i have coded all button click as above button---*/

有关为什么我的应用程序没有创建表格的任何建议?

0 个答案:

没有答案