1

I have a tuple like this which is basically a DB record

record = ('R70:Congestion Ratio', 'pm_201204151900_KPI1.csv', 'pmexport_04_15_2012_20_00.tar.gz')

I am trying to convert it to tuple and then a string to get the query

"INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES %s" %(tuple(record))

However I am getting TypeError: not all arguments converted during string formatting What am I doing wrong ? Is there a better to achieve what I am trying to achieve ?

4 Answers 4

5

Don't build SQL like this. Use the proper form of your database engine.

In SQLite, it works like this:

cur.execute("""INSERT INTO PM_NEW_COUNTERS
  (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME)
  VALUES (?, ?, ?)""", (a, b, c))

For details see the Python Database API Specification v2.0.

Sign up to request clarification or add additional context in comments.

Comments

1

first of all, you shouldent be trying to make queries like this. as stated before.

secondly, to answer your question about string formatting.

record = ('R70:Congestion Ratio', 'pm_201204151900_KPI1.csv', 'pmexport_04_15_2012_20_00.tar.gz')

record is already a tuple.

secondly, you are trying to put the tuple which has 3 values, into only 1 location in your string.

so instead do:

"INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES %s" % " ".join(record))

or if you want more control:

"INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES %s %s %s" % (record[0], record[1], record[2])

3 Comments

Don't give bad advise. Building SQL like this opens the door for nasty attacks.
@Tichodroma you are correct. the OP never said anything about SQL though. im just helping with the specific problem of string formatting he is asking about.
Well, if the question doesn't contain SQL, I don't know how SQL looks.
0

You want more %s there:

"INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES ('%s', '%s', '%s')" %(tuple(record))

2 Comments

I agree that is the way to go, but this was the error he was getting.
0

Use the following, you need to add a comma and remove the % charecter.

>>> str = "INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES ('%s')" %"', '".join(record)
>>> str
"INSERT INTO PM_NEW_COUNTERS (COUNTER_NAME, CSV_FILE_NAME, TAR_FILE_NAME) VALUES ('R70:Congestion Ratio', 'pm_201204151900_KPI1.csv', 'pmexport_04 _15_2012_20_00.tar.gz')"
>>> 

Sorry about the last answer. Here is the updated code.

I would highly suggest using the built in db driver for your query however if you do want to build a string using the tuple my new code will produce the desired output.

2 Comments

OP obviously wants to use the string formatting operator % to insert the values…
I am sorry for the error, I have updated to code to just build the query string using the tuple. Its not the right way to interact with DB but for creating the string query my code works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.