SQL PreparedStatement Android

时间:2016-04-14 01:44:42

标签: android sql

我正在尝试将数据插入到我的数据库中,我尝试手动输入值,但我一直收到错误。我调试并发现我的值不是null,而是错误

Cannot insert the value NULL into column 'AssignedGameID', table 'dementiafypdb.dbo.gameRecord'; column does not allow nulls. INSERT fails.

这是我的数据库连接:

public GameManager(String pID, String gID, int sNo){
        patientID=pID;
        gameID = gID;
        sqlNo = sNo;
    }
    public GameManager(String pID, String gID, int sNo, int s, int t){
        patientID=pID;
        gameID = gID;
        sqlNo = sNo;
        score = s;
        time = t;
    }


    //This method is called to run Game Manager
    public void GameMgrStart(){
        Connection conn=null;
        String sqlStatement;
        try {
            conn = dbConnect();

            sqlStatement = sqlStats();

            //Run statement
            PreparedStatement cmdSql = conn.prepareStatement(sqlStatement);
            cmdSql.execute();

            System.out.println("" + sqlStatement);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    //List of SQL Statements
    public String sqlStats(){
        String sqlStatement= "";

        if(sqlNo == 1){
            sqlStatement = "INSERT INTO GameRecord(Score, timeTaken, AssignedGameID) Values('" + score + "', '" + time + "', (SELECT top 1 assignedGameID FROM assignedGame WHERE gameID='" + gameID + "' and patientAllocationID=(SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')))";
        }

        return sqlStatement;
    }

    //Method to connect to database
    public Connection dbConnect(){
        Connection conn = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");

            //conn = DriverManager.getConnection(dbURL+dbName+dbSecurity);
            conn = DriverManager.getConnection(dbURL+dbName+dbUsername+dbPassword);
            if (conn != null) {
                System.out.println("Connected");
                return conn;

            }
            else
                System.out.println("Failed");

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

这是我访问数据库的代码

私有类AsyncCaller扩展了AsyncTask {

@Override
protected Void doInBackground(Void... params) {
    String patientID ="1", gameID ="1";
    int sID = 1, score = 10, time = 30;

    GameManager gameManager = new GameManager(patientID,gameID,sID,score,time);
    gameManager.GameMgrStart();
    return null;
}

}

我可以知道我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

这是您的INSERT声明:

INSERT INTO GameRecord(Score, timeTaken, AssignedGameID)
    Values('" + score + "', '" + time + "', (SELECT top 1 assignedGameID FROM assignedGame WHERE gameID='" + gameID + "' and patientAllocationID=(SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')))";

这更简单,没有VALUES

INSERT INTO GameRecord(Score, timeTaken, AssignedGameID)
   SELECT top 1 '" + score + "', '" + time + "', assignedGameID
   FROM assignedGame
   WHERE gameID='" + gameID + "' and
         patientAllocationID = (SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')";

您的错误表明查询未返回任何行。使用VALUES时,缺少任何行会产生NULL。使用INSERT . . . SELECT,不会尝试插入。