#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    mysql auto_increase, nextNumber


    I need a way to find the next number that is going to be asinged to an auto_increasing value, using MySQLdb module.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Well here is something that works for me:

    Code:
    db = MySQLdb.connect(host="hostname", user="webuser", passwd="pword", db="database")
    cursor = db.cursor()
    cursor.execute("SELECT id FROM some_table ORDER BY id DESC")
    tuple = cursor.fetchall()
    next_number = int(tuple[0][0]) + 1
    I should warn you that I'm no programmer as of yet. I just started learning this stuff about 2 weeks ago but it works for me.
    I'll learn this stuff someday.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ya, that is basicly what I am currently doing. I have done some things into mysql to see if i could find a better method. It doesn't seem tho that there really is. If anyone knows of a faster method, please share.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Aug 2004
    Posts
    1,795
    Rep Power
    84
    you could do your way and limit 1,1 so it will only give you one result or you can use the following sql

    Code:
    "SELECT MAX(id) FROM some_table"
  8. #5
  9. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    You may be able to use:

    SELECT LAST_INSERT_ID() FROM tablename;

    Then, just increment it.

    btw: crossposting is bad.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ya i know, i was looking for a way to do it in python, then i releaised it may be better done with mysql itself. I thought this thread was dead, so i posted in mysql to see if i could get better results.

IMN logo majestic logo threadwatch logo seochat tools logo