How do I need to handle a unique_constraint on non-key attribute ? I am using Oracle database.
I have a set unique constraint on username field. (emp_id is primary key but I have to check on emp_username). When I intentionally insert a duplicate username, my program is stuck, instead of displaying any error in console while debugging.
String sql = "insert into employee(emp_username, emp_password) values (\'"+username+"\', \'"+password+"\')";
statement.executeUpdate(sql);
But on command line duplicate insertion shows an error:
ERROR at line 1:
ORA-00001: unique constraint (USMAN.UNIQUE_USERNAME) violated
it seems here that the problem is not in your code(Your code is fine), it's in the data that you are trying to insert, the username column is unique, so you can't insert value multiple times in that column .
Related
Is there any way to find the duplicate record from the given exception other than hitting MySQL with another where clause.
Exception :
java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '10:78:5b:9f:0e:c1' for key 'UK_pttkmqgejqcvdojl6gdkt8wi0'
not from the error but you can create a query to see it,
select field name
from tablename
where fieldname = 10:78:5b:9f:0e:c1
and you have to place the name of the column where this 10:78:5b:9f:0e:c1 is stored instead of "field name"
Only, Doing in Java - First Get all records and create map of that data then search record using the key.
I have the following table cl:
id - int(10) primary key
contact - int(10)
list - int(10)
With a unique index on contact and list. When I run concurrently the following query in batch by 100 records:
INSERT INTO cl(list, contact) VALUES (?, ?) ON DUPLICATE KEY UPDATE cl.id = cl.id
Under high pressure it fails in about 20% with the following error:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE cl.id = cl.id' at line 1
80% of the queries runs just fine. When I rerun failed queries with the same parameters, again 20% fail.
Why some of the queries fail and then produce no errors when executed for the second time?
Changing query to
INSERT IGNORE INTO cl(list, contact) VALUES (?, ?)
solved the issue. Though it is unclear why mysql was throwing java.sql.SQLSyntaxErrorException instead of something more unambiguous.
I have a table with 4 fields: _id, to, from, hidden
Upon trying to insert the following values:
_id=167 from=1311005879000 to=1311005879000 hidden=0
into my table, I got an SQLiteConstraintException stating that
'column _id is not unique (code 19)'
To find the cause for this problem I tried querying the size of the table and found it is 0, so I have no idea where this is coming from.
Maybe I didn't understand the error correctly?
Edit: some code!
try {
mDatabase.insertOrThrow("groups", null,
mContentValues);
} catch (SQLException e) {
e.printStackTrace();
}
Creation SQL:
CREATE TABLE IF NOT EXISTS groups(_id LONG PRIMARY KEY,hidden INTEGER,from LONG,to LONG
'column _id is not unique (code 19)'
So you are violating UNIQUE Constraint. This is reason of SQLiteConstraintException. Your _id column is most likely primary key and primary keys have implicitly assigned UNIQUE constraint that say no two rows can have same primary key.
You are trying to insert duplicit _id that already is in db or PK is assigned to NULL.
I tried querying the size of the table and found it is 0, so I have no
idea where this is coming from.
I think your query was broken because your Exception says everything and it cannot be thrown if somewhere is not a problem.
Update:
If you are not assigned NULL to PK and also your table has 0 records probably problem is here:
mDatabase.insertOrThrow("groups", null, mContentValues);
You are assigned NULL to ColumnHack as second param of insert() method that you shouldn't. In SQLite, each row must have at least one column specified. It needs one column that can be safe assigned to NULL.
I have a Table [Name Table] and an associated table workSchedule. I'm using Hibernate 3.6.7.Final to generate my query. The result is:
update Personnel.dbo.[Name Table] set workSchedule=? where [Name IRC]=?
Which throws an Exception:
11-22#10:30:41 WARN [] JDBCExceptionReporter - SQL Error: 8624, SQLState: S0001
11-22#10:30:41 ERROR [] JDBCExceptionReporter - Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.
[Name Table].workSchedule is a foreign key defined thus:
ALTER TABLE [Name Table] ADD workSchedule VARCHAR(34)
FOREIGN KEY REFERENCES workSchedule(id);
workSchedule.id is defined like this:
CREATE TABLE workSchedule
( /* format = letter-days-lunch EX: A-5-1 */
id AS CASE lunch
WHEN 1 THEN scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'1'
ELSE scheduleLetter+'-'+CONVERT(VARCHAR, scheduleDays)+'-'+'5'
END PERSISTED NOT NULL,
/* rest of table follows */
);
If I copy paste the above update query into SSMS and plug values directly in for the ?s it works.
UPDATE:
I just tried changing out the PRIMARY KEY of the table WorkSchedule for an INT IDENTITY column. I left renamed id to shift and otherwise left it as column on the table. I also updated both POJO's and .hbm.xml files. The update query still fails, with the same exception.
I'm listing this as an answer because it's what I ended up doing to solve my problem.
I completely removed the calculated field on WorkSchedule.
I implemented the calcualations as a getter in the the POJO instead.
Re-tooled workschedule to use an IDENTITY for the PK
and re-linked [Name Table] to the new IDENTITY field instead. now it all works.
How do I resolve the error in inserting memo in Access from a Java program?
4159 the size of the string
the error
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
The source code that executes the insert statement:
statement.executeUpdate("INSERT INTO webData VALUES ("+"'" + list.get(y)+"','"+data+ "')");
4159 the size of data
my schma is :
table name webData with 2 coulmun the
first ID of type text
the second Field1 of type memo
i have update the statment but i have get the same error:
statement.executeUpdate("INSERT INTO webData (ID,Field1) VALUES ("+"'" + list.get(y)+"','"+data+ "')");
Thank you
Please post your schema.
Rather than doing:
INSERT INTO webData VALUES (...)
You should be doing:
INSERT INTO webData (MyColumn1, MyColumn2) VALUES (...)
Do not rely on the physical column order in the table, you should state it explicitly to avoid errors.
Does the comma have to be in speech marks and inverted commas? You can simplify this, just a tip :). But yes, post your DB scheme.