java - 将数据插入数据库 - 为什么我使用 -1 而不是 0?

标签 java sqlite

将数据插入数据库时​​,我使用 -1。我是否应该使用 0 来指示是否未插入任何内容,然后返回 false。只是想知道这是正确的方法吗?

非常感谢,

标记

例程.java

if (routineInserted) {                                                          

Toast.makeText(RoutineEdit.this, "Activity Inserted", Toast.LENGTH_LONG).show();

MediaPlayer mymedia = MediaPlayer.create(RoutineEdit.this, R.raw.whoosh);
 mymedia.start();

} else {

数据库.java

    public boolean insertRoutine(int activityImage, String selectedDay, int activitySlot) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(RoutineColumn1,selectedDay);                                                  
    contentValues.put(RoutineColumn2,activityImage);                                                
    contentValues.put(RoutineColumn3,activitySlot);                                                 
    long result = db.insert(RoutineTable, null, contentValues);                                     // The db.insert is responsible for insertion of the data into the database and stores the result of this action (either true or false) in result.

    if(result == -1)                                                                                // if the result is not equal to -1 or data is not successfully inserted it will return FALSE. otherwise TRUE
        return false;
    else
        return true;}

最佳答案

根据doc .

Returns long the row ID of the newly inserted row, or -1 if an error occurred

所以你应该使用-1。

好吧,为了进一步阐述此 API 的开发人员选择 -1 作为任何发生 SQLException 的返回值。

有关更多信息,您应该查看source code

还有一件事,您可以使用一个 return 语句而不是两个。

    public boolean insertRoutine(int activityImage, String selectedDay, int activitySlot) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(RoutineColumn1,selectedDay);                                                  
        contentValues.put(RoutineColumn2,activityImage);                                                
        contentValues.put(RoutineColumn3,activitySlot);                                                 
        long result = db.insert(RoutineTable, null, contentValues);                                     // The db.insert is responsible for insertion of the data into the database and stores the result of this action (either true or false) in result.

        return (result != -1)
}

关于java - 将数据插入数据库 - 为什么我使用 -1 而不是 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650357/

相关文章:

java - 当方向改变时将对象从一个 fragment 传递到另一个 fragment

java - CURDATE() NOT IN(在 CheckIn 和 CheckOut 之间选择 CURDATE() 的房间)

database - 数据库。如何在附加数据库中创建索引?

php - PDO SQLite创建数据库默认权限

java - 使用 Wildfly 16.0.0.Final 和 ejb 客户端的 TLS/SSL 失败并显示 org.xnio.http.UpgradeFailedException : Invalid response code 200

java - 接口(interface)中的协变返回类型未通过 Javac 编译

java - 用户通过Android中的任何第三方应用程序(CC Cleaner或Kill应用程序)清理应用程序后是否可以重新启动后台服务?

android - SQLite 在 Samsung Galaxy GT-I9000 中非常慢

sql - 查询具有耦合条件的行

c# - sqlite 不识别通用列表