在表中插入主键的问题

Posted

技术标签:

【中文标题】在表中插入主键的问题【英文标题】:Problem in inserting primary Key in table 【发布时间】:2019-02-24 11:39:47 【问题描述】:

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

Corporate 表中的列:id(主键自动增量)、uname、密码。 ID 不是从用户那里获取的。

分配表中的列:assignment_topic、assignment_content、total_marks、id(外键)、id_assignment(主键自增)。

信息流: 1. 登录用户 --> 除了检查是否有效用户,初始化 id(1000) 并返回它(searchId() 方法)。 2. 用户分配的UI输入细节 --> 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);

BackgroundTask 类:

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]);
    

DatabaseHelper 类:

package com.example.education;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.nfc.Tag;
import android.util.Log;

class DatabaseHelper extends SQLiteOpenHelper 

    LoginInfo loginInfo = new LoginInfo();

    Teacher_Assignment assign1 = new Teacher_Assignment();

    //Login table
    private static final int DATABASE_VERSION = 41;
    private static final String DATABASE_NAME = "userInfo.db";
    private static final String TABLE_NAME = "corporate";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASSWORD = "password";

    private static final String TABLE_ASSIGNMENT = "assignment";
    //private static final String COLUMN_ID_ASSIGNMENT = "user_id";
    private static final String ASSIGNMENT_TOPIC = "assignment_topic";
    //private static final String COLUMN_NAME = COLUMN_UNAME;
    private static final String ASSIGNMENT_CONTENT = "assignment_content";
    private static final String ASSIGNMENT_TOTAL_MARKS = "total_marks";
    private static final String ID_COLUMN_ASSIGNMENT = "id_assignment";


    private static final String TABLE_STUDENT_SELECTION = "student_selection";
    private static final String COLUMN_STUDENT_ID = "student_id";
    private static final String COLUMN_CLASS_SEC = "class_sec";
    private static final String COLUMN_STUDENT_NAME = "name";
    private static final String COLUMN_TOTAL_MARKS = "total_marks";
    private static final String COLUMN_STUDENT_MARKS = "student_score";



    SQLiteDatabase db;

    private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_UNAME + " TEXT NOT NULL, " + COLUMN_PASSWORD + " TEXT NOT NULL " + ")";


    private static final String CREATE_ASSIGNMENT_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_ASSIGNMENT + "("
            + ASSIGNMENT_TOPIC + " TEXT NOT NULL, " + ASSIGNMENT_CONTENT  + " TEXT NOT NULL, "
            + ASSIGNMENT_TOTAL_MARKS + " TEXT NOT NULL, "
            + ID_COLUMN_ASSIGNMENT + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_ID + " TEXT " + ")";


    //+ " FOREIGN KEY(" + COLUMN_ID + ") REFERENCES " + TABLE_NAME + " (id) "

    public DatabaseHelper(Context context) 
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    

    public void insertData() 

        db = this.getWritableDatabase();
        ContentValues content = new ContentValues();

        content.put(COLUMN_ID,1000);
        content.put(COLUMN_UNAME, "akhil");
        content.put(COLUMN_PASSWORD,"admin");



        db.insert(TABLE_NAME, null, content);

        db.close();
    

    public String insertAssignment(SQLiteDatabase db,String topic, String assignment_content, String total_marks, String id) 

        db = this.getWritableDatabase();
        ContentValues content1 = new ContentValues();

        //id = searchId(COLUMN_UNAME);

        content1.put(ASSIGNMENT_TOPIC, topic);
        content1.put(ASSIGNMENT_CONTENT,assignment_content);
        content1.put(ASSIGNMENT_TOTAL_MARKS,total_marks);
        content1.put(COLUMN_ID, loginInfo.getId());
        db.insert(TABLE_ASSIGNMENT, null, content1);
        db.close();

        return topic;
    

数据库截图:

【问题讨论】:

当您将 id 作为方法中的参数时,为什么要从 loginInfo 获取值? 我将其作为参数,因为在我的后台任务中,我通过 logininfo.getid() 存储数据...您能告诉我另一种获取 id 的方法 您不应该只使用 content1.put(COLUMN_ID, id); 而不是 content1.put(COLUMN_ID, loginInfo.getId()); 嗨迈克..我尝试使用 content1.put(COLUMN_ID, id);但仍然为空 【参考方案1】:

通过将数据从一个活动传递到另一个活动,然后调用 SearchId 函数解决了这个问题

【讨论】:

【参考方案2】:

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

class DatabaseHelper extends SQLiteOpenHelper 

LoginInfo loginInfo = new LoginInfo();

如果 id 是通过//id = searchId(COLUMN_UNAME); 获取的,请检查 searchId 方法。

【讨论】:

不评论时 id = searchId(COLUMN_UNAME); ,正如您在数据库屏幕截图中看到的那样,该值是 not found 而不是 null searchId() 方法返回“未找到”。 好的,所以它采用了 id 的初始值。你能帮我解决这个问题吗?它与 columnindex 有关系吗? 分享 searchId() 方法。 请在下面找到searchID()方法以及searchLogin方法()

以上是关于在表中插入主键的问题的主要内容,如果未能解决你的问题,请参考以下文章

Mysql - 将值插入具有未知主键的多个表中

SQL怎么在有外键的主键表中插数据

主键和外键的作用

主键和外键的作用

Django:关于自动递增主键的最佳实践

sql主键和外键的区别