sql update

Flutter, sqflite, and escaping quotes with SQL INSERT and UPDATE statements

When you want to do a SQL INSERT or UPDATE using the Flutter sqflite package, I’ve found that it’s best to use their insert and update methods (as shown below) so you don’t have to worry about escaping single- and double-quotes. This is similar to the reason why Java developers switched from Statement to PreparedStatement way back in the late 1900s.

As an example, given this SQLite database table:

A ScalikeJdbc SQL SELECT example that maps results to a list of case class instances

Here’s another ScalikeJdbc example. The main benefits of this example are:

  • It shows how to perform a SQL SELECT with ScalikeJdbc
  • How to map the ResultSet values to create a series of objects from the ResultSet
  • How to connect to a database with ScalikeJdbc using a JDBC style of connecting (as opposed to putting the JDBC parameters in a configuration file)

Here’s the ScalikeJdbc example code:

A ScalikeJdbc SQL UPDATE syntax example

Here’s an example of how to write a SQL UPDATE query with ScalikeJdbc:

def updateUrlClickGeoInfo(urlClickId: Long, gi: GeoInfo): Int = NamedDB('kbhr) localTx { implicit session =>
    sql"""
    update url_clicks
    set city = ${gi.city}, state = ${gi.state}, country = ${gi.country}
    where id = ${urlClickId}
    """.update.apply()
}

(As a note to self, this code comes from my PopulateKbhrGeo1 app.)

Update MySQL timestamp on update

MySQL timestamp FAQ: How can I update a MySQL TIMESTAMP field when I issue an update for a MySQL database table?

MySQL has some crazy rules about how timestamp fields can be created, but one nice "timestamp update" syntax you can use looks like this, assuming that you're declaring a timestamp field named last_updated:

A MySQL PHP update query example

Nothing major here, just an example MySQL PHP UPDATE query (really, a PHP SQL UPDATE query example that happens to use a MySQL database). For all the things I've done lately with PHP, I think this is the first PHP update query I've written, and I wanted to put an example out here so I can find it easily later.

So, with no further ado, here is a simple MySQL PHP update query example:

Spring Dao - a Spring JDBC update example

Spring JDBC FAQ: Can you provide an example of a SQL UPDATE query used with Spring JDBC, i.e., Spring Dao class and methods?

Here's some example Java code from a Spring Dao class where I use Spring JDBC and the JDBCTemplate update method to run a SQL UPDATE statement. In short, if you can get access to the Spring JDBCTemplate, this code shows a simple example of how to execute a SQL UPDATE statement.

Java PreparedStatement - a SQL UPDATE example

Java SQL FAQ: Can you provide a Java PreparedStatement example that shows how to use a SQL UPDATE?

Sure. I have quite a few examples on this website, just see the "Related" section for those. But for now, here's a short sample method that performs a JDBC SQL UPDATE using a Java PreparedStatement: