英文:
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;
}
下面的图像显示表和数据库已经被正确创建。
英文:
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.
答案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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论