tags:

views:

282

answers:

2

i have 2 tables

        "CREATE TABLE if not exists tag_name( " +
        "tagid INTEGER PRIMARY KEY, " +
        "title TEXT UNIQUE);";

        "CREATE TABLE if not exists media_tags( " +
        "media_id INTEGER, " +
        "tagid INTEGER);";

Then i insert them with this code. The problem is, last_insert_rowid is not what i expect. I expect when INSERT is ignored it will return the rowid that cause the insert to fail. Example if i insert "tag1", "tag2", "tag3". then insert "tag2" i expect 2 to be returned instead of the last inserted row that was a success. How do i solve this?

        void insertMediaTags(List<string> tags, long mediaId)
        {
            foreach(string tag in tags)
            {
                command.CommandText = 
                    "INSERT OR IGNORE INTO tag_name(title) VALUES(@title); " +
                    "SELECT last_insert_rowid() AS RecordID;";
                command.Parameters.Add("@title", DbType.String).Value = tag;
                long tagid = (long)command.ExecuteScalar();
                command.CommandText =
                    "INSERT INTO media_tags(media_id, tagid) " +
                    "VALUES(@media_id, @tagid);";
                command.Parameters.Add("@media_id", DbType.Int32).Value = mediaId;
                command.Parameters.Add("@tagid", DbType.Int64).Value = tagid;
                command.ExecuteNonQuery();
            }
        }
+1  A: 

This may not be the best solution but using a SELECT statement and checking if the object is null works.

-edit- i am too lazy to change and test it but the code should be doing a insert ... where not exist (select) for currency safety reasons.

    void insertMediaTags(List<string> tags, long mediaId)
    {
        foreach(string tag in tags)
        {
            long tagId;
            command.CommandText = "SELECT tagid FROM tag_name WHERE title=@title;";
            command.Parameters.Add("@title", DbType.String).Value = tag;
            object o = command.ExecuteScalar();
            if (o == null)
            {
                command.CommandText =
                    "INSERT OR IGNORE INTO tag_name(title) VALUES(@title); " +
                    "SELECT last_insert_rowid() AS RecordID;";
                command.Parameters.Add("@title", DbType.String).Value = tag;
                tagId = (long)command.ExecuteScalar();
            }
            else
                tagId = (long)o;

            command.CommandText =
                "INSERT INTO media_tags(media_id, tagid) " +
                "VALUES(@media_id, @tagid);";
            command.Parameters.Add("@media_id", DbType.Int32).Value = mediaId;
            command.Parameters.Add("@tagid", DbType.Int64).Value = tagId;
            command.ExecuteNonQuery();
        }
    }
acidzombie24
A: 

If you use INSERT OR REPLACE instead of INSERT OR IGNORE, I believe that last_insert_rowid will be as you wish it to be.

Alex Martelli
I forgot to mention replace would change the primary key which would break all the entries using the older PK Int.
acidzombie24