Trying to add a new record to a data table in MySQL fails with an error message:
can not add or update a child row
I can do the same command manually in xampp and it works fine, but not when it runs under my app. here is the Code, starting with the table "users" and then "transactions"
public void CreateUsersTable(){
try {
String SQL = "CREATE TABLE IF NOT EXISTS Users ("
+ "Username varchar(80) NOT NULL,"
+ "Userpassword varchar(80) ,"
+ "User_GSM VARCHAR(30),"
+ "User_Tel_Home VARCHAR(30),"
+ "User_Address Varchar(100), "
+ "User_City Varchar(20), "
+ "User_Position Varchar(100), "
+ "PRIMARY KEY(Username))";
//con = DBModule.ConnectDataBase.ConnectDataBase_Method();
statement = con.prepareStatement(SQL);
statement.executeUpdate();
} catch (SQLException ex) {
CustomControls.CustomTools.CustomMsgBox(ex.getMessage());
}
}
and the second table
public void CreateTransactionsTable(){
try {
String SQL = "CREATE TABLE IF NOT EXISTS Transactions ("
+ "TransactionsNum INT(18) NOT NULL AUTO_INCREMENT,"
+ "TransactionsDate DATE,"
+ "TransactionsAmount float(8), "
+ "TransactionsUsername varchar(80) ,"
+ "PRIMARY KEY(TransactionsNum) , "
+ "FOREIGN KEY(TransactionsUsername) REFERENCES Users(Username) )"; // foregign key is the key in this table to accessed from main calling
//con = DBModule.ConnectDataBase.ConnectDataBase_Method();
statement = con.prepareStatement(SQL);
statement.executeUpdate();
} catch (SQLException ex) {
CustomControls.CustomTools.CustomMsgBox(ex.getMessage());
}
}
and finally, this the statement to update the DB
String addTRansSQL = "insert into transactions ( TransactionsDate , TransactionsAmount , TransactionsUsername ) "
+ " values( '" + sqlDate + "' , '" + tramount + "' , ' " + loggeduser + "' )";