Autoincrement insert-select on a non-autoincrement column

There are often times when you need to insert into an existing table that contains data with a unique numeric field. If you created the field as AUTOINCREMENT, everything is fine. However, in my case, I did not because I generate sqlite tables from my MySql database. So, I have many tables that look like this:

drop table concentration_card if exists;

create table concentration_card (
	card_id int not null default 0,
	/*
      ...
	*/
);

create unique index idx0 on concentration_card (card_id);

I came across this answer on StackOverFlow, which I used to solve my problem: http://stackoverflow.com/a/3127004/2122052

First we need the maximum value from the desired field:

select max(concentration_card); -- We don't need a +1 here because we increment in SELECT

Then set the initial @curRow value and create the insert-select query:

 --insert into concentration_card
  SELECT @curRow := @curRow + 1 as card_id, 0, p.dict_id, 
      p.dict_id, 'Verbs' as name, 0 as inactive,
      '' as comments, 6 as major_group_id
  FROM picture_dictionary p
  JOIN    (SELECT @curRow := 1526) r
  where p.word_group = 'Verbs'
   and p.`dict_id` not in (  
 select word1_id
 from concentration_card
 where name in ('Basic Verbs', 'Common Verbs')
 );
Advertisements

Simple Go Web Page (GET Method)

I wanted to give the Go language a try today for a simple web page that I needed. Unfortunately, I got tripped up when I placed an extra / at the end of my handler. Thought I would post the first working copy here before I go off and finish the page. Hopefully, no one else gets burned by that little mistake.

package main

/*
http://localhost:3000/summary?param1=one¶m2=two
*/
import (
   "fmt"
   "net/http"
)

func summaryHandler(w http.ResponseWriter, r *http.Request) {

   var header = `
    <head>
    <meta http-equiv="Content-Type" content=\"text/html; charset=utf-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"
    <script src="/js/myjs.js"></script>

    <link rel="stylesheet" href="/css/dictionary.css">
    </head>
`
    fmt.Fprint(w, header)
    fmt.Fprint(w, "<h1>Hello World: Simple Go Web Page</h1>")
    params := r.URL.Query()
    fmt.Fprint(w, r.URL)
    fmt.Fprint(w, params)
    param1 := params["param1"]
    param2 := params["param2"]

    fmt.Fprint(w, "<p>Param1=", param1, "</p>")
    fmt.Fprint(w, "<p>Param2=", param2, "</p>")
}

func setupWeb() {
    // Put static assets in the resources dir
    http.Handle("/", http.FileServer(http.Dir("resources")))
    // WARNING: "/summary/" will cause pain with the GET
    http.HandleFunc("/summary", summaryHandler)

    err := http.ListenAndServe("localhost:3000", nil)
    if err != nil {
        panic(err)
    }
}

func main() {
    setupWeb()
}