Thread: Insert into sql

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

    Join Date
    Nov 2012
    Posts
    38
    Rep Power
    2

    Insert into sql


    Hi guys i'm having problems with sql. Supposed that I have 3 arguments like this: a = 'abc', b = 'bca', c ='cba' and when I used the sql command
    Code:
    con.execute('INSERT INTO Facebook (Description, ByPersonName, Date) VALUES (%s, %s, %s)', a, b, c)
    it said TypeError: execute() takes exactly 3 arguments (5 given) I tried with executemany and changed %s to ?, it's still keep saying like this.
    Can somebody help me find out the solution of this problem pleasee >o<

    Thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    one or both of these should workd:
    Code:
    con.execute('INSERT INTO Facebook (Description, ByPersonName, Date) VALUES (%s, %s, %s)', (a, b, c) )
    
    con.execute('INSERT INTO Facebook (Description, ByPersonName, Date) VALUES (%s, %s, %s)'%(a, b, c) )
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    38
    Rep Power
    2
    Oh I see thank you so much for your help
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    38
    Rep Power
    2
    Hi, may I ask you more. Supposed I have a data like this :

    kk = ('a', 'b', 'c')
    jj = ('myne', 'myne', 'myne')
    dd = ('2013-01-28', '2013-01-28', '2013-01-28')

    When I tried to use executemany

    Code:
    con.executemany("INSERT INTO Facebook (Description, ByPersonName, Date) VALUES (%s, %s, %s)", (kk , jj, dd))
    the results I get is

    Line 1: Description = a, ByPersonName = b, Date = c
    Line 2: Description = myne, ByPersonName = myne,Date = myne
    Line 3: Description = 2013-01-28, ByPersonName = 2013-01-28,Date = 2013-01-28

    The data doesn't get into column properly, if you know this problem please tell me, Thank you!!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Again, without actually knowing what I'm doing, you need transposition. zip transposes.
    Code:
    con.executemany("INSERT INTO Facebook (Description, ByPersonName, Date) VALUES (%s, %s, %s)", zip(kk , jj, dd))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo