THE SQL CREATE STATEMENT HAS EXCUTES WITH ERROR - java

Can any one help identify where the problem is with this sql create statement. Am trying to excute using JDBC driver but it returns an error:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTSasms.customerAccount ( trn_id INT NOT NULL AUTO' at line 1.
Am using mysql db
String createAccount = "DROP TABLE IF EXISTS `asms`.`customerAccount`"
+ "CREATE TABLE IF NOT EXISTS`asms`.`customerAccount` ( "
+ " `trn_id` INT NOT NULL AUTO_INCREMENT, "
+ " `trn_date` DATE NOT NULL, "
+ " `narration` VARCHAR(200) NULL, "
+ " `value_date` VARCHAR(50) NULL, "
+ " `debit` DOUBLE NULL, `credit` DOUBLE NULL,"
+ " `ledger_balance` DOUBLE NULL DEFAULT 0.0, "
+ " `credit_account_no` VARCHAR(50) NULL, "
+ " `customerAccountcol` VARCHAR(100)NULL, "
+ " `tra_ref_number` VARCHAR(50) NULL, "
+ " `trn_type` VARCHAR(10) NULL, "
+ " `action_type` VARCHAR(10) NULL, "
+ "`approve_insu_funds` VARCHAR(10) NULL,"
+ " PRIMARY KEY (`trn_id`), "
+ " UNIQUE INDEX `debit_UNIQUE` (`debit` ASC),"
+ " UNIQUE INDEX `credit_UNIQUE` (`credit` ASC),"
+ " UNIQUE INDEX `ledger_balance_UNIQUE` (`ledger_balance` ASC))ENGINE = InnoDB AUTO_INCREMENT = 00001";

This should be 2 queries, 1st one drops the table, 2nd one creates the new one
1st drop the table if exists
String dropTableQuery= "DROP TEMPORARY TABLE IF EXISTS `asms`.`customerAccount`";
//execute the query now
then do the create query
String createAccount ="CREATE TABLE IF NOT EXISTS`asms`.`customerAccount` ( "
+ " `trn_id` INT NOT NULL AUTO_INCREMENT, "
+ " `trn_date` DATE NOT NULL, "
+ " `narration` VARCHAR(200) NULL, "
+ " `value_date` VARCHAR(50) NULL, "
+ " `debit` DOUBLE NULL, `credit` DOUBLE NULL,"
+ " `ledger_balance` DOUBLE NULL DEFAULT 0.0, "
+ " `credit_account_no` VARCHAR(50) NULL, "
+ " `customerAccountcol` VARCHAR(100)NULL, "
+ " `tra_ref_number` VARCHAR(50) NULL, "
+ " `trn_type` VARCHAR(10) NULL, "
+ " `action_type` VARCHAR(10) NULL, "
+ "`approve_insu_funds` VARCHAR(10) NULL,"
+ " PRIMARY KEY (`trn_id`), "
+ " UNIQUE INDEX `debit_UNIQUE` (`debit` ASC),"
+ " UNIQUE INDEX `credit_UNIQUE` (`credit` ASC),"
+ " UNIQUE INDEX `ledger_balance_UNIQUE` (`ledger_balance` ASC))ENGINE = InnoDB AUTO_INCREMENT = 00001";
//and ofc. execute the query

Related

jdbc foreign key constraint

I am creating a java application and I have connected to mysql database. I tried creating a table with foreign key and I got error Cannot add foreign key constraint below are my tables
execute("CREATE TABLE " + BBBBB + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " consultantID varchar(225) NOT NULL,\n"
+ " name varchar(225) NOT NULL,\n"
+ " gender varchar(225) NOT NULL,\n"
+ " age varchar(225) NOT NULL,\n"
+ " email varchar(225) NOT NULL,\n"
+ " mobile varchar(225) NOT NULL,\n"
+ " address varchar(225) NOT NULL,\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
);
execute("CREATE TABLE " + AAAAA + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " patientID varchar(225) NOT NULL,\n"
+ " name varchar(225) NOT NULL,\n"
+ " gender varchar(225) NOT NULL,\n"
+ " age varchar(225) NOT NULL,\n"
+ " insurance varchar(225),\n"
+ " mobile varchar(225) NOT NULL,\n"
+ " address varchar(225) NOT NULL,\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
);
execute("CREATE TABLE " + CCCCC + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " patientID varchar(225) NOT NULL,\n"
+ " consultantID varchar(225) NOT NULL,\n"
+ " time varchar(225) NOT NULL,\n"
+ " date varchar(225) NOT NULL,\n"
+ " renewCount INTEGER default 0,\n"
+ " FOREIGN KEY (patientID) REFERENCES AAAAA(pID),\n"
+ " FOREIGN KEY (consultantID) REFERENCES BBBBB(cID),\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
I got an error as stated above. additional codes would be added on request. Thanks
in the third statement, pID and cID don't seem to exist, they should be patientID and consultantID
to create the tables you are using the variables AAAAA and BBBBB, but in the third statement you are referring to them as the string constants "AAAAA" and "BBBBB", which might not be correct, you should always use the variables, for example:
" ... REFERENCES " + BBBBB + "(cID) ... "

om.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;

Im attempting to create 2 tables, but I dont fully understand where Im going wrong. It's obviously a syntax error, and Im new to SQL in general, can someone explain to me what Im doing wrong here?
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" BOOLEAN friendly_fire NOT NULL," +
" VARCHAR(255) hq," +
" VARCHAR(255) rally," +
" PRIMARY KEY (team_name))";
This is my stacktrace:
WARN com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'manager NOT NULL, BOOLEAN team_chat NOT NULL, PRIMARY KEY (uuid))' at line 1
Code used to execute:
private void createTables(String tableName, String secondTableName) throws SQLException {
try {
checkConnection();
} catch (Exception e) {
e.printStackTrace();
}
try {
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" BOOLEAN friendly_fire NOT NULL," +
" VARCHAR(255) hq," +
" VARCHAR(255) rally," +
" PRIMARY KEY (team_name))";
PreparedStatement preparedStatement = connection.prepareStatement(query);
PreparedStatement preparedStatement2 = connection.prepareStatement(query2);
preparedStatement.execute();
preparedStatement2.execute();
main.getLogger().log(Level.INFO, "Tables " + tableName + " and " + secondTableName + " were successfully created.");
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}
The problem is presence of MAX in the table definition. VARCHAR expects a number as an argument so you either need to define MAX as variable and pass it (from Java) or just replace MAX with a number, e.g.:
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR( + " + MAX + ") NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
OR
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
If you do not know the max length then you can use LONGTEXT as type.
Also, we need to fix the ordering of type and column names, it needs to be manager BOOLEAN and not BOOLEAN manager, e.g.:
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" manager BOOLEAN NOT NULL," +
" team_chat BOOLEAN NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" friendly_fire BOOLEAN NOT NULL," +
" hq VARCHAR(255)," +
" rally VARCHAR(255)," +
" PRIMARY KEY (team_name))";
The column name has to come before the data type. So
BOOLEAN manager NOT NULL,
BOOLEAN team_chat NOT NULL,
should be
manager BOOLEAN NOT NULL,
team_chat BOOLEAN NOT NULL,

java.sql.SQLException: ORA-06550

When I am trying to execute this sql statement, I am getting exception as-
java.sql.SQLException: ORA-06550: line 1, column 429:
PLS-00103: Encountered the symbol "/" The symbol "/" was ignored.
This is the below sql String that I am executing- Is there anything wrong with this sql? It will check whether table is there or not, if it is there then it will not create a table and if it is not there, then it will create a table.
public static final String CREATE1 = "DECLARE " +
"t_count INTEGER; " +
"v_sql VARCHAR2(1000) := 'create table " +DATABASE_TABLE + " ( " +
"(ID number(10,0), " +
"CGUID VARCHAR(255), " +
"PGUID VARCHAR(255), " +
"SGUID VARCHAR(255), " +
"USERID VARCHAR(255), " +
"ULOC VARCHAR(255), " +
"SLOC VARCHAR(255), " +
"PLOC VARCHAR(255), " +
"ALOC VARCHAR(255), " +
"SITEID VARCHAR(255), " +
"PRIMARY KEY ( ID ))'; " +
"BEGIN " +
"SELECT COUNT(*) " +
"INTO t_count " +
"FROM user_tables " +
"WHERE table_name = '" +DATABASE_TABLE + "'; " +
"IF t_count = 0 THEN " +
"EXECUTE IMMEDIATE v_sql; " +
"END IF; " +
"END; ";
It is getting printed on the console as-
DECLARE t_count INTEGER; v_sql VARCHAR2(1000) := 'create table LnPData((ID number(10,0), CGUID VARCHAR(255), PGUID VARCHAR(255), SGUID VARCHAR(255), USERID VARCHAR(255), ULOC VARCHAR(255), SLOC VARCHAR(255), PLOC VARCHAR(255), ALOC VARCHAR(255), SITEID VARCHAR(255), PRIMARY KEY ( ID ))'; BEGIN SELECT COUNT(*) INTO t_count FROM user_tables WHERE table_name = 'LnPData'; IF t_count = 0 THEN EXECUTE IMMEDIATE v_sql; END IF; END;
Remove ';' after your END IF statement and run it again.

java.sql.SQLException: ORA-00936: missing expression

Below I am creating table.
public static final String CREATE_SQL = "CREATE TABLE " +DATABASE_TABLE +
"(ID number(10,0), " +
" CGUID VARCHAR(255), " +
" PGUID VARCHAR(255), " +
" SGUID VARCHAR(255), " +
" USERID VARCHAR(255), " +
" ULOC VARCHAR(255), " +
" SLOC VARCHAR(255), " +
" PLOC VARCHAR(255), " +
" ALOC VARCHAR(255), " +
" SITEID VARCHAR(255), " +
" ATTRIBUTEID VARCHAR(255), " +
" ATTRIBUTEVALUE VARCHAR(255), " +
" PRIMARY KEY ( ID ))";
This is the below UPSERT_SQL query when I am trying to update my database table I am always getting- java.sql.SQLException: ORA-00936: missing expression
. I checked my SQL, I am not able to find where the expression is missing. Is something wrong with the below SQL?
public static final String UPSERT_SQL = "MERGE INTO " +DATABASE_TABLE+ " USING ( SELECT ? AS ID, " + // We will maybe add this record
" ? AS CGUID, " +
" ? AS PGUID, " +
" ? AS SGUID, "+
" ? AS USERID, "+
" ? AS ULOC, "+
" ? AS SLOC, "+
" ? AS PLOC, "+
" ? AS ALOC, "+
" ? AS SITEID, "+
" ? AS ATTRIBUTEID, "+
" ? AS ATTRIBUTEVALUE, "+
" FROM dual ) maybe "+
" ON (maybe.ID = "+DATABASE_TABLE+".ID) "+
" WHEN MATCHED THEN "+
// We only need update the fields that might have changed
" UPDATE SET " +DATABASE_TABLE+ ".ULOC = maybe.ULOC, " +DATABASE_TABLE+ ".SLOC = maybe.SLOC, " +DATABASE_TABLE+ ".PLOC = maybe.PLOC, " +DATABASE_TABLE+ ".ALOC = maybe.ALOC "+
" WHEN NOT MATCHED THEN "+
// Insert new record
" INSERT VALUES (maybe.ID, maybe.CGUID, maybe.PGUID, maybe.SGUID, maybe.USERID, maybe.ULOC, maybe.SLOC, maybe.PLOC, maybe.ALOC, maybe.SITEID, maybe.ATTRIBUTEID, maybe.ATTRIBUTEVALUE)";
And from below I am executing that UPSERT_SQL Statement.
LnPDataConstants.PSTMT = LnPDataConstants.DB_CONNECTION.prepareStatement(LnPDataConstants.UPSERT_SQL); // create a statement
LnPDataConstants.PSTMT.setInt(1, (int) ind);
LnPDataConstants.PSTMT.setString(2, LnPDataConstants.CGUID_VALUE);
LnPDataConstants.PSTMT.setString(3, LnPDataConstants.PGUID_VALUE);
LnPDataConstants.PSTMT.setString(4, LnPDataConstants.SGUID_VALUE);
LnPDataConstants.PSTMT.setString(5, LnPDataConstants.UID_VALUE);
LnPDataConstants.PSTMT.setString(6, LnPDataConstants.ULOC_VALUE);
LnPDataConstants.PSTMT.setString(7, LnPDataConstants.SLOC_VALUE);
LnPDataConstants.PSTMT.setString(8, LnPDataConstants.PLOC_VALUE);
LnPDataConstants.PSTMT.setString(9, LnPDataConstants.ALOC_VALUE);
LnPDataConstants.PSTMT.setString(10, LnPDataConstants.SITEID_VALUE);
LnPDataConstants.PSTMT.setString(11, "10200");
LnPDataConstants.PSTMT.setString(12, attrValue1.toString().split("=")[1]);
LnPDataConstants.PSTMT.executeUpdate();
Yes, there is something wrong with the SQL, and it is that you wrote a comma before FROM dual. This causes Oracle's SQL parser to complain.

Inserting into database

I have written a query to insert values into a database in Android:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " );");
However, when I execute it, an error is thrown. Can anyone point out the error in it?
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" INTEGER NOT NULL," + KEY_LONGITUDE + " INTEGER NOT NULL" + " );");
Provide a Datatype to KEY_LATITUDE and to KEY_LONGITUDE.
And also you have kept a (,) at last which was not needed...
the correct code is:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL," + KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " )");
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL);");

Categories

Resources