【问题标题】:Trying to add data to a database尝试将数据添加到数据库
【发布时间】:2018-10-20 00:45:43
【问题描述】:

我正在尝试将数据添加到具有 3 列的数据库中,当我尝试添加多个值时,我收到了我创建的 toast 消息,上面写着“出了点问题”我知道我做错了什么,但我我不确定。这是所有 AddData 方法都有错误的主要活动

btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newEntry = editText.getText().toString();
                String dateReleased=edtDateReleased.getText().toString();
                if (editText.length() != 0) {
                    AddData(newEntry,dateReleased);
                    editText.setText("");
                    edtDateReleased.setText("");
                } else {
                    toastMessage("You must put something in the text");
        btnViewData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
                startActivity(intent);
            }
        });
    }

    public void AddData(String newEntry,String dateReleased) {
        boolean insertData = mDatabaseHelper.addData(newEntry,dateReleased);
        if (insertData) {
            toastMessage("Data Successfully Inserted!");
        } else {
            toastMessage("Something went wrong");
        }
    }

这里是 DatabaseHelper 类

 @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL2 +" TEXT, "
                +COL3+" TEXT,"
                +COL4+" TEXT)";
        db.execSQL(createTable);
    }

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

    public boolean addData(String title,String dateReleased) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, title);
        contentValues.put(COL3,dateReleased);
        String fileName="/sdcard/"+title+".mpg";
        contentValues.put(COL4,fileName);


        Log.d(TAG, "addData: Adding " + title + " to " + TABLE_NAME);

        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

【问题讨论】:

    标签: java android database sqlite


    【解决方案1】:

    你应该首先使用你的类的实例打开连接

    SQLiteDatabase db =this.getWritableDatabase();
    this.onOpen(db);
    ContentValues contentValues = new ContentValues();
    contentValues.put("column_name", "column_value");
    long result = db.insert(TABLE_NAME, null, contentValues);
    db.close();
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2017-04-29
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2014-01-08
      • 2016-09-27
      • 2021-02-18
      相关资源
      最近更新 更多