Mysql jdbc - Inserted values get deleted immediatly - mysql

I am using the mysql java connector (mysql-connector-java-8.0.11) to access a database.
I open a connection and generate some tables. So far everything works.
But when I try to insert a value in one of the tables, the row gets deleted immediatly.
I use following code
PreparedStatement stmt = connection.prepareStatement("INSERT INTO database_upgrade (version) VALUES (?)");
stmt.setInt(1, newVersion);
stmt.executeUpdate();
stmt.close();
When I debug into the executeUpdate the insertId and updateCount are returned correctly. But when I look at the database the entry is missing.
The autoincrement id increased which indicates that the row was inserted and then deleted, but I don't know why.
Interesting point: If I use a new connection for this insert, everything works perfectly. But when I use the connection from the previous actions, it does not.
Just for clarification
All previous statements are closed
The connection is still open
The previous Actions are some "create table" and "alter table"
Can anyone tell me why this happens? I used the same code with MS SQL server and this did not happen.
Thank you!

thanks to Gord Thompson I found it. The autocommit was turned off by some previous sql commands (that I did not double check).
Solution: I removed the autoCommit disabling part in my sql files.

Related

MySQL- Insert a record with empty primary key, works on one server, not the other

I have a table in a MySQL Server (version 5.5.27, installed with EasyPHP for development), it has an ID with Auto_Increment, and it's data type is INT(11).
When i try to insert a record, using this statement, it works.
insert into factclientes (IDFactClientes, IDTercero, SubTotal, IVA, Total)
values ('', '3', '2500.00', '400.00', '2900.00')
ON DUPLICATE KEY UPDATE IDTercero = values(IDTercero),
SubTotal = values(SubTotal),
IVA = values(IVA),
Total = values(Total)
But when i try to insert that same record on my production server (version 5.6.17, installed independently on another machine) it throws an error:
Incorrect integer value: '' for column 'IDFactClientes' at row 1
I know it is because the primary key ID 'IDFactClientes' has an empty value. I do this because i use the same statement to INSERT and to UPDATE. If my program doesn't know and doesn't specify the IDFactClientes, i want a new record, if my program knows the ID already, and it's specified i want the record to be updated.
The weird thing is that it works on my dev machine, but it doesn't on my production server.
Is there a setting im missing?? how could i fix this?? i have the exact same problem with all the tables of my database and i wouldn't want to modify all the statements in my program... if it's possible
Thank you in advance!!
I found it!!! or remembered it... a while ago i heard something about "STRICT MODE", and i suddenly remembered about it!! so i looked for how to turn off the "strict mode" and i found two methods:
Method 1:
Open the "my.ini" file within the MySQL installation directory and look for something like...
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
# Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Method 2:
You may be able to run an SQL query within your database management tool such as phpMyAdmin which can normally be found from your web hosting control panel:
SET ##global.sql_mode= '';
I Think the first method is permanent, and the second one has to be done every connection... i think

Why am I getting a table doesn't exist error for a table that exists?

I am running a simple update statement:
UPDATE sometab
SET `somefield1` = '19',
`somefield2` = '3734941'
WHERE somefield3 = '1234';
and I am getting the error:
ERROR 1146 (42S02): Table 'prod._sometab_new' doesn't exist
I can successfully select from the table where somefield3 is 1234.
Why am I getting a table doesn't exist error for a table that exists? And why does the error message refer to a different table? I don't see any triggers associated with the table.
Additional information: A colleague just noticed that it is referring to a prod scheme, but the statement is running in a dev schema built from prod. The update statement works in DBs that were built a few days ago using the same method, but all of the DBs built after some, as of yet, unknown time exhibit the error.
The current theory is that a conversion script to move us to UTF-8 is currently running and creating tables like _ORIG_new as part of its conversion. We are going to wait for the conversion script to finish and then rebuild the dev databases and see if the error still persists.
Does this happen if you also try Insert into or Delete statements ?
Insert INTO sometab(somefield1, somefield2) VALUES (a, b).
If that works you should not have problems probably, otherwise you have problems accessing your database.
Second, are you sure you are using the correct database file and that are you connected to it properly. If you are using it in external application (c#), check your connection strings.
Also check how are you executing the query. I cant think of other more specific solution to your problem

SQL query times out when updating using inner join?

This query dies when I try to execute it in PHP code and in phpMyAdmin.
UPDATE Inventory
INNER JOIN InventorySuppliers
ON Inventory.LocalSKU = InventorySuppliers.LocalSKU
SET Inventory.Integer2 = '1'
WHERE InventorySuppliers.SupplierSKU = '2D4027A6'
The error is:
1205 - Lock wait timeout exceeded; try restarting transaction
How can I prevent the lock timeout and/or solve this problem?
I can run this query in Microsoft Access correctly, and phpMyAdmin db is a copy of that Access database. Increasing the execution time is not an option for me as that will take too long for one record update.
$data1 = array('Inventory.Integer2'=>$shipping);
$this->db->where('InventorySuppliers.SupplierSKU', $SupplierSKU);
$this->db->update('Inventory inner join InventorySuppliers on Inventory.LocalSKU = InventorySuppliers.LocalSKU', $data1);
$this->db->close();
return ($this->db->affected_rows() > 0);
Issue this command before running your UPDATE.
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
Okay...Interesting for me...
As I told you, My SQL db was a copy from ms access db...Dont know what happened, but mysql database tables dont have primay key or index although original db has have them...I tried asigning PK and indexes, but mysql returned me error, My final solution was
Delete thetable in mysql
Make sure PK is asigned in table structures ( In my cases, after
importing from MS-Access, I have to do it again)
Check indexed feilds ( I have one feild indexed), and make sure index exists
and this did the trick for me, Now the same query is running okay...
thanks to all for the help...Hopes These two steps might help someone in future...

table locked or in use when calling RunSQL

I have some code which re-arranges some items on a form, but only one SQL query. All my tables aren't locked before the code runs but for some reason I get an error when running:
DoCmd.RunSQL ("Select * Into MasterTable From Year07 Where 'ClassName' = '7A'")
Error:
The database engine could not lock table because it is already in use by another person or process. (Error 3211) To complete this operation, a table currently in use by another user must be locked. Wait for the other user to finish working with the table, and then try the operation again.
Any ideas what I can do to stop the table being locked?
Is MasterTable included in your form's Record Source? If so, you can't replace it, or modify its structure, while the form is open.
Apart from the table lock issue, there is a logic error in the SELECT statement.
Where 'ClassName' = '7A'
The string, ClassName, will never be equal to the string, 7A. Therefore your SELECT can never return any records. If ClassName is the name of a field in your Year07 table, discard the quotes which surround the field name.
Where ClassName = '7A'
I'm guessing, but if you're using a form that is bound to MasterTable, you can't run a query to replace it with a new MasterTable while you've got it open in the form.
I would suggest that you get rid of the MakeTable query (SELECT INTO) and instead use a plain append query (INSERT). You'll want to clean out the old data before appending the new, though.
Basically, a MakeTable query is, in my opinion, something that does not belong in a production app, and any process that you've automated with a MakeTable query should be replaced instead with a persistent temp table that is cleared before the new data is appended to it.
I have seen this when you re-open a database after Access has crashed. Typically for me a reboot has fixed this.
What version of MSAccess? Not sure about newer ones, but for Access 2003 and previous, if you were sure nobody was in the database, you could clear up locks after a crash by deleting the .ldb file.

trouble deleting from mysql

I am fairly new to using mysql. I have an application that performs some basic querying. I am also trying to run a simple delete statement -
delete from mydb.mytable
This table is a simple 2 column table with not keys or triggers or anything defined. For some reason, the delete is not being performed. If I run the statement from MySql Workbench in the query window, it works fine. From the code, it does nothing. I am not seeing any error messages. I created a user with select, insert, update and delete rights to the schema. I am able to do the insert fine, but the delete does not seem to be working.
Is there a setting for mysql that I am missing that will not allow me to perform the delete?
Thanks for any thoughts.
Fist of all, check if
you are connected to the right database ;
you are using transaction and forgetting 'commit' ;
the user you use have enough permissions to delete from the table .
As a side notice, if you want to delete all records, you should use truncate instead of delete
Are you using transactions? My first guess is that your code might be issuing a BEGIN TRANSACTION without a COMMIT.
We would have to see some of your code to answer the question.
My guess is that you are not calling commit from your code. You can configure MySQL to auto-commit your queries, but this is usually not what you want.