Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在表中插入主键时出现问题_Java_Sqlite_Android Studio_Android Sqlite_Sqliteopenhelper - Fatal编程技术网

Java 在表中插入主键时出现问题

Java 在表中插入主键时出现问题,java,sqlite,android-studio,android-sqlite,sqliteopenhelper,Java,Sqlite,Android Studio,Android Sqlite,Sqliteopenhelper,我有两个表,corporate(用于存储登录信息)和assignment(用于存储分配详细信息) 公司表中的列:id(主键自动递增)、uname、password。 Id不是从用户获取的 分配表中的列:分配主题、分配内容、总分、id(外键)、id分配(主键自动递增) 信息流: 1.登录用户-->除了检查是否为有效用户外,初始化id(1000)并返回它(searchId()方法)。 2.用户界面输入用户-->BackgroundTask-->insertAssignment方法分配的详细信息 问题

我有两个表,corporate(用于存储登录信息)和assignment(用于存储分配详细信息)

公司表中的列:id(主键自动递增)、uname、password。 Id不是从用户获取的

分配表中的列:分配主题、分配内容、总分、id(外键)、id分配(主键自动递增)

信息流: 1.登录用户-->除了检查是否为有效用户外,初始化id(1000)并返回它(searchId()方法)。 2.用户界面输入用户-->BackgroundTask-->insertAssignment方法分配的详细信息

问题:我无法通过从公司表检索id(主键)在分配表中插入id(外键)。相关列显示为空

尝试了以下操作:

  • 正如您在DatabaseHelper类中看到的,我尝试了外键,但它不起作用,因此将其删除
  • 除了上述内容之外,我还尝试通过getter和setter获取id,在一定程度上可以做到这一点,但不知道数据在哪里被破坏
  • 因为我是Android新手,若你们能帮我解决这个问题就太好了

    请让我知道,如果你需要任何进一步的信息从我的结束

    下面是必要的代码

    插入作业类别:

    public void SubmitData(View view) {
    
        String topic_assign = topic.getText().toString();
        String topic_content_assign = topic_content.getText().toString();
        String total_marks = t_marks.getText().toString();
        String id = loginInfo.getId();
    
        Log.e("assignment_id:",id);
    
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute("add_assignment", topic_assign, topic_content_assign, total_marks,id);
    }
    
    背景任务类:

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.AsyncTask;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class BackgroundTask extends AsyncTask<String, Teacher_Assignment, String> {
    
        private static final String TAG = " Background task";
    
        LoginInfo loginInfo = new LoginInfo();
    
        Activity activity;
        ListView listView;
    
        AssignmentAdapter assignmentAdapter;
        Teacher_Assignment teacher_assignment = new Teacher_Assignment();
    
        private Context context;
    
    
        BackgroundTask(Context context)
        {
            this.context =  context;
            activity = (Activity)context;
        }
    
        @Override
        protected String doInBackground(String... params) {
    
            String method = params[0];
            DatabaseHelper databaseHelper = new DatabaseHelper(context);
            if(method.equals("add_assignment")) {
                String topic_assignment = params[1];
                String topic_content = params[2];
                String total_marks = params[3];
                String id = params[4];
    
    
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                databaseHelper.insertAssignment(db,topic_assignment,topic_content,total_marks, id);
                Log.d(TAG,"inserted successfully");
    
            } else if(method.equals("get_info")) {
    
                listView = (ListView) activity.findViewById(R.id.list_assignment);
    
                //DatabaseHelper databaseHelper = new DatabaseHelper(context);
                SQLiteDatabase db = databaseHelper.getReadableDatabase();
                Cursor cursor = databaseHelper.getInformation(db);
    
    
                assignmentAdapter = new AssignmentAdapter(context, R.layout.activity_assignment_page_recycle_view);
    
                String assignment_content;
                while(cursor.moveToNext()){
    
                    assignment_content = cursor.getString(cursor.getColumnIndex(teacher_assignment.Assignment_content));
                    Teacher_Assignment teacher_assignment = new Teacher_Assignment(assignment_content);
                    publishProgress(teacher_assignment);
                }
    
                return "get_info";
            }
    
    
            return "One row inserted";
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected void onPostExecute(String result) {
    
            if (result.equals("get_info")) {
                listView.setAdapter(assignmentAdapter);
            } else {
                Toast.makeText(context, result, Toast.LENGTH_LONG).show();
            }
    
        }
    
    
        protected void onProgressUpdate(Assignment_Page... values) {
            assignmentAdapter.add(values[2]);
        }
    }
    
    数据库截图:


    id值来自在BackgroundTask中初始化的loginInfo实例:

    class DatabaseHelper extends SQLiteOpenHelper {
    
    LoginInfo loginInfo = new LoginInfo();
    

    如果id是通过
    //id=searchId(列\u UNAME)获得的检查searchId方法。

    此问题通过将数据从一个活动传递到另一个活动,然后在不注释id=searchId(列_UNAME)时调用searchId函数来解决,正如您在数据库屏幕截图中看到的,值变为not found而不是Null searchId()方法返回“not found”。好的,它取id的初始值。您能帮我解决这个问题吗…这与columnindex有关。与我们共享searchId()方法。请在searchId()下面查找方法以及searchLogin方法()当您在方法中将id作为参数时,为什么要从loginInfo获取值?我之所以将其作为参数,是因为在我的后台任务中,我通过loginInfo.getid()存储数据…您能告诉我另一种获取ID的方法吗?您不就是使用
    content1.put(COLUMN\u ID,ID)
    而不是
    content1.put(COLUMN_ID,loginInfo.getId())?嗨,迈克..我试着使用content1.put(COLUMN\u ID,ID);但仍然是空的
    
    class DatabaseHelper extends SQLiteOpenHelper {
    
    LoginInfo loginInfo = new LoginInfo();