Android:在另一个表

时间:2015-12-28 01:01:55

标签: android sqlite

我在另一个表中插入数据时遇到问题,我创建了一个联系人表和测验表,我创建了另一个用于测试表的类,它是AdminContact类,我调用了DatabaseHelper中的AdminContact类。但是当我在测验表中保存数据并且我将单击保存按钮时,它将仅返回到上一个活动,并且没有已保存在sqlite数据库中的数据。请检查我的代码。谢谢..

    AdminContact Class:
        public class AdminContact {

            public static  abstract class Question{

                public static final String COLUMN_QUESTION = "question";
                public static final String COLUMN_ANSWER1 = "answer1";
                public static final String COLUMN_ANSWER2 = "answer2";
                public static final String COLUMN_ANSWER3 = "answer3";
                public static final String COLUMN_ANSWER4 = "answer4";
               public static final String TABLE_QUIZ = "quiz";




            }

        }


    My DatabaseHelper:
    public class DatabaseHelper extends SQLiteOpenHelper {

       // contact table
       private  static final int DATABASE_VERSION = 1;
        private  static final String DATABASE_NAME = "contacts.db";
        private  static final String TABLE_NAME = "contacts";
        private  static final String COLUMN_ID = "id";
        private  static final String COLUMN_NAME = "name";
        private  static final String COLUMN_EMAIL = "email";
        private  static final String COLUMN_UNAME = "uname";
        private  static final String COLUMN_PASS = "pass";

        SQLiteDatabase db;

        private static final String CREATE_QUERY = "CREATE TABLE "+ AdminContact.Question.TABLE_QUIZ+"("+ AdminContact.Question.COLUMN_QUESTION+" TEXT,"+
                AdminContact.Question.COLUMN_ANSWER1+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER2+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER3+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER4+" TEXT);";

        private static final String TABLE_CREATE = "create table contacts (id integer primary key not null ," +
                "name text not null, email text not null, uname text not null, pass text not null);";


        public DatabaseHelper(Context context)
        {
            super(context , DATABASE_NAME , null , DATABASE_VERSION);
           Log.e("DATABASE OPERATION", "Database created");
        }

    public void addQuestion(String question, String answer1, String answer2, String answer3, String answer4, SQLiteDatabase db){

        ContentValues contentValues = new ContentValues();
        contentValues.put(AdminContact.Question.COLUMN_QUESTION, question);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER1, answer1);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER2, answer2);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER3, answer3);
        contentValues.put(AdminContact.Question.COLUMN_ANSWER4, answer4);
        db.insert(AdminContact.Question.TABLE_QUIZ, null,contentValues);
        Log.e("DATABASE OPERATION", "One row inserted");

    }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            db.execSQL(CREATE_QUERY);
            this.db = db;
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
            String query1 = "DROP TABLE IF EXISTS "+AdminContact.Question.TABLE_QUIZ;
            db.execSQL(query);
            db.execSQL(query1);
            this.onCreate(db);

        }

My OnSaveClick Button:


public class Add  extends Activity {

    DatabaseHelper helper;
    SQLiteDatabase sqlitedatabase;
    EditText editTextQuestion, editTextAns1, editTextAns2, editTextAns3, editTextAns4;
    Button clear;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_question);
        helper = new DatabaseHelper(this);

        editTextQuestion = (EditText) findViewById(R.id.TFquestion);
        editTextAns1 = (EditText) findViewById(R.id.TFans1);
        editTextAns2 = (EditText) findViewById(R.id.TFans2);
        editTextAns3 = (EditText) findViewById(R.id.TFans3);
        editTextAns4 = (EditText) findViewById(R.id.TFans4);
}


    public void OnSaveClick(View view)
    {
        String question = editTextQuestion.getText().toString();
        String answer1 = editTextAns1.getText().toString();
        String answer2 = editTextAns2.getText().toString();
        String answer3 = editTextAns3.getText().toString();
        String answer4 = editTextAns4.getText().toString();
        helper = new DatabaseHelper(context);
        sqlitedatabase = helper.getWritableDatabase();
        helper.addQuestion(question,answer1,answer2,answer3,answer4,sqlitedatabase);
        Toast.makeText(getBaseContext(), "Successfully save!",Toast.LENGTH_LONG).show();

        helper.close();
    }
}

1 个答案:

答案 0 :(得分:0)

您的测验表没有PRIMARY KEY。我认为你应该实现BaseColumn并使用_ID作为主键,如下所示:

public static  abstract class Question implements BaseColumns {

            public static final String COLUMN_QUESTION = "question";
            public static final String COLUMN_ANSWER1 = "answer1";
            public static final String COLUMN_ANSWER2 = "answer2";
            public static final String COLUMN_ANSWER3 = "answer3";
            public static final String COLUMN_ANSWER4 = "answer4";
           public static final String TABLE_QUIZ = "quiz";
        }

private static final String CREATE_QUERY = "CREATE TABLE "+ AdminContact.Question.TABLE_QUIZ+"("+ AdminContact.Question._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + AdminContact.Question.COLUMN_QUESTION+" TEXT,"+
            AdminContact.Question.COLUMN_ANSWER1+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER2+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER3+" TEXT,"+ AdminContact.Question.COLUMN_ANSWER4+" TEXT);";