【问题标题】:How to save Integer to Database(SQLite)如何将整数保存到数据库(SQLite)
【发布时间】:2018-08-03 18:48:57
【问题描述】:

在我的程序中,我有一个数据库,我的数据库中有一个名为 student 的表,在 student 表中,有一个名为 StudentID 的列现在应该保存十个数字我的问题是学生 ID 列无法保存数字,当我输入小于十的数字时,它会显示保存消息,但在 logcat 中有错误,当我输入十个数字时,它将从程序中退出。

对不起,我的英语不好。

学生表的 SQLite 代码:

create table student(student_id integer(10) primary key not null , class_id int , student_name nvarchar(50) , FOREIGN KEY (class_id) REFERENCES class(class_id))

保存学生方法:

    public void SaveStudent(int studentId , int classID , String studentName)
{
    ContentValues values = new ContentValues();

    values.put("student_id" , studentId);
    values.put("class_id" , classID);
    values.put("student_name" , studentName);

    database.insert("student" , null , values);

    Toast.makeText(context, "Save!" , Toast.LENGTH_SHORT).show();
}

获取学生信息:

    Button saveStudent_btn = (Button)viewLayout.findViewById(R.id.item_btn_SaveStudent);

    saveStudent_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
    public void onClick(View v) {

    HashMap<String, Object> obj = (HashMap<String, Object>)     adapter.getItem(position);
    int classId = (int) obj.get("class_id");

    EditText studentName_et = (EditText)viewLayout.findViewById(R.id.item_et_StudentName);
    String studentName = studentName_et.getText().toString();

    EditText studentId_et = (EditText)viewLayout.findViewById(R.id.item_et_StudentId);
    int studentId = Integer.parseInt(studentId_et.getText().toString());

    database.OpenDatabase();
    database.SaveStudent(studentId , classId , studentName);
    database.close();

    }
    });

输入 10 个数字时出现 Logcat 错误:

02-23 19:05:07.850 1896-1896/com.example.user.classmanager E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.user.classmanager, PID: 1896
        java.lang.NumberFormatException: Invalid int: "4318659489"
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parse(Integer.java:413)
        at java.lang.Integer.parseInt(Integer.java:367)
 com.example.user.classmanager.SecondTab$1$1$1.onClick(SecondTab.java:94)
        at android.view.View.performClick(View.java:5225)
        at android.view.View$PerformClick.run(View.java:21195)
        at android.os.Handler.handleCallback(Handler.java:739)

少于 10 个数字的 logcat 错误:

  02-23 19:42:32.502 10388-10388/com.example.user.classmanager E/SQLiteDatabase: Error inserting 

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: student.student_id (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
at com.example.user.classmanager.DatabaseHandler.SaveStudent(DatabaseHandler.java:189)
at com.example.user.classmanager.SecondTab$1$1$1.onClick(SecondTab.java:97)

【问题讨论】:

    标签: android database performance sqlite android-sqlite


    【解决方案1】:

    问题是int的长度,你的号码溢出。 请改用Long

    Long  studentId = Long.parseLong(studentId_et.getText().toString());
    

    int 在 java 中的范围从 -2,147,483,648 到 2,147,483,647 。你的号码溢出了。

    对于UNIQUE constraint failed,您违反了主键约束。确保生成的 id 是唯一的,并且始终不为空。

    【讨论】:

    • 这种类型(整数 10)是数据库的好类型吗?我不希望程序的大小增加
    • 如果您只使用数据作为本地手段,那么这一切都取决于您的数据结构,如果没有同步到服务器,那么您可以使用 studentId 作为自动增量。否则就保持这种方式。大小与它无关,直到它的范围内。
    • 嘿朋友我有新问题,保存号码后,当显示号码时,存储的号码被存储َ作为其他号码@ADM
    • 这似乎是您的代码中的一些逻辑问题。您应该针对此问题提出一个新问题。首先调试您的代码,检查究竟保存到数据库的内容。
    • 你可以看到我的新问题:stackoverflow.com/questions/49304779/…
    【解决方案2】:

    UNIQUE constraint failed 这意味着您正在尝试插入 id 值,该值已经存在并且违反了主键的唯一性..插入到使StudentID 自动递增的空白处。

    【讨论】:

    • 哦,谢谢你的帮助
    • @NimaKhalili 你很高兴
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2020-11-30
    • 2013-01-18
    • 2021-06-05
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多