dimanche 3 mai 2015

Android SQLite DataType mismatch

I am trying to add JSON objects as strings to my database. I am using the following code :

public static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + "("
                    + KEY_PRIMARY + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COLUMN_ID + " TEXT, "
                    + COLUMN_TITLE + " TEXT, "
                    + COLUMN_AUTHOR + " TEXT, "
                    + COLUMN_VOTES + " TEXT, "
                    + COLUMN_SEARCH + " TEXT)";

public int insertQuestion3(Context c,String ids,String titles,String authors,String votes,String search)
    {
        DatabaseWrapper databaseWrapper = new DatabaseWrapper(c);
        Log.e("ERROR2", String.valueOf(isDatabaseOpened()));
        myDataBase = databaseWrapper.getWritableDatabase();

        long questionId = 0;
        if (isDatabaseOpened()) {
            ContentValues values = new ContentValues();
            values.put(QuestionORM.COLUMN_ID, ids);
            values.put(QuestionORM.COLUMN_TITLE,titles);
            values.put(QuestionORM.COLUMN_AUTHOR, authors);
            values.put(QuestionORM.COLUMN_VOTES, votes);
            values.put(QuestionORM.COLUMN_SEARCH, search);
            questionId = myDataBase.insert(QuestionORM.TABLE_NAME, "null", values);
            Log.e(TAG, "Inserted new Question with ID: " + questionId);
            myDataBase.close();
        }
        return (int) questionId;
        }

And in the activity :

holdid = new JSONObject();
                    holdid.put("uniqueIDs", new JSONArray(ids));
                    String _id = holdid.toString();
                    Log.e("VALUE5",_id);

                    holdauthor = new JSONObject();
                    holdauthor.put("uniqueAuthors",new JSONArray(authors));
                    String _auth = holdauthor.toString();
                    Log.e("VALUE4",_auth);

                    holdtitle = new JSONObject();
                    holdtitle.put("uniqueTitles",new JSONArray(titles));
                    String _title = holdtitle.toString();
                    Log.e("VALUE2",_title);

                    holdvotes = new JSONObject();
                    holdvotes.put("uniqueVotes",new JSONArray(votes));
                    String _vote = holdvotes.toString();
                    Log.e("VALUE",_vote);                           q.insertQuestion3(MainActivity.this,_id,_title,_auth,_vote,val);

But I get the following errors in logical:

statement aborts at 5: [INSERT INTO question(search,votes,title,id,author) VALUES (?,?,?,?,?)] datatype mismatch

But all of them have the same datatype i.e. TEXT.

I am getting this error on this line :

            questionId = myDataBase.insert(QuestionORM.TABLE_NAME, "null", values);

But all of them have the same datatype i.e. TEXT and also ,the questionId is returned as -1.

How do I fix this ?

Thanks !

Aucun commentaire:

Enregistrer un commentaire