I want to add a row to a database table, but if a row exists with the same unique key I want to update the row. For example: INSERT INTO table_name (ID, NAME, AGE) VALUES(1, "A", 19); Le...
Insert into a MySQL table or update if exists I want to add a row to a database table, but if a row exists with the same unique key I want to update the row. For example, insert into table (id, name, age) values(1, "A", 19) Let’s say the un...
I am little confused with insert on duplicate update query. I have MySQL table with structure like this: record_id (PRIMARY, UNIQUE) person_id (UNIQUE) some_text some_other_text I want to update
I am little confused with insert on duplicate update query. I have MySQL table with structure like this: record_id (PRIMARY, UNIQUE) person_id (UNIQUE) some_text some_other_text I want to update
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 'IF ROW_COUNT()=0 INSERT INTO Table (Account, Location, Value) VALUES ('A' at line 1
Maybe I am doing it wrong ?
B4X:
Dim item, Account, location As String
item = "123"
Account = "ABC"
location = "Test"
sql.ExecNonQuery2("UPDATE Table SET Item=? WHERE Location=? AND Account=? IF ROW_COUNT()=0 INSERT INTO Table (Account, Location, Item) VALUES (?,?,?)",Array (item,location,Account,Account,location,item))
I am going to see if I can re-design my SQL database, and include a primary key somehow based on the data I am using. This way I can use the ON DUPLICATE KEY function in the query.
Dim myItem As String = "123"
Dim myAccount As String = "ABC"
Dim myLocation As String = "Test"
Dim itemsql1 As SQL
itemsql1.Initialize2("org.mariadb.jdbc.Driver", "jdbc:mariadb://" & ABMShared.myServerAddress & ":3306/" & myServerDatabase, myServerUsername, myServerPassword)
Dim itemCursor1 As ResultSet
itemCursor1 = itemsql1.ExecQuery("SELECT * FROM mytable WHERE Item = '" & myItem & "' AND account = '" & myAccount & "' AND location = '" & myLocation & "'")
If itemCursor1.Nextrow = True Then
itemsql1.ExecNonQuery2("Update mytable SET item = ? WHERE location = '" & myLocation & "' AND Account = '" & myAccount & "'", Array As Object(myItem))
Else
itemsql1.ExecNonQuery2("INSERT INTO contract VALUES(?,?,?,)", Array As Object(myItem,myAccount,myLocation))
Endif
itemCursor1.Close
itemsql1.close