查询未正确运行 – SQLite Android Studio

huangapple go评论81阅读模式
英文:

Query not running correctly - SQlite Android Studio

问题

以下是您提供的代码的翻译部分:

我有这个函数,应该将数据插入到SQLite数据库中,如果数据没有被插入,则返回false,但是函数却返回true,但是当我去检查.db文件时,表中没有数据。

public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
    ContentValues contentValues = new ContentValues();
    SQLiteDatabase db = this.getWritableDatabase();

    //将数据设置到列中
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, emailAddress);
    contentValues.put(COL_4, postalAddress);
    contentValues.put(COL_5, password);
    contentValues.put(COL_6, phoneNumber);
    contentValues.put(COL_7, bloodType);

    //将数据插入到数据库中
    long result = db.insert(TABLE_NAME, null, contentValues);

    //检查数据是否被正确插入
    if (result == -1) {
        return false;
    } else
        return true;
}

下面的图像显示表和数据库已经被正确创建。

查询未正确运行 – SQLite Android Studio

英文:

I have this function that should insert data into the SQlite DB and return false if the data doesn't get inserted however the function returns true but when I go to check the .db file there's no data in the table.

public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
    ContentValues contentValues = new ContentValues();
    SQLiteDatabase db = this.getWritableDatabase();

    //set data to columns
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, emailAddress);
    contentValues.put(COL_4, postalAddress);
    contentValues.put(COL_5, password);
    contentValues.put(COL_6, phoneNumber);
    contentValues.put(COL_7, bloodType);

    //inserts data into db
    long result = db.insert(TABLE_NAME, null, contentValues);

    //checks to see if data has been inserted correctly
    if (result == -1) {
        return false;
    } else
        return true;


}

the image below shows that the table and database have been created correctly.

查询未正确运行 – SQLite Android Studio

答案1

得分: 1

你应该按照此处所述的方式调用#beginTransaction()#setTransactionSuccessful()#endTransaction()

> 如果任何事务在没有被标记为已成功(通过调用setTransactionSuccessful)的情况下结束,更改将被回滚。

public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
    ContentValues contentValues = new ContentValues();
    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    try {
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, emailAddress);
        contentValues.put(COL_4, postalAddress);
        contentValues.put(COL_5, password);
        contentValues.put(COL_6, phoneNumber);
        contentValues.put(COL_7, bloodType);
        long result = db.insert(TABLE_NAME, null, contentValues);
        db.setTransactionSuccessful();
        return result != -1;
    } finally {
        db.endTransaction();
    }
}
英文:

You should call #beginTransaction(), #setTransactionSuccessful() and #endTransaction() like described here.

> The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful).

public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
    ContentValues contentValues = new ContentValues();
    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    try {
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, emailAddress);
        contentValues.put(COL_4, postalAddress);
        contentValues.put(COL_5, password);
        contentValues.put(COL_6, phoneNumber);
        contentValues.put(COL_7, bloodType);
        long result = db.insert(TABLE_NAME, null, contentValues);
        db.setTransactionSuccessful();
        return result != -1;
    } finally {
        db.endTransaction();
    }
}

huangapple
  • 本文由 发表于 2020年4月4日 00:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61016636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定