Prepare for insertions...

But first of all we have to deal with the creation of row ids.

I'm not a big fan of automatic ids, so I built a small ID generation system. Lol, nice name, isn't it ?

In practice, it's just a table that holds the last ID given for a table and a column.

The table looks like this :

CREATE TABLE ids (
  tbl varchar(45) NOT NULL,
  fld varchar(45) NOT NULL,
  lastId int(11) DEFAULT NULL,
  PRIMARY KEY (tbl,fld)
);

 

To get a new ID for the table posts and the column post_id, I just have to call the static method

SqlQuery.getId('posts', 'post_id');
It delivers an integer ID, synchronously. We'll see on next page that it's very practical.

If no row exists in the table, it inserts it, else it increases the field lastId. In every case, it returns an integer :)

Of course the names are totally virtual, so you don't need to provide the real names of your tables / fields.

 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.