外键列获取NULL值

外键列获取NULL值

本文介绍了外键列获取NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQLite的双表格(信息和WorkDetails),其中WorkDetails具有参考信息的外键。当每次插入数据,外键应当遵循PK的信息的数量。不过,我得到的外键列NULL值。在信息和WorkDetails表的PK是自动递增。

MyDatabaseAdapter.java

 公共无效的onCreate(SQLiteDatabase DB)
        {
           db.execSQL(CREATE TABLE+ TABLE_INFO +(ID INTEGER PRIMARY KEY,Name文本));
           db.execSQL(CREATE TABLE+ TABLE_WORKDETAILS +(ID INTEGER PRIMARY KEY,项目文本,WorkDescription TEXT,每文本,TimeIn DATETIME,超时DATETIME,TotalHours DATETIME,TableInfo_id INTEGER,外键(TableInfo_id)参考文献TABLE_INFO(ID)) ;
        }

WorkDetailsTable.java

  WD =新com.example.project.project.API.WorkDetailsAPI(本);
 TS =新com.example.project.project.API.InfoAPI(本);    按钮BTN1 =(按钮)findViewById(R.id.button2);
        btn1.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看为arg0){
                W1 = txtWork1.getText()的toString()。
                W2 = txtWork2.getText()的toString()。
                W3 = txtWork3.getText()的toString()。
                W4 = txtWork4.getText()的toString()。
                A1 = spinnerTra.getSelectedItem()的toString()。
                A2 = spinnerTra2.getSelectedItem()的toString()。
                A3 = spinnerTra3.getSelectedItem()的toString()。
                A4 = spinnerTra4.getSelectedItem()的toString()。
                。P1 = per1.getText()的toString();
                。P2 = per2.getText()的toString();
                。P3 = per3.getText()的toString();
                。P4 = per4.getText()的toString();
                ts.insertTimeSheet(名); //指TimeSheetAPI
                WD.insertWorkDetails(A1,W 1,P 1,B,C,TH); //插入多行,并参考WorkDetailsAPI
                WD.insertWorkDetails(A2,W2,P2,D,E1,TH);
                WD.insertWorkDetails(A3,W3,P3,F,G,日);
                WD.insertWorkDetails(A4,W4,P4,H,I,TH);
            }
        });

InfoAPI.java

 公共类InfoAPI {
    私人SQLiteDatabase数据库;
    私人MyDatabaseHelper dbHelper;
    公众的String [] = allColumns {MyDatabaseHelper.ID,MyDatabaseHelper.Name};    公共InfoAPI(上下文的背景下)
    {
        dbHelper =新MyDatabaseHelper(背景);
    }    公共无效的open()抛出的SQLException {
        数据库= dbHelper.getWritableDatabase();    }    公共无效的close(){
        dbHelper.close();
    }
    众长insertTimeSheet(字符串名称)
    {
        数据库= dbHelper.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(MyDatabaseHelper.Name,名);
        database.insert(MyDatabaseHelper.TABLE_INFO,空,价值);
        database.close();
        返回0;    }}

WorkDetailsAPI.java

 公共类WorkDetailsAPI {
    私人SQLiteDatabase数据库;
    私人MyDatabaseHelper dbHelper;
    公众的String [] allColumns={MyDatabaseHelper.ID2,MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Per,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut,MyDatabaseHelper.TotalHours,MyDatabaseHelper.TableInfo_id};    公共WorkDetailsAPI(上下文的背景下)
    {
        dbHelper =新MyDatabaseHelper(背景);
    }    公共无效的open()抛出的SQLException {
        数据库= dbHelper.getWritableDatabase();
    }    公共无效的close(){
        dbHelper.close();
    }
    众长insertWorkDetails(字符串项目,字符串workDescription,每串,串timeIn,超时字符串,字符串totalHours)
    {
        数据库= dbHelper.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(MyDatabaseHelper.Project,项目);
        values​​.put(MyDatabaseHelper.WorkDescription,workDescription);
        values​​.put(MyDatabaseHelper.Per,份);
        values​​.put(MyDatabaseHelper.TimeIn,timeIn);
        values​​.put(MyDatabaseHelper.TimeOut,超时);
        values​​.put(MyDatabaseHelper.TotalHours,totalHours);
        database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,空,价值);
        database.close();
        返回0;    }}


解决方案

改变你的插入方法是这样,所以你得到你table_info ID

 众长insertTimeSheet(字符串名称)
{
    数据库= dbHelper.getWritableDatabase();
    ContentValues​​值=新ContentValues​​();
    values​​.put(MyDatabaseHelper.Name,名);
    database.insert(MyDatabaseHelper.TABLE_INFO,空,价值);
    光标光标= database.rawQuery(SELECT MAX(ID)FROM Table_Info,NULL);
    database.close();
    返回cursor.getLong(0);
}

和改变你的code

 长ID = ts.insertTimeSheet(名); //指TimeSheetAPI
WD.insertWorkDetails(A1,W 1,P 1,B,C,日,ID);

您insertwd应

 众长insertWorkDetails(字符串项目,字符串workDescription,每串,串timeIn,超时字符串,字符串totalHours,长ID)
{
    数据库= dbHelper.getWritableDatabase();
    ContentValues​​值=新ContentValues​​();
    values​​.put(MyDatabaseHelper.Project,项目);
    values​​.put(MyDatabaseHelper.WorkDescription,workDescription);
    values​​.put(MyDatabaseHelper.Per,份);
    values​​.put(MyDatabaseHelper.TimeIn,timeIn);
    values​​.put(MyDatabaseHelper.TimeOut,超时);
    values​​.put(MyDatabaseHelper.TotalHours,totalHours);
    values​​.put(TableInfo_id,ID);
    database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,空,价值);
    database.close();
    返回0;}

I have two table (Information and WorkDetails) in SQLite where WorkDetails has a foreign key which refer to Information. When everytime the data inserted, the foreign key should follow the number of PK in Information. However, I get NULL value in foreign key column. The PK in Information and WorkDetails Table is auto-increment.

MyDatabaseAdapter.java

  public void onCreate(SQLiteDatabase db)
        {
           db.execSQL("create table "+TABLE_INFO+"(ID INTEGER PRIMARY KEY ,Name TEXT)");
           db.execSQL("create table"+TABLE_WORKDETAILS+"(ID INTEGER PRIMARY KEY , Project TEXT, WorkDescription TEXT, Per Text, TimeIn DATETIME, TimeOut DATETIME,TotalHours DATETIME, TableInfo_id INTEGER, FOREIGN KEY(TableInfo_id)REFERENCES TABLE_INFO(ID)");
        }

WorkDetailsTable.java

WD= new com.example.project.project.API.WorkDetailsAPI(this);
 ts= new com.example.project.project.API.InfoAPI(this);

    Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                W1=txtWork1.getText().toString();
                W2=txtWork2.getText().toString();
                W3=txtWork3.getText().toString();
                W4=txtWork4.getText().toString();
                a1 = spinnerTra.getSelectedItem().toString();
                a2= spinnerTra2.getSelectedItem().toString();
                a3 = spinnerTra3.getSelectedItem().toString();
                a4=spinnerTra4.getSelectedItem().toString();
                P1=per1.getText().toString();
                P2=per2.getText().toString();
                P3=per3.getText().toString();
                P4=per4.getText().toString();
                ts.insertTimeSheet(name); // refer to TimeSheetAPI
                WD.insertWorkDetails(a1,W1,P1,b,c,th); // insert multiple row and refer to WorkDetailsAPI
                WD.insertWorkDetails(a2,W2,P2,d,e1,th);
                WD.insertWorkDetails(a3, W3, P3, f, g,th);
                WD.insertWorkDetails(a4,W4,P4,h,i,th);
            }
        });

InfoAPI.java

   public class InfoAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    public String[] allColumns={MyDatabaseHelper.ID,MyDatabaseHelper.Name};

    public InfoAPI(Context context)
    {
        dbHelper=new MyDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();

    }

    public void close() {
        dbHelper.close();
    }
    public long insertTimeSheet(String name)
    {
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MyDatabaseHelper.Name,name);
        database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
        database.close();
        return 0 ;

    }

}

WorkDetailsAPI.java

public class WorkDetailsAPI {
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    public String[] allColumns={MyDatabaseHelper.ID2,MyDatabaseHelper.Project,MyDatabaseHelper.WorkDescription,MyDatabaseHelper.Per,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut,MyDatabaseHelper.TotalHours,MyDatabaseHelper.TableInfo_id};

    public WorkDetailsAPI(Context context)
    {
        dbHelper=new MyDatabaseHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }
    public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours)
    {
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MyDatabaseHelper.Project,project);
        values.put(MyDatabaseHelper.WorkDescription,workDescription);
        values.put(MyDatabaseHelper.Per,per);
        values.put(MyDatabaseHelper.TimeIn,timeIn);
        values.put(MyDatabaseHelper.TimeOut,timeOut);
        values.put(MyDatabaseHelper.TotalHours, totalHours);
        database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
        database.close();
        return 0 ;

    }

}
解决方案

change your insert method like this so you get your table_info id

public long insertTimeSheet(String name)
{
    database=dbHelper.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(MyDatabaseHelper.Name,name);
    database.insert(MyDatabaseHelper.TABLE_INFO,null,values);
    Cursor cursor = database.rawQuery("SELECT MAX(ID) FROM Table_Info", null);
    database.close();
    return cursor.getLong(0) ;
}

and change your code

long id = ts.insertTimeSheet(name); // refer to TimeSheetAPI
WD.insertWorkDetails(a1,W1,P1,b,c,th,id);

your insertwd should

public long insertWorkDetails(String project, String workDescription, String per,String timeIn,String timeOut,String totalHours, long id)
{
    database=dbHelper.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(MyDatabaseHelper.Project,project);
    values.put(MyDatabaseHelper.WorkDescription,workDescription);
    values.put(MyDatabaseHelper.Per,per);
    values.put(MyDatabaseHelper.TimeIn,timeIn);
    values.put(MyDatabaseHelper.TimeOut,timeOut);
    values.put(MyDatabaseHelper.TotalHours, totalHours);
    values.put("TableInfo_id", id);
    database.insert(MyDatabaseHelper.TABLE_WORKDETAILS,null,values);
    database.close();
    return 0 ;

}

这篇关于外键列获取NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:21