/ Published in: Groovy
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env groovy import groovy.sql.Sql /*println 'Show all tables' sql.eachRow('show tables'){ row -> println row[0] } println ''*/ println 'Create user table' create table if not exists user( id int auto_increment primary key, name varchar(63) not null, password varchar(35) not null ) ''') println '' println 'Create category table' create table if not exists category( id int auto_increment primary key, name varchar(63) not null, parent_id int null ) ''') println '' println 'Create product table' create table if not exists product( id int auto_increment primary key, category_id int not null, name varchar(63) not null, price decimal(10,2) not null, description text null ) ''') println '' //Generate some data. insertUser = 'insert into user(name, password) values(?, ?)' println "Inserting user: tester$idx" //sql.executeInsert(insertUser, ["tester", "a"]) //Doesn't work??? } insertCategory = 'insert into category(name, parent_id) values(?,?)' println "Inserting category: cat$idx" } lastCatId = sql.firstRow('select last_insert_id()')[0] insertProduct = 'insert into product(category_id, name, price, description) values(?,?,?,?)' println "Inserting product: product$idx" }