variable in tableName MySQL-Python

This is the place for queries that don't fit in any of the other categories.

variable in tableName MySQL-Python

Postby appollosputnik » Mon Aug 26, 2013 3:49 pm

In my table name for mysql I have variabale as name. I trying with the below code but it's not working. Can you help what wil be the right syntax?...Any help is highly appreciated. Please help me with the CreateTable and InsertTable syntax. Thanks Sujan
Code: Select all
    int n
    tableName = 'paypalTable_' + str(n)

    field1_name_and_type = 'TransactionNo INT,\
                    Withdrawnto TEXT,\
                    OriginalTransaction TEXT,\
                    Sentby CHAR(200),\
                    Buyeremail CHAR(200),\
                    Paymentsentto CHAR(200),\
                    Netamount CHAR(100),\
                    Date CHAR(100),\
                    Time CHAR(100),\
                    CustomerServiceURL CHAR(200),\
                    CustomerServiceEmail CHAR(200)'

    sql="CREATE TABLE IF NOT EXISTS %s (%s) " % (tableName, field1_name_and_type)
    cur.execute(sql)
    db.commit()

  sql = "INSERT INTO %s (TransactionNo, Withdrawnto, OriginalTransaction, Sentby,  Buyeremail,\
         Paymentsentto, Netamount, Date, Time, CustomerServiceURL, CustomerServiceEmail) \
         VALUES('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (tableName), \
         (trans, withdrawn, OriginalTransaction, Sentby, Buyeremail, Paymentsentto, Netamount, Date, Time, CustomerServiceURL, CustomerServiceEmail)
       
appollosputnik
 
Posts: 7
Joined: Tue Aug 06, 2013 9:13 am

Re: variable in tableName MySQL-Python

Postby micseydel » Mon Aug 26, 2013 6:48 pm

appollosputnik wrote:I trying with the below code but it's not working.

Could you elaborate on that? Do you get a traceback? Do you get a SyntaxError? Does the syntax error point somewhere specific?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1369
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: variable in tableName MySQL-Python

Postby appollosputnik » Tue Aug 27, 2013 3:44 am

error: Unknow database name.

I am creating Table but it is refering database name.
appollosputnik
 
Posts: 7
Joined: Tue Aug 06, 2013 9:13 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], W3C [Linkcheck] and 5 guests