将数据插入本地存储失败

时间:2018-12-12 16:58:44

标签: android sql database sqlite return

活动代码

ClassA

onCreate

    private SQLiteDatabase mSqliteDatabase;

public static String TABLE_CHAT_DATA = null;
public static final String KEY_SENDER = "Sender";
public static final String KEY_MESSAGE = "Message";
public static final String KEY_TIME = "Time";
public static final String KEY_SEEN = "Seen";
public static final String KEY_TIMER = "Timer";
public static final String KEY_TYPE = "Type";

插入数据

 mSqliteDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
    TABLE_CHAT_DATA = MessageRecieverId;
    String mQuery = "CREATE TABLE IF NOT EXISTS " + TABLE_CHAT_DATA + "(" + KEY_SENDER + " TEXT, " + KEY_MESSAGE
            + " TEXT, " + KEY_TIME + " TEXT, " + KEY_SEEN + " TEXT, " + KEY_TIMER + " TEXT, " + KEY_TYPE + "TEXT)";
    mSqliteDatabase.execSQL(mQuery);

Toast显示Nope,表示返回值为-1,表示失败。我已经多次通过相同的代码,但是无法弄清楚其失败的原因。

表创建成功,并且在插入数据时它们都不为空,因为我也尝试插入硬编码数据,但仍然失败。还尝试使用 MainData helper = new MainData(this); 代替 getApplicationContext(),但仍然无法正常工作。有人可以指出我哪里出问题了吗?

尝试insertOrThrow后出现错误

     MainData helper = new MainData(getApplicationContext());
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_SENDER, "Me");
        contentValues.put(KEY_MESSAGE, messageText);
        contentValues.put(KEY_TIME, String.valueOf(ServerValue.TIMESTAMP));
        contentValues.put(KEY_SEEN, "No");
        contentValues.put(KEY_TIMER, "TYPE");
        contentValues.put(KEY_TYPE, "Text");

        long returnVariable = db.insert(TABLE_CHAT_DATA,null,contentValues);
        if (returnVariable == -1) {
            Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
            //-1 means there was an error updating the values
        } else {
            Toast.makeText(getApplication(),"SAVED", Toast.LENGTH_SHORT).show();
        }

有点困惑,因为它没有显示这样的列,但是在创建时它显然具有该列

0 个答案:

没有答案